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

AbstractResourceTransformer   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 3
dl 0
loc 66
ccs 0
cts 27
cp 0
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
createResource() 0 1 ?
A getData() 0 4 1
A setTransformer() 0 5 1
A setData() 0 5 1
A __invoke() 0 15 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