FractalAdapter::collection()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 3
dl 0
loc 6
ccs 3
cts 3
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Tequilarapido\ApiResponse;
4
5
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
6
use Illuminate\Contracts\Support\Arrayable;
7
use Illuminate\Http\Request;
8
use League\Fractal\Manager;
9
use League\Fractal\Resource\Collection;
10
use League\Fractal\Resource\Item;
11
use League\Fractal\Serializer\SerializerAbstract;
12
13
class FractalAdapter
14
{
15
    /** @var \League\Fractal\Manager */
16
    protected $manager;
17
18
    /** @var Request */
19
    private $request;
20
21
    /**
22
     * FractalAdapter constructor.
23
     *
24
     * @param SerializerAbstract $serializer
25
     * @param Request $request
26
     */
27 10
    public function __construct(SerializerAbstract $serializer, Request $request)
0 ignored issues
show
Bug introduced by
You have injected the Request via parameter $request. This is generally not recommended as there might be multiple instances during a request cycle (f.e. when using sub-requests). Instead, it is recommended to inject the RequestStack and retrieve the current request each time you need it via getCurrentRequest().
Loading history...
28
    {
29 10
        $this->manager = new Manager();
30 10
        $this->manager->setSerializer($serializer);
31
32 10
        $this->request = $request;
33 10
    }
34
35
    /**
36
     * Return an item.
37
     *
38
     * @param $data
39
     * @param null $transformer
40
     * @param null $resourceKey
41
     * @return array
42
     */
43 1
    public function item($data, $transformer = null, $resourceKey = null)
44
    {
45 1
        $resource = new Item($data, $this->getTransformer($transformer), $resourceKey);
46
47 1
        return $this->manager->createData($resource)->toArray();
48
    }
49
50
    /**
51
     * Return a collection of items.
52
     *
53
     * @param $data
54
     * @param null $transformer
55
     * @param null $resourceKey
56
     * @return array
57
     */
58 1
    public function collection($data, $transformer = null, $resourceKey = null)
59
    {
60 1
        $resource = new Collection($data, $this->getTransformer($transformer), $resourceKey);
61
62 1
        return $this->manager->createData($resource)->toArray();
63
    }
64
65
    /**
66
     * @param LengthAwarePaginator $paginator
67
     * @param null $transformer
68
     * @param null $resourceKey
69
     * @return array
70
     */
71 1
    public function paginatedCollection(LengthAwarePaginator $paginator, $transformer = null, $resourceKey = null)
72
    {
73 1
        $paginator->appends($this->request->query());
0 ignored issues
show
Bug introduced by
It seems like $this->request->query() targeting Illuminate\Http\Concerns...ractsWithInput::query() can also be of type null; however, Illuminate\Contracts\Pag...on\Paginator::appends() does only seem to accept array|string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
74
75 1
        $resource = new Collection($paginator->items(), $this->getTransformer($transformer), $resourceKey);
76
77 1
        $resource->setPaginator(new PaginatorAdapter($paginator));
78
79 1
        return $this->manager->createData($resource)->toArray();
80
    }
81
82
    /**
83
     * Return transformer.
84
     *
85
     * @param $transformer
86
     * @return \Closure
87
     */
88 3
    protected function getTransformer($transformer)
89
    {
90
        return $transformer ?: function ($data) {
91 3
            if ($data instanceof Arrayable) {
92
                return $data->toArray();
93
            }
94
95 3
            return (array) $data;
96 3
        };
97
    }
98
}
99