Completed
Push — master ( 9a1097...9d1cce )
by Nate
04:06
created

AbstractResourceTransformer   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 3
dl 0
loc 76
ccs 0
cts 31
cp 0
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
createResource() 0 1 ?
A getData() 0 4 1
A transform() 0 15 1
A setTransformer() 0 5 1
A setData() 0 5 1
A __invoke() 0 4 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 AbstractTransformer 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)
0 ignored issues
show
Unused Code introduced by
The parameter $scope is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
42
    {
43
        return $this->data;
44
    }
45
46
    /**
47
     * @param Scope $scope
48
     * @param string|null $identifier
49
     * @return mixed
50
     */
51
    public function transform(Scope $scope, string $identifier = null)
52
    {
53
        $childScope = $scope->childScope($identifier);
54
55
        $resource = $this->createResource(
56
            $childScope
57
        );
58
59
        return $resource->transform(
60
            $this->transformer,
61
            $this->getData(
62
                $childScope
63
            )
64
        );
65
    }
66
67
    /**
68
     * @param callable $transformer
69
     * @return $this
70
     */
71
    public function setTransformer(callable $transformer)
72
    {
73
        $this->transformer = $transformer;
74
        return $this;
75
    }
76
77
    /**
78
     * @param mixed $data
79
     * @return $this
80
     */
81
    public function setData($data)
82
    {
83
        $this->data = $data;
84
        return $this;
85
    }
86
87
    /**
88
     * @inheritdoc
89
     */
90
    public function __invoke($data, Scope $scope, string $identifier = null)
91
    {
92
        return $this->transform($scope, $identifier);
93
    }
94
}
95