Completed
Pull Request — master (#74)
by
unknown
13:28 queued 09:37
created

ModelFactory::getByClassName()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 9

Duplication

Lines 6
Ratio 40 %

Code Coverage

Tests 7
CRAP Score 2.0438

Importance

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