Passed
Push — master ( 81d3f9...4ab6f6 )
by Gabriel
11:06
created

HasPropertiesNamespaces   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 14
eloc 32
c 1
b 0
f 0
dl 0
loc 88
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A initPropertiesNamespaces() 0 3 1
A generatePropertyDirectory() 0 3 1
A getPropertyItemsRootNamespace() 0 14 3
A generateItemsDirectory() 0 13 3
A generateManagerDirectory() 0 5 1
A getPropertiesNamespaces() 0 7 2
A generatePropertiesNamespaces() 0 12 3
1
<?php
2
3
namespace ByTIC\Models\SmartProperties\Definitions\Definition;
4
5
use Nip\Utility\Str;
6
7
/**
8
 * Trait HasPropertiesNamespaces
9
 * @package ByTIC\Models\SmartProperties\Definitions\Definition
10
 */
11
trait HasPropertiesNamespaces
12
{
13
    protected $propertiesNamespaces = null;
14
15
    /**
16
     * @return null
17
     */
18
    public function getPropertiesNamespaces()
19
    {
20
        if ($this->propertiesNamespaces === null) {
21
            $this->initPropertiesNamespaces();
22
        }
23
24
        return $this->propertiesNamespaces;
25
    }
26
27
    protected function initPropertiesNamespaces()
28
    {
29
        $this->propertiesNamespaces = $this->generatePropertiesNamespaces();
30
    }
31
32
    protected function generatePropertiesNamespaces()
33
    {
34
        $directories = (array)$this->generateItemsDirectory();
35
        $rootNamespace = $this->getPropertyItemsRootNamespace();
36
37
        $propertiesNamespaces = [];
38
        foreach ($directories as $namespace => $directory) {
39
            $namespace = Str::contains($namespace, '\\') ? $namespace : $rootNamespace;
40
            $namespace = trim($namespace, '\\');
41
            $propertiesNamespaces[$namespace.'\\'] = $directory;
42
        }
43
        return $propertiesNamespaces;
44
    }
45
46
    /**
47
     * @return string
48
     */
49
    protected function generateItemsDirectory()
50
    {
51
        $methodName = 'get' . $this->getName() . 'ItemsDirectory';
0 ignored issues
show
Bug introduced by
It seems like getName() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

51
        $methodName = 'get' . $this->/** @scrutinizer ignore-call */ getName() . 'ItemsDirectory';
Loading history...
52
        if (method_exists($this->manager, $methodName)) {
53
            return $this->manager->$methodName();
54
        }
55
56
        $methodName = 'get' . Str::plural($this->getName()) . 'Directory';
57
        if (method_exists($this->manager, $methodName)) {
58
            return $this->manager->$methodName();
59
        }
60
61
        return $this->generateManagerDirectory() . DIRECTORY_SEPARATOR . $this->generatePropertyDirectory();
62
    }
63
64
    /**
65
     * @return string
66
     */
67
    protected function generateManagerDirectory()
68
    {
69
        $reflector = new \ReflectionObject($this->manager);
70
71
        return dirname($reflector->getFileName());
72
    }
73
74
    /**
75
     * @return string
76
     */
77
    protected function generatePropertyDirectory()
78
    {
79
        return $this->getLabel();
0 ignored issues
show
Bug introduced by
It seems like getLabel() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

79
        return $this->/** @scrutinizer ignore-call */ getLabel();
Loading history...
80
    }
81
82
    /**
83
     * @return string
84
     */
85
    protected function getPropertyItemsRootNamespace(): string
86
    {
87
        $manager = $this->getManager();
0 ignored issues
show
Bug introduced by
It seems like getManager() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

87
        /** @scrutinizer ignore-call */ 
88
        $manager = $this->getManager();
Loading history...
88
        $method = 'get' . $this->getName() . 'ItemsRootNamespace';
89
        if (method_exists($manager, $method)) {
90
            return $manager->{$method}();
91
        }
92
93
        $method = 'get' . $this->getName() . 'Namespace';
94
        if (method_exists($manager, $method)) {
95
            return $manager->{$method}();
96
        }
97
98
        return $manager->getModelNamespace() . $this->getLabel() . '\\';
99
    }
100
}
101