Completed
Push — master ( 2f0398...5c44ed )
by
unknown
13s
created

EntityFileExtractorTrait::processObject()   B

Complexity

Conditions 10
Paths 6

Size

Total Lines 31
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 10.1371

Importance

Changes 0
Metric Value
cc 10
eloc 17
nc 6
nop 2
dl 0
loc 31
ccs 16
cts 18
cp 0.8889
crap 10.1371
rs 7.6666
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Smart\EtlBundle\Extractor;
4
5
use Smart\EtlBundle\Exception\Extractor\EntityAlreadyRegisteredException;
6
use Smart\EtlBundle\Exception\Extractor\EntityIdentifiedNotFoundException;
7
use Smart\EtlBundle\Exception\Extractor\EntityIdentifierAlreadyProcessedException;
8
use Smart\EtlBundle\Exception\Extractor\EntityTypeNotHandledException;
9
use Symfony\Component\PropertyAccess\PropertyAccess;
10
use Symfony\Component\PropertyAccess\PropertyAccessor;
11
12
/**
13
 * Nicolas Bastien <[email protected]>
14
 */
15
trait EntityFileExtractorTrait
16
{
17
    /**
18
     * @var string
19
     */
20
    protected $folderToExtract;
21
22
    /**
23
     * List of entities to extract
24
     * [
25
     *      'entity_code' => 'Model Classname'
26
     * ]
27
     * @var array
28
     */
29
    protected $entitiesToProcess = [];
30
31
    /**
32
     * @var array
33
     */
34
    protected $entities = [];
35
36
    /**
37
     * @var PropertyAccessor
38
     */
39
    protected $accessor;
40
41
    /**
42
     * @param string $entityType
43
     * @param string$entityClass
44
     * @param function $identifierCallback
0 ignored issues
show
Bug introduced by
The type Smart\EtlBundle\Extractor\function was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
45
     * @return $this
46
     * @throws \Exception
47
     */
48 9
    public function addEntityToProcess($entityType, $entityClass, $identifierCallback)
49
    {
50 9
        if (isset($this->entitiesToProcess[$entityType])) {
51 2
            throw new EntityAlreadyRegisteredException($entityType);
52
        }
53
54 9
        $this->entitiesToProcess[$entityType] = [
55 9
            'type' => $entityType,
56 9
            'class' => $entityClass,
57 9
            'callback' => $identifierCallback
58
        ];
59
60 9
        return $this;
61
    }
62
63
    /**
64
     * @inheritDoc
65
     */
66 7
    public function extract()
67
    {
68
        try {
69 7
            $this->init();
70 7
            $this->check();
71
72 7
            $files = $this->getFiles($this->getFileExtension());
73 7
            foreach ($this->entitiesToProcess as $entityType => $data) {
74 7
                if (!isset($files[$entityType])) {
75
                    continue;
76
                }
77 7
                $this->processFile($entityType);
78
            }
79
80 3
            return $this->entities;
81 4
        } catch (\Exception $e) {
82 4
            throw $e;
83
        }
84
    }
85
86
    /**
87
     * @see AbstractFolderExtrator::getFiles()
88
     */
89
    abstract protected function getFiles($extension);
90
91
    /**
92
     * @var AbstractFolderExtrator::getFileExtension()
93
     */
94
    abstract protected function getFileExtension();
95
96
    /**
97
     * @var AbstractFolderExtrator::extractFileContent()
98
     */
99
    abstract protected function extractFileContent($filepath);
100
101
    /**
102
     * @see AbstractExtractor::transformData()
103
     */
104
    abstract protected function transformData(array $data);
105
106 7
    protected function check()
107
    {
108 7
        parent::check();
109
110 7
        if (count($this->entitiesToProcess) === 0) {
111
            throw new \BadMethodCallException('Nothing to process');
112
        }
113 7
    }
114
115 7
    protected function init()
116
    {
117 7
        $this->accessor = PropertyAccess::createPropertyAccessor();
118 7
    }
119
120
    /**
121
     * @param string $entityType
122
     * @param array $data
123
     * @return mixed
124
     * @throws \Exception
125
     */
126 7
    protected function processObject($entityType, array $data)
127
    {
128 7
        if (!isset($this->entitiesToProcess[$entityType])) {
129
            throw new EntityTypeNotHandledException($entityType);
130
        }
131
132 7
        $data = $this->transformData($data);
133 7
        if ($data === null) {
134
            return null;
135
        }
136
137 7
        $objectClass = $this->entitiesToProcess[$entityType]['class'];
138 7
        $object = new $objectClass();
139
140 7
        foreach ($data as $key => $value) {
141 7
            $valueToSet = $value;
142 7
            if (is_string($value) && strpos($value, '@') === 0) {
143
                //handle relations
144 4
                $valueToSet = $this->getEntity($value);
145 7
            } elseif (is_array($value)) {
146 2
                foreach ($valueToSet as $k => $v) {
147 2
                    if (is_string($v) && strpos($v, '@') === 0) {
148 2
                        $valueToSet[$k] = $this->getEntity($v);
149
                    }
150
                }
151
            }
152
153 7
            $this->accessor->setValue($object, $key, $valueToSet);
154
        }
155
156 5
        return $object;
157
    }
158
159
    /**
160
     * @param string $identifier
161
     * @return mixed
162
     * @throws \Exception
163
     */
164 4
    protected function getEntity($identifier)
165
    {
166 4
        if (strpos($identifier, '@') === 0) {
167 4
            $identifier = substr($identifier, 1);
168
        }
169 4
        if (!isset($this->entities[$identifier])) {
170 2
            throw new EntityIdentifiedNotFoundException($identifier);
171
        }
172
173 2
        return $this->entities[$identifier];
174
    }
175
176
    /**
177
     * @param  string $filename
178
     * @throws \Exception
179
     */
180 7
    protected function processFile($filename)
181
    {
182 7
        $filepath = sprintf('%s/%s.' . $this->getFileExtension(), $this->folderToExtract, $filename);
183
184 7
        $data = $this->extractFileContent($filepath);
185 7
        foreach ($data as $values) {
186 7
            if ($values === null) {
187
                continue;
188
            }
189 7
            $object = $this->processObject($filename, $values);
190 5
            if ($object !== null) {
191 5
                $entityIdentifier = $this->entitiesToProcess[$filename]['callback']($object);
192 5
                if (isset($this->entities[$entityIdentifier])) {
193 2
                    throw new EntityIdentifierAlreadyProcessedException($entityIdentifier);
194
                }
195 5
                $this->entities[$entityIdentifier] = $object;
196
            }
197
        }
198 3
    }
199
}
200