Test Failed
Push — master ( 06623e...343947 )
by Lukas
04:19
created

DeferredAwareTransformer::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 1
1
<?php
2
3
namespace Lneicelis\Transformer;
4
5
use Lneicelis\Transformer\Exception\TransformerNotFoundException;
6
use Lneicelis\Transformer\Helper\Arr;
7
use Lneicelis\Transformer\ValueObject\Context;
8
use Lneicelis\Transformer\ValueObject\Deferred;
9
use Lneicelis\Transformer\ValueObject\Path;
10
11
class DeferredAwareTransformer extends Transformer
12
{
13
    /** @var array[] */
14
    private $deferredTree = [];
15
16
    /** @var LoaderRegistry */
17
    private $loaderRegistry;
18
19
    public function __construct(array $pipes, LoaderRegistry $loaderRegistry)
20 2
    {
21
        parent::__construct($pipes);
22 2
23 2
        $this->loaderRegistry = $loaderRegistry;
24
    }
25
26
    /**
27
     * @param mixed $resource
28
     * @param Context|null $context
29
     * @return string|int|float|array
30
     * @throws TransformerNotFoundException
31 2
     */
32
    public function transform($resource, Context $context = null)
33 2
    {
34
        $context = $context ?: new Context();
35 2
36 2
        $result = parent::transform($resource, $context);
37
        $result = $this->loadDeferred($result, $context);
0 ignored issues
show
Bug introduced by
It seems like $result can also be of type double and integer and string; however, parameter $result of Lneicelis\Transformer\De...sformer::loadDeferred() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

37
        $result = $this->loadDeferred(/** @scrutinizer ignore-type */ $result, $context);
Loading history...
38 2
39
        return $result;
40
    }
41
42
    /**
43
     * @param $deferred
44
     * @param Context $context
45
     * @param Path $path
46
     * @return array|float|int|string
47
     * @throws TransformerNotFoundException
48
     */
49 2
    protected function transformAny($deferred, Context $context, Path $path)
50
    {
51 2
        if (! $deferred instanceof Deferred) {
52 2
            return parent::transformAny($deferred, $context, $path);
53
        }
54
55 2
        $resource = $deferred->getResource();
56 2
        $property = $deferred->getProperty();
57 2
        $resourceClass = get_class($resource);
58
59 2
        $this->deferredTree = Arr::setValue($this->deferredTree, [$resourceClass, $property, (string)$path], $deferred);
60
61
        return null;
62
    }
63
64
    /**
65
     * @param array $result
66
     * @param Context $context
67
     * @return array
68 2
     * @throws TransformerNotFoundException
69
     */
70 2
    protected function loadDeferred(array $result, Context $context): array
71 2
    {
72 2
        foreach ($this->deferredTree as $resourceClass => $properties) {
73
            foreach ($properties as $property => $deferredByPath) {
74 2
                $loader = $this->loaderRegistry->getLoader($resourceClass, $property);
75
                $paths = array_keys($deferredByPath);
76 2
                $deferreds = array_values($deferredByPath);
77
78 2
                unset($this->deferredTree[$resourceClass][$property]);
79
80 2
                $loadedValues = $loader->load($deferreds);
81
                $transformedValues = $this->transform($loadedValues, $context);
82 2
83 2
                foreach ($paths as $index => $path) {
84 2
                    /** @var Path $path */
85 2
                    $result = Arr::setValue(
86
                        $result,
87
                        explode('.', $path),
88
                        $transformedValues[$index]
89
                    );
90 2
                }
91
            }
92
        }
93 2
94
        return $result;
95 2
    }
96
}
97