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

FolderBuilders::generateManagerDirectory()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
c 0
b 0
f 0
dl 0
loc 5
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
namespace ByTIC\Models\SmartProperties\Definitions\Builders;
4
5
use ByTIC\Models\SmartProperties\Definitions\Definition;
6
use ByTIC\Models\SmartProperties\Properties\PropertiesFactory;
7
use Nip\Records\AbstractModels\RecordManager;
8
use Nip\Utility\Str;
9
use RecursiveDirectoryIterator;
10
use RecursiveIteratorIterator;
11
12
/**
13
 * Class FolderBuilders
14
 * @package ByTIC\Models\SmartProperties\Definitions\Builders
15
 */
16
class FolderBuilders
17
{
18
    /**
19
     * @var RecordManager
20
     */
21
    protected $manager;
22
23
    /**
24
     * @var Definition
25
     */
26
    protected $definition;
27
28
    /**
29
     * @param RecordManager $manager
30
     * @param string $field
31
     * @param null|string $name
32
     */
33
    public static function create($manager, $field, $name = null): Definition
34
    {
35
        $builder = new static();
36
        $builder->manager = $manager;
37
        $builder->definition = $builder->newDefinition($manager);
38
        $builder->definition->setField($field);
39
        if ($name) {
40
            $builder->definition->setName($name);
41
        }
42
        $builder->definition->setBuild(function () use ($builder) {
43
            $items = $builder->buildItems();
44
            foreach ($items as $item) {
45
                $builder->definition->addItem($item);
46
            }
47
        });
48
        return $builder->definition;
49
    }
50
51
52
    /**
53
     * @param $manager
54
     * @return Definition
55
     */
56
    protected function newDefinition($manager): Definition
57
    {
58
        $definition = new Definition();
59
        $definition->setManager($manager);
60
61
        return $definition;
62
    }
63
64
    /**
65
     * @return array
66
     */
67
    protected function buildItems()
68
    {
69
//        $names = $this->getItemsNamesFromManager();
70
71
        $items = $this->getItemsFromFiles();
72
73
        return $items;
74
    }
75
76
77
    /**
78
     * @return array
79
     */
80
    protected function getItemsFromFiles(): array
81
    {
82
        $directories = $this->definition->getPropertiesNamespaces();
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $directories is correct as $this->definition->getPropertiesNamespaces() targeting ByTIC\Models\SmartProper...tPropertiesNamespaces() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
83
        $items = [];
84
        foreach ($directories as $namespace => $directory) {
0 ignored issues
show
Bug introduced by
The expression $directories of type null is not traversable.
Loading history...
85
            $items = array_merge($items, $this->getItemsFromDirectory($directory, $namespace));
86
        }
87
        return $items;
88
    }
89
90
    protected function getItemsFromDirectory($directory, $namespace): array
91
    {
92
        $files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($directory));
93
        $names = [];
94
        foreach ($files as $file) {
95
            if ($file->isDir()) {
96
                continue;
97
            }
98
            $name = str_replace($directory, '', $file->getPathname());
99
            $name = str_replace('.php', '', $name);
100
            $name = trim($name, DIRECTORY_SEPARATOR . '\\');
101
            if ($this->isAbstractItemName($name)) {
102
                continue;
103
            }
104
            $class = $namespace . '' . $name;
105
            $names[$name] = PropertiesFactory::forDefinition($this->definition, $class, $namespace);
106
        }
107
108
        return array_unique($names);
109
    }
110
111
112
    /**
113
     * @return array|boolean
114
     */
115
    protected function getItemsNamesFromManager()
116
    {
117
        $methodName = 'get' . $this->definition->getName() . 'Names';
118
        if (method_exists($this->manager, $methodName)) {
119
            return $this->manager->$methodName();
120
        }
121
122
        return false;
123
    }
124
125
    /**
126
     * @param string $name
127
     *
128
     * @return bool
129
     */
130
    protected function isAbstractItemName(string $name): bool
131
    {
132
        if (in_array($name, ['Abstract', 'Generic'])) {
133
            return true;
134
        }
135
        if (strpos($name, 'Abstract') === 0) {
136
            return true;
137
        }
138
        if (Str::endsWith($name, 'Trait')) {
139
            return true;
140
        }
141
        if (strpos($name, '\Abstract') !== false) {
142
            return true;
143
        }
144
        if (strpos($name, DIRECTORY_SEPARATOR . 'Abstract') !== false) {
145
            return true;
146
        }
147
148
        return false;
149
    }
150
151
152
}
153