OtaVersionRepository.php
2.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
<?php
namespace App\Repositories\Backend\Ota;
use App\Exceptions\GeneralException;
use App\Modules\Models\Ota\OtaVersion;
use App\Modules\Repositories\Ota\BaseOtaVersionRepository;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Storage;
/**
* Class BaseOtaVersionRepository
* @package App\Modules\Repositories\Ota
*/
class OtaVersionRepository extends BaseOtaVersionRepository
{
/**
* @return mixed
*/
public function getForDataTable()
{
return OtaVersion::query();
}
/**
* @param $input
* @return bool
* @throws GeneralException
*/
public function create($input)
{
$otaVersion = $this->createOtaVersionStub($input);
if ($otaVersion->save()) {
return true;
}
throw new GeneralException(trans('exceptions.backend.otaVersion.create_error'));
}
/**
* @param OtaVersion $otaVersion
* @param $input
* @return bool
* @throws GeneralException
*/
public function update(OtaVersion $otaVersion, $input)
{
Log::info("update param:" . json_encode($input));
if ($otaVersion->update($input)) {
return true;
}
throw new GeneralException(trans('exceptions.backend.otaVersion.update_error'));
}
/**
* @param $input
* @return OtaVersion
*/
private function createOtaVersionStub($input)
{
$otaVersion = new OtaVersion();
$otaVersion->version_code = $input['version_code'];
$otaVersion->update_info = $input['update_info'];
$otaVersion->filename = $input['filename'];
return $otaVersion;
}
/**
* @param OtaVersion $otaVersion
* @return mixed
*/
public function delete(OtaVersion $otaVersion)
{
$latest_version = $this->getLatest();
if ($latest_version == $otaVersion)
{
$otaVersion->delete();
$latest_version = $this->getLatest();
if ($latest_version == null)
{
Storage::disk('ota')->put('.env', '');
}
else
{
Storage::disk('ota')->put('.env', 'LATEST_VERSION='.$latest_version->version_code."\nFILENAME=".$latest_version->filename);
}
}
else
{
$otaVersion->delete();
}
}
}