Test Failed
Push — master ( baa3df...d825b9 )
by Julien
04:52
created

Fractal::setTransformer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
1
<?php
2
3
namespace Zemit\Mvc\Controller\Rest;
4
5
use League\Fractal\Resource\Collection;
6
use League\Fractal\Resource\Item;
7
use League\Fractal\Serializer\SerializerAbstract;
8
use League\Fractal\TransformerAbstract;
9
use Phalcon\Mvc\Model\ResultsetInterface;
10
use Phalcon\Mvc\ModelInterface;
11
use Zemit\Fractal\Manager;
12
use Zemit\Fractal\ModelTransformer;
13
use Zemit\Fractal\Serializer\RawArraySerializer;
14
15
trait Fractal
16
{
17
    public ?Manager $fractalManager;
18
    public ?SerializerAbstract $fractalSerializer;
19
    public ?TransformerAbstract $transformer;
20
    
21
    /**
22
     * Get the default fractal manager
23
     */
24
    public function getFractalManager(): Manager
25
    {
26
        if (!$this->fractalManager) {
27
            $this->fractalManager = new Manager();
28
            $this->fractalManager->setSerializer($this->getFractalSerializer());
29
        }
30
        
31
        return $this->fractalManager;
32
    }
33
    
34
    /**
35
     * Set the default fractal manager
36
     */
37
    public function setFractalManager(?Manager $manager): void
38
    {
39
        $this->fractalManager = $manager;
40
    }
41
    
42
    /**
43
     * Get the default fractal serializer
44
     */
45
    public function getFractalSerializer(): SerializerAbstract
46
    {
47
        if (!$this->fractalSerializer) {
48
            $this->fractalSerializer = new RawArraySerializer();
49
        }
50
        
51
        return $this->fractalSerializer;
52
    }
53
    
54
    /**
55
     * Set the default fractal serializer
56
     */
57
    public function setFractalSerializer(SerializerAbstract $serializer): void
58
    {
59
        $this->fractalSerializer = $serializer;
60
    }
61
    
62
    /**
63
     * Get the default transformer for the fractal manager
64
     */
65
    public function getTransformer(): ?TransformerAbstract
66
    {
67
        if (!$this->transformer) {
68
            $this->transformer = new ModelTransformer();
69
        }
70
        
71
        return $this->transformer;
72
    }
73
    
74
    /**
75
     * Set a default transformer for the fractal manager
76
     */
77
    public function setTransformer(?TransformerAbstract $transformer = null): void
78
    {
79
        $this->transformer = $transformer;
80
    }
81
    
82
    /**
83
     * Return true if the transformer is defined
84
     */
85
    public function hasTransformer(): bool
86
    {
87
        return (bool)$this->transformer;
88
    }
89
    
90
    /**
91
     * Transform the model using the fractal manager, serializer and transformer
92
     */
93
    public function transformModel(ModelInterface $model, ?TransformerAbstract $transformer = null, ?Manager $fractalManager = null): array
94
    {
95
        $fractalManager ??= $this->getFractalManager();
96
        $transformer ??= $this->getTransformer();
97
        return $fractalManager->createData(new Item($model, $transformer))->toArray();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $fractalManager->...ransformer))->toArray() could return the type null which is incompatible with the type-hinted return array. Consider adding an additional type-check to rule them out.
Loading history...
98
    }
99
    
100
    /**
101
     * Transform the resultset models using the fractal manager, serializer and transformer
102
     */
103
    public function transformResultset(ResultsetInterface $resultset, ?TransformerAbstract $transformer = null, ?Manager $fractalManager = null): array
104
    {
105
        $fractalManager ??= $this->getFractalManager();
106
        $transformer ??= $this->getTransformer();
107
        return $fractalManager->createData(new Collection($resultset, $transformer))->toArray();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $fractalManager->...ransformer))->toArray() could return the type null which is incompatible with the type-hinted return array. Consider adding an additional type-check to rule them out.
Loading history...
108
    }
109
}
110