generatePropertyDirectory()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
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
        $name = $this->getName();
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
        /** @scrutinizer ignore-call */ 
52
        $name = $this->getName();
Loading history...
52
        if ($name) {
53
            $methodName = 'get' . $name . 'ItemsDirectory';
54
            if (method_exists($this->manager, $methodName)) {
55
                return $this->manager->$methodName();
56
            }
57
58
            $methodName = 'get' . Str::plural($name) . 'Directory';
59
            if (method_exists($this->manager, $methodName)) {
60
                return $this->manager->$methodName();
61
            }
62
        }
63
64
        return $this->generateManagerDirectory() . DIRECTORY_SEPARATOR . $this->generatePropertyDirectory();
65
    }
66
67
    /**
68
     * @return string
69
     */
70
    protected function generateManagerDirectory()
71
    {
72
        $reflector = new \ReflectionObject($this->manager);
73
74
        return dirname($reflector->getFileName());
75
    }
76
77
    /**
78
     * @return string
79
     */
80
    protected function generatePropertyDirectory()
81
    {
82
        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

82
        return $this->/** @scrutinizer ignore-call */ getLabel();
Loading history...
83
    }
84
85
    /**
86
     * @return string
87
     */
88
    protected function getPropertyItemsRootNamespace(): string
89
    {
90
        $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

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