Completed
Push — develop ( 2854ec...8c6a12 )
by Nate
01:51
created

AbstractResourceTransformer::transform()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 15
ccs 0
cts 13
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 8
nc 1
nop 2
crap 2

1 Method

Rating   Name   Duplication   Size   Complexity  
A AbstractResourceTransformer::setData() 0 5 1
1
<?php
2
3
/**
4
 * @author    Flipbox Factory
5
 * @copyright Copyright (c) 2017, Flipbox Digital
6
 * @link      https://github.com/flipbox/transform/releases/latest
7
 * @license   https://github.com/flipbox/transform/blob/master/LICENSE
8
 */
9
10
namespace Flipbox\Transform\Transformers;
11
12
use Flipbox\Transform\Resources\ResourceInterface;
13
use Flipbox\Transform\Scope;
14
15
/**
16
 * @author Flipbox Factory <[email protected]>
17
 * @since 1.0.0
18
 */
19
abstract class AbstractResourceTransformer extends AbstractExplicitTransformer implements ResourceTransformerInterface
20
{
21
    /**
22
     * @var mixed
23
     */
24
    protected $data;
25
26
    /**
27
     * @var callable|TransformerInterface
28
     */
29
    protected $transformer;
30
31
    /**
32
     * @param Scope $scope
33
     * @return ResourceInterface
34
     */
35
    abstract protected function createResource(Scope $scope): ResourceInterface;
36
37
    /**
38
     * @param Scope $scope
39
     * @return mixed
40
     */
41
    protected function getData(Scope $scope)
42
    {
43
        return $this->data;
44
    }
45
46
    /**
47
     * @param callable $transformer
48
     * @return $this
49
     */
50
    public function setTransformer(callable $transformer)
51
    {
52
        $this->transformer = $transformer;
53
        return $this;
54
    }
55
56
    /**
57
     * @param mixed $data
58
     * @return $this
59
     */
60
    public function setData($data)
61
    {
62
        $this->data = $data;
63
        return $this;
64
    }
65
66
    /**
67
     * @inheritdoc
68
     */
69
    public function __invoke($data, Scope $scope, string $identifier = null)
70
    {
71
        $childScope = $scope->childScope($identifier);
72
73
        $resource = $this->createResource(
74
            $childScope
75
        );
76
77
        return $resource->transform(
78
            $this->transformer,
79
            $this->getData(
80
                $childScope
81
            )
82
        );
83
    }
84
}
85