Completed
Push — master ( 980a0d...4ce845 )
by Cheren
09:46
created

Manager   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 107
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 8
dl 0
loc 107
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
B create() 0 41 6
A _findElement() 0 19 4
A _getClassName() 0 4 1
A _normalize() 0 4 1
1
<?php
2
/**
3
 * CakeCMS Core
4
 *
5
 * This file is part of the of the simple cms based on CakePHP 3.
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 *
9
 * @package   Core
10
 * @license   MIT
11
 * @copyright MIT License http://www.opensource.org/licenses/mit-license.php
12
 * @link      https://github.com/CakeCMS/Core".
13
 * @author    Sergey Kalistratov <[email protected]>
14
 */
15
16
namespace Core\Cck\Element;
17
18
use JBZoo\Utils\FS;
19
use JBZoo\Utils\Str;
20
use Cake\Utility\Hash;
21
use Cake\Utility\Text;
22
use Cake\Core\Configure;
23
use Cake\Core\ClassLoader;
24
use Core\ORM\Entity\Element as EntityElement;
25
use Core\Cck\Element\Exception\ElementException;
26
27
/**
28
 * Class Manager
29
 *
30
 * @package Core\Element
31
 */
32
class Manager
33
{
34
35
    const DEFAULT_GROUP = 'Item';
36
37
    /**
38
     * Create element instance.
39
     *
40
     * @param string $type
41
     * @param string $group
42
     * @param array $config
43
     * @param EntityElement|null $entity
44
     * @return Element
45
     * @throws ElementException|\JBZoo\Utils\Exception
46
     */
47
    public function create($type, $group = self::DEFAULT_GROUP, array $config = [], EntityElement $entity = null)
48
    {
49
        $type = $this->_normalize($type);
50
        if (!$type) {
51
            throw new ElementException(__d('core_dev', 'Element type "{0}" is empty', $type));
52
        }
53
54
        $group = $this->_normalize($group);
55
        if (!$group) {
56
            throw new ElementException(__d('core_dev', 'Element group "{0}" is empty', $group));
57
        }
58
59
        $this->_findElement($group, $type);
60
        $className = $this->_getClassName($group, $type);
61
62
        if (!class_exists($className)) {
63
            throw new ElementException(__d('core_dev', 'Element class "{0}" not found!', $className));
64
        }
65
66
        /** @var Element $element */
67
        $element = new $className($type, $group);
68
69
        $config = Hash::merge($config, [
70
            'id'          => $element->isCore() ? '_' . Str::low($type) : Text::uuid(),
71
            'name'        => $element->getName(),
72
            'type'        => $type,
73
            'group'       => $group,
74
            'description' => '',
75
        ]);
76
77
        $element->id = $config['id'];
78
        $element->setConfig($config);
79
80
        if ($entity !== null) {
81
            $element->setEntity($entity);
82
        }
83
84
        $element->initialize();
85
86
        return $element;
87
    }
88
89
    /**
90
     * Find and load element.
91
     *
92
     * @param string $group
93
     * @param string $type
94
     * @return bool|string
95
     */
96
    protected function _findElement($group, $type)
97
    {
98
        $className = $this->_getClassName($group, $type);
99
100
        if (!class_exists($className)) {
101
            $loader = new ClassLoader();
102
            $paths  = Configure::read('App.paths.elements');
103
            foreach ($paths as $path) {
104
                $path = FS::clean($path . '/' . $group . '/' . $type, '/');
105
                $loader->addNamespace("\\{$group}\\{$type}", $path);
106
                $result = $loader->loadClass("{$group}\\{$type}\\{$type}Element");
107
                if ($result !== false) {
108
                    return $result;
109
                }
110
            }
111
        }
112
113
        return false;
114
    }
115
116
    /**
117
     * Get current class name.
118
     *
119
     * @param string $group
120
     * @param string $type
121
     * @return string
122
     */
123
    protected function _getClassName($group, $type)
124
    {
125
        return "\\Elements\\{$group}\\{$type}Element";
126
    }
127
128
    /**
129
     * Normalize string format.
130
     *
131
     * @param string $string
132
     * @return string
133
     */
134
    protected function _normalize($string)
135
    {
136
        return ucfirst(Str::low($string));
137
    }
138
}
139