FractalService   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 103
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 103
c 0
b 0
f 0
wmc 11
lcom 1
cbo 2
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A item() 0 4 1
A collection() 0 4 1
A parseIncludes() 0 8 2
A setDefaultSerializer() 0 4 1
A makeBuilder() 0 19 3
A makeFractal() 0 14 3
1
<?php
2
3
namespace Nord\Lumen\Fractal;
4
5
use Nord\Lumen\Fractal\Contracts\FractalBuilder as FractalBuilderContract;
6
use Nord\Lumen\Fractal\Contracts\FractalService as FractalServiceContract;
7
use League\Fractal\Manager;
8
use League\Fractal\Serializer\SerializerAbstract;
9
use League\Fractal\TransformerAbstract;
10
11
class FractalService implements FractalServiceContract
12
{
13
14
    /**
15
     * @var array
16
     */
17
    private $includes = [];
18
19
    /**
20
     * @var SerializerAbstract
21
     */
22
    private $defaultSerializer;
23
24
25
    /**
26
     * @inheritdoc
27
     */
28
    public function item($data, TransformerAbstract $transformer = null, $resourceKey = null)
29
    {
30
        return $this->makeBuilder(FractalBuilderContract::RESOURCE_ITEM, $data, $transformer, $resourceKey);
31
    }
32
33
34
    /**
35
     * @inheritdoc
36
     */
37
    public function collection($data, TransformerAbstract $transformer = null, $resourceKey = null)
38
    {
39
        return $this->makeBuilder(FractalBuilderContract::RESOURCE_COLLECTION, $data, $transformer, $resourceKey);
40
    }
41
42
43
    /**
44
     * @inheritdoc
45
     */
46
    public function parseIncludes($includes)
47
    {
48
        if (is_string($includes)) {
49
            $includes = explode(',', $includes);
50
        }
51
52
        $this->includes = array_merge($this->includes, (array) $includes);
53
    }
54
55
56
    /**
57
     * @inheritdoc
58
     */
59
    public function setDefaultSerializer(SerializerAbstract $serializer)
60
    {
61
        $this->defaultSerializer = $serializer;
62
    }
63
64
65
    /**
66
     * Creates a builder for serializing data.
67
     *
68
     * @param array                    $resourceClass
69
     * @param mixed                    $data
70
     * @param TransformerAbstract|null $transformer
71
     * @param string|null              $resourceKey
72
     *
73
     * @return FractalBuilder
74
     */
75
    protected function makeBuilder(
76
        $resourceClass,
77
        $data,
78
        TransformerAbstract $transformer = null,
79
        $resourceKey = null
80
    ) {
81
        $fractal = $this->makeFractal();
82
        $builder = new FractalBuilder($fractal, $resourceClass, $data);
0 ignored issues
show
Documentation introduced by
$resourceClass is of type array, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
83
84
        if ($transformer !== null) {
85
            $builder->setTransformer($transformer);
86
        }
87
88
        if ($resourceKey !== null) {
89
            $builder->setResourceKey($resourceKey);
90
        }
91
92
        return $builder;
93
    }
94
95
96
    /**
97
     * @return Manager
98
     */
99
    protected function makeFractal()
100
    {
101
        $fractal = new Manager();
102
103
        if (isset($this->defaultSerializer)) {
104
            $fractal->setSerializer($this->defaultSerializer);
105
        }
106
107
        if (isset($this->includes)) {
108
            $fractal->parseIncludes($this->includes);
109
        }
110
111
        return $fractal;
112
    }
113
}
114