AbstractLoader   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 17
dl 0
loc 33
ccs 18
cts 18
cp 1
rs 10
c 0
b 0
f 0
wmc 8

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
B loadObject() 0 23 7
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