EloquentFeatureRepository   A
last analyzed

Complexity

Total Complexity 18

Size/Duplication

Total Lines 87
Duplicated Lines 58.62 %

Coupling/Cohesion

Components 0
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 18
lcom 0
cbo 4
dl 51
loc 87
c 0
b 0
f 0
ccs 47
cts 47
cp 1
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A save() 0 18 3
A remove() 0 10 2
A findByName() 13 13 2
A enableFor() 14 14 4
A disableFor() 14 14 4
A isEnabledFor() 10 10 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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);
0 ignored issues
show
Bug introduced by
The method features() does not seem to exist on object<LaravelFeature\Fe...le\FeaturableInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
70 3
    }
71
72 9 View Code Duplication
    public function disableFor($featureName, FeaturableInterface $featurable)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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);
0 ignored issues
show
Bug introduced by
The method features() does not seem to exist on object<LaravelFeature\Fe...le\FeaturableInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
85 3
    }
86
87 6 View Code Duplication
    public function isEnabledFor($featureName, FeaturableInterface $featurable)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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