Completed
Push — master ( 951c22...8d4d5f )
by Nate
01:59 queued 48s
created

Transformers::init()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 5
c 0
b 0
f 0
ccs 0
cts 5
cp 0
rs 9.4285
cc 1
eloc 3
nc 1
nop 0
crap 2
1
<?php
2
3
/**
4
 * @copyright  Copyright (c) Flipbox Digital Limited
5
 * @license    https://flipboxfactory.com/software/flux/license
6
 * @link       https://www.flipboxfactory.com/software/flux/
7
 */
8
9
namespace flipbox\flux\services;
10
11
use craft\helpers\ArrayHelper;
12
use craft\helpers\Json;
13
use flipbox\ember\exceptions\ObjectNotFoundException;
14
use flipbox\ember\helpers\QueryHelper;
15
use flipbox\ember\services\traits\objects\Accessor;
16
use flipbox\flux\db\TransformerQuery;
17
use flipbox\flux\events\RegisterTransformerEvent;
18
use flipbox\flux\Flux;
19
use flipbox\flux\helpers\TransformerHelper;
20
use Flipbox\Transform\Transformers\TransformerInterface;
21
use yii\base\Component;
22
use yii\base\Event;
23
use yii\db\QueryInterface;
24
25
/**
26
 * @author Flipbox Factory <[email protected]>
27
 * @since 1.0.0
28
 */
29
class Transformers extends Component
30
{
31
    use Accessor;
32
33
    /**
34
     * @event Event an event that is triggered when the query is initialized via [[init()]].
35
     */
36
    const EVENT_INIT = 'init';
37
38
    /**
39
     * Initializes the object.
40
     * This method is called at the end of the constructor. The default implementation will trigger
41
     * an [[EVENT_INIT]] event. If you override this method, make sure you call the parent implementation at the end
42
     * to ensure triggering of the event.
43
     */
44
    public function init()
45
    {
46
        parent::init();
47
        $this->trigger(self::EVENT_INIT);
48
    }
49
50
    /**
51
     * @inheritdoc
52
     */
53
    public static function objectClass()
54
    {
55
        return null;
56
    }
57
58
    /**
59
     * @inheritdoc
60
     */
61
    public function getQuery($config = []): QueryInterface
62
    {
63
        $query = new TransformerQuery();
64
65
        if (!empty($config)) {
66
            QueryHelper::configure(
67
                $query,
68
                $config
69
            );
70
        }
71
72
        return $query;
73
    }
74
75
    /**
76
     * @param array $config
77
     * @return array
78
     */
79
    protected function prepareConfig(array $config = []): array
80
    {
81
        if (null !== $settings = ArrayHelper::remove($config, 'config')) {
82
            if (is_string($settings)) {
83
                $settings = Json::decodeIfJson($settings);
84
            }
85
86
            $config = array_merge(
87
                $config,
88
                $settings
89
            );
90
        }
91
92
        return $config;
93
    }
94
95
    /**
96
     * @param $transformer
97
     * @param string $scope
98
     * @param string|null $class
99
     * @param callable|TransformerInterface|null $default
100
     * @return callable|TransformerInterface|null
101
     */
102
    public function resolve(
103
        $transformer,
104
        string $scope = Flux::GLOBAL_SCOPE,
105
        string $class = null,
106
        $default = null
107
    ) {
108
        if (null !== ($callable = TransformerHelper::resolve($transformer))) {
109
            return $callable;
110
        }
111
112
        if (is_string($transformer)) {
113
            return $this->find($transformer, $scope, $class, $default);
114
        }
115
116
        return null;
117
    }
118
119
    /**
120
     * @param string $identifier
121
     * @param string $scope
122
     * @param string|null $class
123
     * @param callable|TransformerInterface|null $default
124
     * @return callable|TransformerInterface
125
     * @throws ObjectNotFoundException
126
     */
127
    public function get(
128
        string $identifier,
129
        string $scope = Flux::GLOBAL_SCOPE,
130
        string $class = null,
131
        $default = null
132
    ) {
133
        if (null === ($transformer = $this->find($identifier, $scope, $class, $default))) {
134
            $this->notFoundException();
135
        }
136
137
        return $transformer;
138
    }
139
140
    /**
141
     * @param string $identifier
142
     * @param string $scope
143
     * @param string|null $class
144
     * @param callable|TransformerInterface|null $default
145
     * @return callable|TransformerInterface|null
146
     */
147
    public function find(
148
        string $identifier,
149
        string $scope = Flux::GLOBAL_SCOPE,
150
        string $class = null,
151
        $default = null
152
    ) {
153
        $identifierKey = is_numeric($identifier) ? 'id' : 'handle';
154
155
        $condition = [
156
            $identifierKey => $identifier,
157
            'scope' => $scope
158
        ];
159
160
        if ($class !== null) {
161
            $condition['class'] = $class;
162
        }
163
164
        $transformer = $this->findByCondition($condition) ?: $default;
165
166
        return $this->triggerEvent(
167
            $identifier,
168
            $scope,
169
            $class,
170
            $transformer
171
        );
172
    }
173
174
    /**
175
     * @param string $identifier
176
     * @param string $scope
177
     * @param string|null $class
178
     * @param callable|TransformerInterface|null $transformer
179
     * @return callable|TransformerInterface|null
180
     */
181
    private function triggerEvent(string $identifier, string $scope, string $class = null, $transformer = null)
182
    {
183
        if ($class === null) {
184
            $class = get_class($this);
185
        }
186
187
        $event = new RegisterTransformerEvent([
188
            'scope' => $scope,
189
            'transformer' => $transformer
190
        ]);
191
192
        Event::trigger(
193
            $class,
194
            $identifier,
195
            $event
196
        );
197
198
        return TransformerHelper::resolve($event->transformer);
199
    }
200
}
201