FractalService::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 4
c 0
b 0
f 0
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
namespace App\Project\App\Support;
4
5
use League\Fractal\Manager;
6
use League\Fractal\Pagination\PagerfantaPaginatorAdapter;
7
use League\Fractal\Resource\ResourceInterface;
8
use League\Fractal\Serializer\JsonApiSerializer;
9
10
class FractalService extends Manager
11
{
12
13
    /**
14
     * @var string
15
     */
16
    private $baseUrl;
17
18
    public function __construct($baseUrl)
19
    {
20
        $this->baseUrl = $baseUrl;
21
        parent::__construct();
22
    }
23
24
    /**
25
     * @param $resource
26
     * @param bool $success
27
     * @return array
28
     */
29
    public function transform($resource, $success = true)
30
    {
31
        if ($resource instanceof ResourceInterface) {
32
33
            $this->setSerializer(new JsonApiSerializer($this->baseUrl));
34
35
            //if there is an include, set transformer include
36
            if (isset($_GET['include']) && !empty($_GET['include'])) {
37
                $this->parseIncludes($_GET['include']);
38
            }
39
40
            $resource = $this->createData($resource);
41
42
            $response = array_merge(['success' => $success], $resource->toArray());
43
        }else {
44
            $response = [
45
                'success' => $success,
46
                'message' => $resource
47
            ];
48
        }
49
50
        return $response;
51
52
    }
53
54
    protected function paginatorAdapter()
55
    {
56
        $paginatorAdapter = new PagerfantaPaginatorAdapter($paginator, function(int $page) use ($request, $router) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $request seems to be never defined.
Loading history...
Unused Code introduced by
The assignment to $paginatorAdapter is dead and can be removed.
Loading history...
Comprehensibility Best Practice introduced by
The variable $paginator does not exist. Did you maybe mean $paginatorAdapter?
Loading history...
Comprehensibility Best Practice introduced by
The variable $router seems to be never defined.
Loading history...
57
            $route = $request->attributes->get('_route');
58
            $inputParams = $request->attributes->get('_route_params');
59
            $newParams = array_merge($inputParams, $request->query->all());
60
            $newParams['page'] = $page;
61
            return $router->generate($route, $newParams, 0);
62
        });
63
    }
64
65
}
66