FractalBuilder   A
last analyzed

Complexity

Total Complexity 26

Size/Duplication

Total Lines 245
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
dl 0
loc 245
c 0
b 0
f 0
wmc 26
lcom 1
cbo 5
rs 10

14 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A setMeta() 0 6 1
A setTransformer() 0 6 1
A setResourceKey() 0 10 3
A setPaginator() 0 10 2
A setCursor() 0 10 2
A setSerializer() 0 6 1
A toArray() 0 4 1
A toJson() 0 4 1
A makeResource() 0 15 4
A makeScope() 0 10 2
A setFractal() 0 8 2
A setResourceClass() 0 12 4
A setData() 0 4 1
1
<?php
2
3
namespace Nord\Lumen\Fractal;
4
5
use League\Fractal\Pagination\CursorInterface;
6
use League\Fractal\Resource\Item;
7
use League\Fractal\Resource\ResourceAbstract;
8
use Nord\Lumen\Fractal\Contracts\FractalBuilder as FractalBuilderContract;
9
use League\Fractal\Manager;
10
use League\Fractal\Pagination\PaginatorInterface;
11
use League\Fractal\Resource\Collection;
12
use League\Fractal\Scope;
13
use League\Fractal\Serializer\SerializerAbstract;
14
use League\Fractal\TransformerAbstract;
15
use Nord\Lumen\Fractal\Exceptions\InvalidArgument;
16
use Nord\Lumen\Fractal\Exceptions\NotApplicable;
17
18
class FractalBuilder implements FractalBuilderContract
19
{
20
21
    /**
22
     * @var Manager
23
     */
24
    private $fractal;
25
26
    /**
27
     * @var string
28
     */
29
    private $resourceClass;
30
31
    /**
32
     * @var mixed
33
     */
34
    private $data;
35
36
    /**
37
     * @var array
38
     */
39
    private $meta = [];
40
41
    /**
42
     * @var string
43
     */
44
    private $resourceKey;
45
46
    /**
47
     * @var TransformerAbstract
48
     */
49
    private $transformer;
50
51
    /**
52
     * @var SerializerAbstract
53
     */
54
    private $serializer;
55
56
    /**
57
     * @var PaginatorInterface
58
     */
59
    private $paginator;
60
61
    /**
62
     * @var CursorInterface
63
     */
64
    private $cursor;
65
66
    /**
67
     * @var array
68
     */
69
    private static $validResourceClasses = [
70
        self::RESOURCE_COLLECTION,
71
        self::RESOURCE_ITEM,
72
    ];
73
74
75
    /**
76
     * FractalBuilder constructor.
77
     *
78
     * @param Manager $fractal
79
     * @param string  $resourceClass
80
     * @param mixed   $data
81
     */
82
    public function __construct(Manager $fractal, $resourceClass, $data)
83
    {
84
        $this->setFractal($fractal);
85
        $this->setResourceClass($resourceClass);
86
        $this->setData($data);
87
    }
88
89
90
    /**
91
     * @inheritdoc
92
     */
93
    public function setMeta(array $meta)
94
    {
95
        $this->meta = $meta;
96
97
        return $this;
98
    }
99
100
101
    /**
102
     * @inheritdoc
103
     */
104
    public function setTransformer(TransformerAbstract $transformer)
105
    {
106
        $this->transformer = $transformer;
107
108
        return $this;
109
    }
110
111
112
    /**
113
     * @inheritdoc
114
     */
115
    public function setResourceKey($resourceKey)
116
    {
117
        if (!is_string($resourceKey) || strlen($resourceKey) === 0) {
118
            throw new InvalidArgument('Resource key must be a non-empty string.');
119
        }
120
121
        $this->resourceKey = $resourceKey;
122
123
        return $this;
124
    }
125
126
127
    /**
128
     * @inheritdoc
129
     */
130
    public function setPaginator(PaginatorInterface $paginator)
131
    {
132
        if ($this->resourceClass !== self::RESOURCE_COLLECTION) {
133
            throw new NotApplicable('Paginators can only be used with collections.');
134
        }
135
136
        $this->paginator = $paginator;
137
138
        return $this;
139
    }
140
141
142
    /**
143
     * @inheritdoc
144
     */
145
    public function setCursor(CursorInterface $cursor)
146
    {
147
        if ($this->resourceClass !== self::RESOURCE_COLLECTION) {
148
            throw new NotApplicable('Cursors can only be used with collections.');
149
        }
150
151
        $this->cursor = $cursor;
152
153
        return $this;
154
    }
155
156
157
    /**
158
     * @inheritdoc
159
     */
160
    public function setSerializer(SerializerAbstract $serializer)
161
    {
162
        $this->serializer = $serializer;
163
164
        return $this;
165
    }
166
167
168
    /**
169
     * @inheritdoc
170
     */
171
    public function toArray()
172
    {
173
        return $this->makeScope()->toArray();
174
    }
175
176
177
    /**
178
     * @inheritdoc
179
     */
180
    public function toJson()
181
    {
182
        return $this->makeScope()->toJson();
183
    }
184
185
186
    /**
187
     * Creates the resource for this builder.
188
     *
189
     * @return ResourceAbstract
190
     */
191
    protected function makeResource()
192
    {
193
        /** @var Item|Collection $resource */
194
        $resource = new $this->resourceClass($this->data, $this->transformer, $this->resourceKey);
195
196
        if (!empty($this->meta)) {
197
            $resource->setMeta($this->meta);
198
        }
199
200
        if ($resource instanceof Collection && isset($this->paginator)) {
201
            $resource->setPaginator($this->paginator);
202
        }
203
204
        return $resource;
205
    }
206
207
208
    /**
209
     * Creates the scope for this builder.
210
     *
211
     * @return Scope
212
     */
213
    protected function makeScope()
214
    {
215
        $resource = $this->makeResource();
216
217
        if (isset($this->serializer)) {
218
            $this->fractal->setSerializer($this->serializer);
219
        }
220
221
        return $this->fractal->createData($resource);
222
    }
223
224
225
    /**
226
     * @param Manager $fractal
227
     */
228
    private function setFractal(Manager $fractal)
229
    {
230
        if ($fractal === null) {
231
            throw new InvalidArgument('Fractal must be an instance of League\Fractal\Manager.');
232
        }
233
234
        $this->fractal = $fractal;
235
    }
236
237
238
    /**
239
     * @param string $resourceClass
240
     */
241
    private function setResourceClass($resourceClass)
242
    {
243
        if (!is_string($resourceClass) || strlen($resourceClass) === 0) {
244
            throw new InvalidArgument('Resource class must be a non-empty string.');
245
        }
246
247
        if (!in_array($resourceClass, self::$validResourceClasses)) {
248
            throw new InvalidArgument('Resource class is invalid.');
249
        }
250
251
        $this->resourceClass = $resourceClass;
252
    }
253
254
255
    /**
256
     * @param mixed $data
257
     */
258
    private function setData($data)
259
    {
260
        $this->data = $data;
261
    }
262
}
263