Completed
Push — master ( 952332...2b9cec )
by Nassif
03:07 queued 49s
created

FractalAdapter   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 95.24%

Importance

Changes 0
Metric Value
dl 0
loc 86
ccs 20
cts 21
cp 0.9524
rs 10
c 0
b 0
f 0
wmc 7
lcom 1
cbo 8

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A item() 0 6 1
A collection() 0 6 1
A paginatedCollection() 0 10 1
A getTransformer() 0 10 3
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