Test Failed
Push — master ( baa3df...d825b9 )
by Julien
04:52
created

ModelName::getModelNamespaces()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 7
ccs 0
cts 4
cp 0
crap 6
rs 10
1
<?php
2
3
namespace Zemit\Mvc\Controller\Rest;
4
5
use Phalcon\Mvc\ModelInterface;
6
use Phalcon\Text;
7
use Zemit\Mvc\Controller\AbstractTrait\AbstractDispatcher;
8
use Zemit\Mvc\Controller\AbstractTrait\AbstractInjectable;
9
use Zemit\Mvc\Controller\AbstractTrait\AbstractLoader;
10
use Zemit\Mvc\Controller\AbstractTrait\AbstractModelsManager;
11
use Zemit\Mvc\Controller\AbstractTrait\AbstractRouter;
12
13
trait ModelName
14
{
15
    use AbstractInjectable;
16
    use AbstractLoader;
17
    use AbstractDispatcher;
18
    use AbstractRouter;
19
    use AbstractModelsManager;
20
    
21
    protected ?string $modelName;
22
    protected ?array $modelNamespaces;
23
    
24
    /**
25
     * Get the default model class name
26
     */
27
    public function getModelName(): ?string
28
    {
29
        if (!$this->modelName) {
30
            $this->modelName = $this->getModelNameFromController();
31
        }
32
        
33
        return $this->modelName;
34
    }
35
    
36
    /**
37
     * Set the default model class name
38
     */
39
    public function setModelName(?string $modelName = null): void
40
    {
41
        $this->modelName = $modelName;
42
    }
43
    
44
    /**
45
     * Get the default model namespaces
46
     */
47
    public function getModelNamespaces(): array
48
    {
49
        if (!$this->modelNamespaces) {
50
            $this->modelNamespaces = $this->getLoader()->getNamespaces();
51
        }
52
        
53
        return $this->modelNamespaces;
54
    }
55
    
56
    /**
57
     * Set the default model namespaces
58
     */
59
    public function setModelNamespaces(?array $modelNamespaces = []): void
60
    {
61
        $this->modelNamespaces = $modelNamespaces;
62
    }
63
    
64
    /**
65
     * Try to find the appropriate model which would suit the current controller name
66
     */
67
    public function getModelNameFromController(?array $namespaces = null, ?string $needle = 'Models'): ?string
68
    {
69
        $model = ucfirst(Text::camelize(Text::uncamelize($this->getControllerName())));
70
        
71
        if (class_exists($model)) {
72
            return $model;
73
        }
74
        
75
        $namespaces ??= $this->getModelNamespaces();
76
        foreach ($namespaces as $namespace => $path) {
77
            if (strpos($namespace, $needle) !== false) {
0 ignored issues
show
Bug introduced by
It seems like $needle can also be of type null; however, parameter $needle of strpos() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

77
            if (strpos($namespace, /** @scrutinizer ignore-type */ $needle) !== false) {
Loading history...
78
                $possibleModel = $namespace . '\\' . $model;
79
                if (class_exists($possibleModel) && is_subclass_of($possibleModel, ModelInterface::class)) {
80
                    return $possibleModel;
81
                }
82
            }
83
        }
84
        
85
        return null;
86
    }
87
    
88
    /**
89
     * Return the current controller short name
90
     */
91
    public function getControllerName(): string
92
    {
93
        return $this->getDispatcher()->getControllerName()
94
            ?: substr(basename(str_replace('\\', '/', get_class($this))), 0, -10);
95
    }
96
    
97
    /**
98
     * Load the model using the modelsManager
99
     * return a new model instance
100
     */
101
    public function loadModel(?string $modelName = null): ModelInterface
102
    {
103
        $modelName ??= $this->getModelName();
104
        return $this->getModelsManager()->load($modelName);
0 ignored issues
show
Bug introduced by
It seems like $modelName can also be of type null; however, parameter $modelName of Phalcon\Mvc\Model\Manager::load() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

104
        return $this->getModelsManager()->load(/** @scrutinizer ignore-type */ $modelName);
Loading history...
105
    }
106
}
107