1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace LaravelFeature\Repository; |
4
|
|
|
|
5
|
|
|
use LaravelFeature\Domain\Exception\FeatureException; |
6
|
|
|
use LaravelFeature\Domain\Repository\FeatureRepositoryInterface; |
7
|
|
|
use LaravelFeature\Domain\Model\Feature; |
8
|
|
|
use LaravelFeature\Featurable\FeaturableInterface; |
9
|
|
|
use LaravelFeature\Model\Feature as Model; |
10
|
|
|
|
11
|
|
|
class EloquentFeatureRepository implements FeatureRepositoryInterface |
12
|
|
|
{ |
13
|
9 |
|
public function save(Feature $feature) |
14
|
9 |
|
{ |
15
|
|
|
/** @var Model $model */ |
16
|
6 |
|
$model = Model::where('name', '=', $feature->getName())->first(); |
17
|
|
|
|
18
|
6 |
|
if (!$model) { |
19
|
6 |
|
$model = new Model(); |
20
|
6 |
|
} |
21
|
|
|
|
22
|
6 |
|
$model->name = $feature->getName(); |
23
|
6 |
|
$model->is_enabled = $feature->isEnabled(); |
24
|
|
|
|
25
|
|
|
try { |
26
|
6 |
|
$model->save(); |
27
|
6 |
|
} catch (\Exception $e) { |
28
|
3 |
|
throw new FeatureException('Unable to save the feature: ' . $e->getMessage()); |
29
|
|
|
} |
30
|
3 |
|
} |
31
|
|
|
|
32
|
6 |
|
public function remove(Feature $feature) |
33
|
|
|
{ |
34
|
|
|
/** @var Model $model */ |
35
|
6 |
|
$model = Model::where('name', '=', $feature->getName())->first(); |
36
|
6 |
|
if (!$model) { |
37
|
3 |
|
throw new FeatureException('Unable to find the feature.'); |
38
|
|
|
} |
39
|
|
|
|
40
|
3 |
|
$model->delete(); |
41
|
3 |
|
} |
42
|
|
|
|
43
|
6 |
View Code Duplication |
public function findByName($featureName) |
|
|
|
|
44
|
|
|
{ |
45
|
|
|
/** @var Model $model */ |
46
|
6 |
|
$model = Model::where('name', '=', $featureName)->first(); |
47
|
6 |
|
if (!$model) { |
48
|
3 |
|
throw new FeatureException('Unable to find the feature.'); |
49
|
|
|
} |
50
|
|
|
|
51
|
3 |
|
return Feature::fromNameAndStatus( |
52
|
3 |
|
$model->name, |
53
|
3 |
|
$model->is_enabled |
54
|
3 |
|
); |
55
|
|
|
} |
56
|
|
|
|
57
|
9 |
View Code Duplication |
public function enableFor($featureName, FeaturableInterface $featurable) |
|
|
|
|
58
|
|
|
{ |
59
|
|
|
/** @var Model $model */ |
60
|
9 |
|
$model = Model::where('name', '=', $featureName)->first(); |
61
|
9 |
|
if (!$model) { |
62
|
3 |
|
throw new FeatureException('Unable to find the feature.'); |
63
|
|
|
} |
64
|
|
|
|
65
|
6 |
|
if ((bool) $model->is_enabled === true || $featurable->hasFeature($featureName) === true) { |
66
|
3 |
|
return; |
67
|
|
|
} |
68
|
|
|
|
69
|
3 |
|
$featurable->features()->attach($model->id); |
|
|
|
|
70
|
3 |
|
} |
71
|
|
|
|
72
|
9 |
View Code Duplication |
public function disableFor($featureName, FeaturableInterface $featurable) |
|
|
|
|
73
|
|
|
{ |
74
|
|
|
/** @var Model $model */ |
75
|
9 |
|
$model = Model::where('name', '=', $featureName)->first(); |
76
|
9 |
|
if (!$model) { |
77
|
3 |
|
throw new FeatureException('Unable to find the feature.'); |
78
|
|
|
} |
79
|
|
|
|
80
|
6 |
|
if ((bool) $model->is_enabled === true || $featurable->hasFeature($featureName) === false) { |
81
|
3 |
|
return; |
82
|
|
|
} |
83
|
|
|
|
84
|
3 |
|
$featurable->features()->detach($model->id); |
|
|
|
|
85
|
3 |
|
} |
86
|
|
|
|
87
|
6 |
View Code Duplication |
public function isEnabledFor($featureName, FeaturableInterface $featurable) |
|
|
|
|
88
|
|
|
{ |
89
|
|
|
/** @var Model $model */ |
90
|
6 |
|
$model = Model::where('name', '=', $featureName)->first(); |
91
|
6 |
|
if (!$model) { |
92
|
3 |
|
throw new FeatureException('Unable to find the feature.'); |
93
|
|
|
} |
94
|
|
|
|
95
|
3 |
|
return ($model->is_enabled) ? true : $featurable->hasFeature($featureName); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
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.