Completed
Push — master ( 2b9cec...803551 )
by Nassif
05:15 queued 03:08
created

FractalAdapter::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 2
dl 0
loc 7
ccs 5
cts 5
cp 1
crap 1
rs 9.4285
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 9
    public function __construct(SerializerAbstract $serializer, Request $request)
1 ignored issue
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 9
        $this->manager = new Manager();
30 9
        $this->manager->setSerializer($serializer);
31
32 9
        $this->request = $request;
33 9
    }
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());
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
    protected function getTransformer($transformer)
89
    {
90 3
        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