AbstractLoader::loadObject()   B
last analyzed

Complexity

Conditions 7
Paths 9

Size

Total Lines 23
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 7

Importance

Changes 0
Metric Value
cc 7
eloc 14
nc 9
nop 4
dl 0
loc 23
ccs 15
cts 15
cp 1
crap 7
rs 8.8333
c 0
b 0
f 0
1
<?php
2
3
namespace Darkilliant\ImportBundle\Loader;
4
5
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
6
7
/**
8
 * Class AbstractLoader.
9
 *
10
 * @internal
11
 */
12
abstract class AbstractLoader
13
{
14
    /** @var PropertyAccessorInterface */
15
    protected $accessor;
16
17 1
    public function __construct(PropertyAccessorInterface $accessor)
18
    {
19 1
        $this->accessor = $accessor;
20 1
    }
21
22 1
    protected function loadObject($object, array $data, array $mapping, array $mappingRelation = [])
23
    {
24 1
        foreach ($mapping as $objectPath => $arrayPath) {
25 1
            if (!is_array($arrayPath)) {
26 1
                $propertyAccesorPath = substr($arrayPath, 1);
27 1
                if (false === strpos($arrayPath, '@')) {
28 1
                    $this->accessor->setValue($object, $objectPath, $arrayPath);
29 1
                } elseif ($this->accessor->isReadable($data, $propertyAccesorPath)) {
30 1
                    $this->accessor->setValue($object, $objectPath, $this->accessor->getValue($data, $propertyAccesorPath));
31
                }
32
            }
33
34 1
            if (isset($mappingRelation[$objectPath]) && array_key_exists($objectPath, $data)) {
35 1
                $classRelation = $mappingRelation[$objectPath];
36 1
                $this->accessor->setValue(
37 1
                    $object,
38 1
                    $objectPath,
39 1
                    $this->loadObject(new $classRelation(), $data[$objectPath], $arrayPath)
40
                );
41
            }
42
        }
43
44 1
        return $object;
45
    }
46
}
47