Type::findAll()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 8
c 0
b 0
f 0
ccs 0
cts 7
cp 0
rs 9.4285
cc 2
eloc 4
nc 2
nop 0
crap 6
1
<?php
2
3
/**
4
 * @copyright  Copyright (c) Flipbox Digital Limited
5
 * @license    https://flipboxfactory.com/software/link/license
6
 * @link       https://www.flipboxfactory.com/software/link/
7
 */
8
9
namespace flipbox\link\services;
10
11
use craft\helpers\ArrayHelper;
12
use flipbox\link\events\RegisterLinkTypes;
13
use flipbox\link\types\Asset as AssetType;
14
use flipbox\link\types\Category as CategoryType;
15
use flipbox\link\types\Entry as EntryType;
16
use flipbox\link\types\TypeInterface;
17
use flipbox\link\types\Url as UrlType;
18
use yii\base\Component;
19
20
/**
21
 * @author Flipbox Factory <[email protected]>
22
 * @since 1.0.0
23
 */
24
class Type extends Component
25
{
26
27
    /**
28
     * The event name
29
     */
30
    const EVENT_REGISTER_TYPES = 'registerTypes';
31
32
    /**
33
     * The property to uniquely identify a link type
34
     */
35
    const IDENTIFIER = 'identifier';
36
37
    /**
38
     * @var array
39
     */
40
    private $types;
41
42
    /**
43
     * @return array
44
     */
45
    public function findAll()
46
    {
47
        if ($this->types === null) {
48
            $this->types = $this->registerTypes();
49
        }
50
51
        return $this->types;
52
    }
53
54
    /**
55
     * @param string $class
56
     * @return TypeInterface|null
57
     */
58
    public function find(string $class)
59
    {
60
        return ArrayHelper::getValue(
61
            $this->findAll(),
62
            $class
63
        );
64
    }
65
66
    /**
67
     * @return array
68
     */
69
    protected function registerTypes()
70
    {
71
        $event = new RegisterLinkTypes([
72
            'types' => $this->firstParty()
73
        ]);
74
75
        $this->trigger(
76
            self::EVENT_REGISTER_TYPES,
77
            $event
78
        );
79
80
        return $this->resolveTypes($event->types);
81
    }
82
83
    /**
84
     * Populate valid properties.  This occurs when we have a content value
85
     * and we need to populate it's contents on an existing TypeInterface
86
     *
87
     * @param TypeInterface $type
88
     * @param array $properties
89
     */
90
    public function populate(TypeInterface $type, array $properties)
91
    {
92
        foreach ($type->getProperties() as $key => $value) {
93
            if (array_key_exists($key, $properties)) {
94
                $type->{$key} = $properties[$key];
95
            }
96
        }
97
    }
98
99
    /**
100
     * @param array $types
101
     * @return array
102
     */
103
    private function resolveTypes(array $types)
104
    {
105
        $validTypes = [];
106
        foreach ($types as $type) {
107
            if (!$type instanceof TypeInterface) {
108
                $type = new $type();
109
            }
110
            $validTypes[get_class($type)] = $type;
111
        }
112
113
        return $validTypes;
114
    }
115
116
    /**
117
     * @param $type
118
     * @return array|null|object
119
     */
120
    public function create($type)
121
    {
122
        if ($type instanceof TypeInterface) {
123
            return $type;
124
        }
125
126
        if (!is_array($type)) {
127
            $type = ['class' => $type];
128
        }
129
130
        $type = \Yii::createObject(
131
            $type
132
        );
133
134
        if (!$type instanceof TypeInterface) {
135
            return null;
136
        }
137
138
        return $type;
139
    }
140
141
    /**
142
     * @return array
143
     */
144
    private function firstParty()
145
    {
146
        return [
147
            AssetType::class,
148
            CategoryType::class,
149
            EntryType::class,
150
            UrlType::class
151
        ];
152
    }
153
}
154