Completed
Push — master ( 612f00...c95169 )
by Nassif
02:34
created

FractalAdapter::paginatedCollection()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 3
dl 0
loc 10
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\Http\Request;
7
use League\Fractal\Manager;
8
use League\Fractal\Resource\Collection;
9
use League\Fractal\Resource\Item;
10
use League\Fractal\Serializer\SerializerAbstract;
11
12
class FractalAdapter
13
{
14
    /** @var \League\Fractal\Manager */
15
    protected $manager;
16
17
    /** @var Request */
18
    private $request;
19
20
    /**
21
     * FractalAdapter constructor.
22
     *
23
     * @param SerializerAbstract $serializer
24
     * @param Request $request
25
     */
26
    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...
27
    {
28
        $this->manager = new Manager();
29
        $this->manager->setSerializer($serializer);
30
31
        $this->request = $request;
32
    }
33
34
    /**
35
     * Return an item.
36
     *
37
     * @param $data
38
     * @param null $transformer
39
     * @param null $resourceKey
40
     * @return array
41
     */
42
    public function item($data, $transformer = null, $resourceKey = null)
43
    {
44
        $resource = new Item($data, $this->getTransformer($transformer), $resourceKey);
45
46
        return $this->manager->createData($resource)->toArray();
47
    }
48
49
    /**
50
     * Return a collection of items.
51
     *
52
     * @param $data
53
     * @param null $transformer
54
     * @param null $resourceKey
55
     * @return array
56
     */
57
    public function collection($data, $transformer = null, $resourceKey = null)
58
    {
59
        $resource = new Collection($data, $this->getTransformer($transformer), $resourceKey);
60
61
        return $this->manager->createData($resource)->toArray();
62
    }
63
64
    /**
65
     * @param LengthAwarePaginator $paginator
66
     * @param null $transformer
67
     * @param null $resourceKey
68
     * @return array
69
     */
70
    public function paginatedCollection(LengthAwarePaginator $paginator, $transformer = null, $resourceKey = null)
71
    {
72
        $paginator->appends($this->request->query());
73
74
        $resource = new Collection($paginator->items(), $this->getTransformer($transformer), $resourceKey);
75
76
        $resource->setPaginator(new PaginatorAdapter($paginator));
77
78
        return $this->manager->createData($resource)->toArray();
79
    }
80
81
    /**
82
     * Return transformer.
83
     *
84
     * @param $transformer
85
     * @return \Closure
86
     */
87
    protected function getTransformer($transformer)
88
    {
89
        return $transformer ?: function ($data) {
90
91
            if ($data instanceof Arrayable) {
0 ignored issues
show
Bug introduced by
The class Tequilarapido\ApiResponse\Arrayable does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
92
                return $data->toArray();
93
            }
94
95
            return (array)$data;
96
        };
97
    }
98
}
99