|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* This file is part of the Laravel Platfourm package. |
|
4
|
|
|
* |
|
5
|
|
|
* (c) Avtandil Kikabidze aka LONGMAN <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace Longman\Platfourm\Text\Repositories\Eloquent; |
|
12
|
|
|
|
|
13
|
|
|
use Longman\Platfourm\Contracts\Repository\Repository; |
|
14
|
|
|
use Longman\Platfourm\Contracts\Repository\RepositoryCriteria; |
|
15
|
|
|
use Longman\Platfourm\Repository\Eloquent\BaseRepository; |
|
16
|
|
|
use Longman\Platfourm\Repository\Events\RepositoryEntityUpdated; |
|
17
|
|
|
use Longman\Platfourm\Text\Models\Eloquent\Text; |
|
18
|
|
|
|
|
19
|
|
View Code Duplication |
class TextRepository extends BaseRepository implements Repository, RepositoryCriteria |
|
|
|
|
|
|
20
|
|
|
{ |
|
21
|
|
|
|
|
22
|
|
|
public function model() |
|
23
|
|
|
{ |
|
24
|
|
|
if (class_exists(\App\Models\Text::class)) { |
|
25
|
|
|
return \App\Models\Text::class; |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
return Text::class; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Update a entity in repository by id. |
|
33
|
|
|
* |
|
34
|
|
|
* @param array $attributes |
|
|
|
|
|
|
35
|
|
|
* @param $id |
|
36
|
|
|
* @return mixed |
|
37
|
|
|
*/ |
|
38
|
|
|
public function updateValue(array $options, $value) |
|
39
|
|
|
{ |
|
40
|
|
|
$this->applyScope(); |
|
41
|
|
|
$_skipPresenter = $this->skipPresenter; |
|
42
|
|
|
|
|
43
|
|
|
$this->skipPresenter(true); |
|
44
|
|
|
|
|
45
|
|
|
$model = $this->model->where($options)->first(); |
|
46
|
|
|
|
|
47
|
|
|
$model->setAttribute('value', $value); |
|
48
|
|
|
|
|
49
|
|
|
$model->saveOrFail(); |
|
50
|
|
|
|
|
51
|
|
|
$this->skipPresenter($_skipPresenter); |
|
52
|
|
|
$this->resetModel(); |
|
53
|
|
|
|
|
54
|
|
|
event(new RepositoryEntityUpdated($this, $model)); |
|
55
|
|
|
|
|
56
|
|
|
return $this->parserResult($model); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
public function autosave($locales, $lang, $scope, array $data) |
|
|
|
|
|
|
60
|
|
|
{ |
|
61
|
|
|
$ins = []; |
|
62
|
|
|
$ins['scope'] = $scope; |
|
63
|
|
|
foreach ($data as $key) { |
|
64
|
|
|
foreach ($locales as $locale => $locale_data) { |
|
65
|
|
|
$model = $this->model->newInstance(); |
|
66
|
|
|
$exists = $model->where(['key' => $key, 'lang' => $locale])->exists(); |
|
67
|
|
|
if ($exists) { |
|
68
|
|
|
continue; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
$ins['key'] = $key; |
|
72
|
|
|
$ins['lang'] = $locale; |
|
73
|
|
|
$ins['value'] = $key; |
|
74
|
|
|
$model->fill($ins); |
|
75
|
|
|
$model->save(); |
|
76
|
|
|
$this->resetModel(); |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
return true; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
} |
|
83
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.