Exporter::execute()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
crap 1
1
<?php
2
3
namespace leandrogehlen\exporter\data;
4
5
use leandrogehlen\exporter\serializers\Serializer;
6
use Yii;
7
use yii\base\Component;
8
use yii\base\InvalidConfigException;
9
use yii\db\Connection;
10
use yii\di\Instance;
11
use yii\i18n\Formatter;
12
13
/**
14
 * Exporter provides data performing DB queries.
15
 *
16
 * @author Leandro Guindani Gehlen <[email protected]>
17
 */
18
class Exporter extends Component
19
{
20
    use CollectionTrait;
21
22
    /**
23
     * @var string|array|Serializer
24
     */
25
    public $serializer;
26
27
    /**
28
     * @var string
29
     */
30
    public $description;
31
32
    /**
33
     * @var Session[]
34
     */
35
    public $sessions = [];
36
37
    /**
38
     * @var Dictionary[]
39
     */
40
    public $dictionaries = [];
41
42
    /**
43
     * @var Event[]
44
     */
45
    public $events = [];
46
47
    /**
48
     * @var Provider[]
49
     */
50
    public $providers = [];
51
52
    /**
53
     * @var Parameter[]
54
     */
55
    public $parameters = [];
56
57
    /**
58
     * @var Connection|array|string the DB connection object or the application component ID of the DB connection
59
     */
60
    public $db = 'db';
61
62
    /**
63
     * @var array|Formatter the formatter used to format model attribute values into displayable texts.
64
     * This can be either an instance of [[Formatter]] or an configuration array for creating the [[Formatter]]
65
     * instance. If this property is not set, the "formatter" application component will be used.
66
     */
67
    public $formatter;
68
69
    /**
70
     * Initializes the exporter.
71
     * This method will initialize required property values and instantiate collection objects.
72
     */
73 10
    public function init()
74
    {
75 10
        parent::init();
76 10
        $this->db = Instance::ensure($this->db, Connection::className());
77
78 10
        $this->initSerializer();
79 9
        $this->initFormatter();
80
81 8
        $this->initElements($this->sessions, Session::className());
82 8
        $this->initElements($this->dictionaries, Dictionary::className());
83 8
        $this->initElements($this->events, Event::className());
84 8
        $this->initElements($this->providers, Provider::className());
85 8
        $this->initElements($this->parameters, Parameter::className());
86 8
    }
87
88
89
    /**
90
     * Finds Provider instance by the given name.
91
     * @param string $name the provider name
92
     * @return Provider
93
     * @throws InvalidConfigException if provider not found
94
     */
95 8
    public function findProvider($name)
96
    {
97 8
        foreach ($this->providers as $provider) {
98 8
            if ($provider->name == $name) {
99 7
                return $provider;
100
            }
101 4
        }
102 1
        throw new InvalidConfigException('The provider with name "' . $name .  '" not found');
103
    }
104
105
    /**
106
     * Finds Dictionary instance by the given name.
107
     * @param string $name the dictionary name
108
     * @return Dictionary
109
     * @throws InvalidConfigException if dictionary not found
110
     */
111 4
    public function findDictionary($name)
112
    {
113 4
        foreach ($this->dictionaries as $dictionary) {
114 4
            if ($dictionary->name == $name) {
115 3
                return $dictionary;
116
            }
117 2
        }
118 1
        throw new InvalidConfigException('The dictionary with name "' . $name .  '" not found');
119
    }
120
121
    /**
122
     * Finds Parameter instance by the given name.
123
     * @param string $name the dictionary name
124
     * @return Parameter|null
125
     */
126 1
    public function findParameter($name)
127
    {
128 1
        foreach ($this->parameters as $param) {
129 1
            if ($param->name == $name) {
130 1
                return $param;
131
            }
132 1
        }
133
        return null;
134
    }
135
136
    /**
137
     * Finds Event instance by the given name.
138
     * @param string $name the dictionary name*
139
     * @return Event|null
140
     * @throws InvalidConfigException if is invalid expression event
141
     */
142 5
    public function findEvent($name)
143
    {
144 5
        foreach ($this->events as $event) {
145 4
            if ($event->name == $name) {
146 4
                if (is_callable($event->expression)) {
147 3
                    return $event;
148
                } else {
149 1
                    throw new InvalidConfigException('The expression of event "' . $name .  '" must be callable');
150
                }
151
            }
152 1
        }
153 1
        return null;
154
    }
155
156
    /**
157
     * Performs data conversion
158
     * @return string the formatted data
159
     */
160 8
    public function execute()
161
    {
162 8
        $data = $this->serializer->serialize($this->sessions);
163 4
        return $this->serializer->formatData($data);
164
    }
165
166
    /**
167
     * Initializes the Serializer property
168
     * @throws InvalidConfigException
169
     */
170 10
    protected function initSerializer()
171
    {
172 10
        if (is_string($this->serializer)) {
173 8
            $this->serializer = Yii::createObject([
174 8
                'class' => $this->serializer,
175
                'exporter' => $this
176 8
            ]);
177 10
        } elseif (is_array($this->serializer)) {
178 1
            $this->serializer['exporter'] = $this;
179 1
            $this->serializer = Yii::createObject($this->serializer);
180 1
        }
181
182 10
        if (!$this->serializer instanceof Serializer) {
183 1
            throw new InvalidConfigException('The "serializer" property must be either a Serializer object.');
184
        }
185 9
    }
186
187
    /**
188
     * Initializes the Formatter property
189
     * @throws InvalidConfigException
190
     */
191 9
    protected function initFormatter()
192
    {
193 9
        if ($this->formatter === null) {
194 8
            $this->formatter = Yii::$app->getFormatter();
195 9
        } elseif (is_array($this->formatter)) {
196
            $this->formatter = Yii::createObject($this->formatter);
197
        }
198 9
        if (!$this->formatter instanceof Formatter) {
199 1
            throw new InvalidConfigException('The "formatter" property must be either a Format object or a configuration array.');
200
        }
201 8
    }
202
}
203