Completed
Push — develop ( 951c22...71e040 )
by Nate
03:40
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
     * @return callable|TransformerInterface|null
100
     */
101
    public function resolve(
102
        $transformer,
103
        string $scope = Flux::GLOBAL_SCOPE,
104
        string $class = null
105
    ) {
106
        if (null !== ($callable = TransformerHelper::resolve($transformer))) {
107
            return $callable;
108
        }
109
110
        if (is_string($transformer)) {
111
            return $this->find($transformer, $scope, $class);
112
        }
113
114
        return null;
115
    }
116
117
    /**
118
     * @param string $identifier
119
     * @param string $scope
120
     * @param string|null $class
121
     * @return callable|TransformerInterface
122
     * @throws ObjectNotFoundException
123
     */
124
    public function get(
125
        string $identifier,
126
        string $scope = Flux::GLOBAL_SCOPE,
127
        string $class = null
128
    ) {
129
        if (null === ($transformer = $this->find($identifier, $scope, $class))) {
130
            $this->notFoundException();
131
        }
132
133
        return $transformer;
134
    }
135
136
    /**
137
     * @param string $identifier
138
     * @param string $scope
139
     * @param string|null $class
140
     * @return callable|TransformerInterface|null
141
     */
142
    public function find(
143
        string $identifier,
144
        string $scope = Flux::GLOBAL_SCOPE,
145
        string $class = null
146
    ) {
147
        $identifierKey = is_numeric($identifier) ? 'id' : 'handle';
148
149
        $condition = [
150
            $identifierKey => $identifier,
151
            'scope' => $scope
152
        ];
153
154
        if($class !== null) {
155
            $condition['class'] = $class;
156
        }
157
158
        return $this->triggerEvent(
159
            $identifier,
160
            $scope,
161
            $class,
162
            $this->findByCondition($condition)
163
        );
164
    }
165
166
    /**
167
     * @param string $identifier
168
     * @param string $scope
169
     * @param string|null $class
170
     * @param callable|TransformerInterface|null $transformer
171
     * @return callable|TransformerInterface|null
172
     */
173
    private function triggerEvent(string $identifier, string $scope, string $class = null, $transformer = null)
174
    {
175
        if($class === null) {
176
            $class = get_class($this);
177
        }
178
179
        $event = new RegisterTransformerEvent([
180
            'scope' => $scope,
181
            'transformer' => $transformer
182
        ]);
183
184
        Event::trigger(
185
            $class,
186
            $identifier,
187
            $event
188
        );
189
190
        return $event->transformer;
191
    }
192
}
193