ModelFactory   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 112
Duplicated Lines 10.71 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 88.64%

Importance

Changes 0
Metric Value
dl 12
loc 112
ccs 39
cts 44
cp 0.8864
rs 10
c 0
b 0
f 0
wmc 10
lcom 1
cbo 5

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 16 1
B getBySlug() 6 38 6
A getByName() 0 4 1
A getByClassName() 6 14 2

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 Anavel\Crud\Abstractor\Eloquent;
4
5
use ANavallaSuiza\Laravel\Database\Contracts\Manager\ModelManager;
6
use Anavel\Crud\Abstractor\Exceptions\FactoryException;
7
use Anavel\Crud\Contracts\Abstractor\FieldFactory as FieldAbstractorFactoryContract;
8
use Anavel\Crud\Contracts\Abstractor\ModelFactory as ModelAbstractorFactoryContract;
9
use Anavel\Crud\Contracts\Abstractor\RelationFactory as RelationAbstractorFactoryContract;
10
use Anavel\Crud\Contracts\Form\Generator as FormGenerator;
11
use Anavel\Foundation\Contracts\Anavel;
12
use EasySlugger\Slugger;
13
use ReflectionClass;
14
15
class ModelFactory implements ModelAbstractorFactoryContract
16
{
17
    protected $modelManager;
18
    protected $relationFactory;
19
    protected $fieldFactory;
20
    protected $slugger;
21
22
    protected $allConfiguredModels;
23
24
    /**
25
     * @var Anavel
26
     */
27
    protected $anavel;
28
    /**
29
     * @var FormGenerator
30
     */
31
    private $generator;
32
33
    /**
34
     * ModelFactory constructor.
35
     *
36
     * @param array                             $allConfiguredModels
37
     * @param ModelManager                      $modelManager
38
     * @param RelationAbstractorFactoryContract $relationFactory
39
     * @param FieldAbstractorFactoryContract    $fieldFactory
40
     * @param FormGenerator                     $generator
41
     * @param Anavel                            $anavel
42
     */
43 6
    public function __construct(
44
        array $allConfiguredModels,
45
        ModelManager $modelManager,
46
        RelationAbstractorFactoryContract $relationFactory,
47
        FieldAbstractorFactoryContract $fieldFactory,
48
        FormGenerator $generator,
49
        Anavel $anavel
50
    ) {
51 6
        $this->allConfiguredModels = $allConfiguredModels;
52 6
        $this->modelManager = $modelManager;
53 6
        $this->relationFactory = $relationFactory;
54 6
        $this->fieldFactory = $fieldFactory;
55 6
        $this->slugger = new Slugger();
56 6
        $this->generator = $generator;
57 6
        $this->anavel = $anavel;
58 6
    }
59
60
    /**
61
     * @param $slug
62
     * @param null $id
63
     *
64
     * @throws \Exception
65
     *
66
     * @return Model|null
67
     */
68 3
    public function getBySlug($slug, $id = null)
69
    {
70 3
        $model = null;
71
72 3
        foreach ($this->allConfiguredModels as $modelName => $config) {
73 3
            $modelSlug = $this->slugger->slugify($modelName);
74
75 3
            if ($modelSlug === $slug) {
76 2
                if (is_array($config)) {
77 2
                    $modelNamespace = $config['model'];
78 2
                } else {
79
                    $modelNamespace = $config;
80
                }
81
82 2
                $model = new Model($config, $this->modelManager->getAbstractionLayer($modelNamespace),
83 2
                    $this->relationFactory, $this->fieldFactory, $this->generator,
84 2
                    !$this->anavel->hasModule('Anavel\Uploads\UploadsModuleProvider'));
85
86 2
                $model->setSlug($modelSlug)
87 2
                    ->setName($modelName);
88
89 2 View Code Duplication
                if (is_null($id)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
90 1
                    $model->setInstance($this->modelManager->getModelInstance($modelNamespace));
91 1
                } else {
92 1
                    $repository = $this->modelManager->getRepository($modelNamespace);
93 1
                    $model->setInstance($repository->findByOrFail($repository->getModel()->getKeyName(), $id));
94
                }
95
96 2
                break;
97
            }
98 3
        }
99
100 3
        if (is_null($model)) {
101 1
            throw new FactoryException('Model '.$slug.' not found on configuration');
102
        }
103
104 2
        return $model;
105
    }
106
107
    public function getByName($name, $id = null)
108
    {
109
        return $this->getBySlug($this->slugger->slugify($name));
110
    }
111
112 1
    public function getByClassName($classname, array $config, $id = null)
113
    {
114 1
        $model = new Model(array_merge(['model' => $classname], $config), $this->modelManager->getAbstractionLayer($classname), $this->relationFactory, $this->fieldFactory, $this->generator);
115 1
        $model->setSlug($this->slugger->slugify((new ReflectionClass($classname))->getShortName()));
116
117 1 View Code Duplication
        if (is_null($id)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
118 1
            $model->setInstance($this->modelManager->getModelInstance($classname));
119 1
        } else {
120
            $repository = $this->modelManager->getRepository($classname);
121
            $model->setInstance($repository->findByOrFail($repository->getModel()->getKeyName(), $id));
122
        }
123
124 1
        return $model;
125
    }
126
}
127