GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 05366b...453307 )
by Freek
02:37
created

FractalFactory   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 3
dl 0
loc 87
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
B getFractalInstance() 0 22 5
A setData() 0 6 1
A determineDataType() 0 12 3
A setTransformer() 0 4 1
A setSerializer() 0 4 1
1
<?php
2
3
namespace Spatie\Fractalistic;
4
5
use Traversable;
6
use League\Fractal\Manager;
7
use League\Fractal\Serializer\SerializerAbstract;
8
use Spatie\Fractalistic\Exceptions\InvalidFractalHelperArgument;
9
10
class FractalFactory
11
{
12
    /** @var \Spatie\Fractalistic\Fractal */
13
    protected $fractal;
14
15
    /** @var array */
16
    protected $arguments;
17
18
    public function __construct(array $arguments)
19
    {
20
        $this->fractal = new Fractal(new Manager());
21
22
        $this->arguments = $arguments;
23
    }
24
25
    /**
26
     * @return \Spatie\Fractalistic\Fractal
27
     *
28
     * @throws \Spatie\Fractalistic\Exceptions\InvalidFractalHelperArgument
29
     */
30
    public function getFractalInstance()
31
    {
32
        if (count($this->arguments) === 1) {
33
            throw InvalidFractalHelperArgument::secondArgumentMissing();
34
        }
35
36
        if (count($this->arguments) >= 2) {
37
            $this->setData($this->arguments[0]);
38
39
            $this->setTransformer($this->arguments[1]);
40
        }
41
42
        if (count($this->arguments) >= 3) {
43
            $this->setSerializer($this->arguments[2]);
44
        }
45
46
        if (count($this->arguments) > 3) {
47
            throw InvalidFractalHelperArgument::tooManyArguments($this->arguments);
48
        }
49
50
        return $this->fractal;
51
    }
52
53
    /**
54
     * @param mixed $data
55
     */
56
    public function setData($data)
57
    {
58
        $dataType = $this->determineDataType($data);
59
60
        $this->fractal->data($dataType, $data);
61
    }
62
63
    /**
64
     * @param mixed $data
65
     *
66
     * @return string
67
     */
68
    protected function determineDataType($data)
69
    {
70
        if (is_array($data)) {
71
            return 'collection';
72
        }
73
74
        if ($data instanceof Traversable) {
75
            return 'collection';
76
        }
77
78
        return 'item';
79
    }
80
81
    /**
82
     * @param callable|\League\Fractal\TransformerAbstract $transformer
83
     */
84
    protected function setTransformer($transformer)
85
    {
86
        $this->fractal->transformWith($transformer);
87
    }
88
89
    /**
90
     * @param \League\Fractal\Serializer\SerializerAbstract $serializer
91
     */
92
    protected function setSerializer(SerializerAbstract $serializer)
93
    {
94
        $this->fractal->serializeWith($serializer);
95
    }
96
}
97