JsonApiEncoder::encode()   C
last analyzed

Complexity

Conditions 7
Paths 10

Size

Total Lines 43
Code Lines 26

Duplication

Lines 6
Ratio 13.95 %

Importance

Changes 0
Metric Value
dl 6
loc 43
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 26
nc 10
nop 4
1
<?php
2
3
namespace App\Common;
4
5
use \Neomerx\JsonApi\Encoder\Encoder;
6
use \Neomerx\JsonApi\Encoder\EncoderOptions;
7
use \Neomerx\JsonApi\Factories\Factory;
8
use \Neomerx\JsonApi\Contracts\Document\LinkInterface;
9
use \Neomerx\JsonApi\Document\Link;
10
use Slim\Http\Request;
11
use App\Model\User;
12
13
final class JsonApiEncoder
14
{
15
    private $settings;
16
17
    /**
18
     * JsonApiEncoder constructor.
19
     *
20
     * @param $settings
21
     */
22
    public function __construct($settings)
23
    {
24
        $this->settings = $settings;
25
    }
26
27
    /**
28
     * @param Request      $request
29
     * @param mixed        $entities
30
     * @param integer|null $pageNumber
31
     * @param integer|null $pageSize
32
     *
33
     * @return string
34
     */
35
    public function encode(Request $request, $entities, $pageNumber = null, $pageSize = null)
36
    {
37
        $factory    = new Factory();
38
        $parameters = $factory->createQueryParametersParser()->parse($request);
39
        $user       = Auth::getUser();
40
        $schemas    = $this->settings['encoder']['schemas']['default'];
41
42
        if ($user && $user->role_id == User::ROLE_ADMIN) {
43
            $schemas = $this->settings['encoder']['schemas']['extended'];
44
        }
45
46
        $encoder = Encoder::instance(
47
            $schemas,
48
            new EncoderOptions(
49
                JSON_PRETTY_PRINT,
50
                $this->settings['params']['host'].'/api'
51
            )
52
        );
53
54
        if (isset($pageNumber) && isset($pageSize)) {
55
            $encoder->withMeta([
56
                'total' => $entities->total(),
57
                'count' => $entities->count(),
58
            ]);
59
60
            $links = [
61
                LinkInterface::SELF  => new Link('?page[number]='.$pageNumber.'&page[size]='.$pageSize, null, false),
62
                LinkInterface::FIRST => new Link('?page[number]=1&page[size]='.$pageSize, null, false),
63
                LinkInterface::LAST  => new Link('?page[number]='.$entities->lastPage().'&page[size]='.$pageSize, null, false),
64
            ];
65
66 View Code Duplication
            if (($entities->lastPage() - ($pageNumber + 1)) >= 0) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
67
                $links[LinkInterface::NEXT] = new Link('?page[number]='.($pageNumber + 1).'&page[size]='.$pageSize, null, false);
68
            }
69 View Code Duplication
            if (($pageNumber - 1) > 0) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
70
                $links[LinkInterface::PREV] = new Link('?page[number]='.($pageNumber - 1).'&page[size]='.$pageSize, null, false);
71
            }
72
73
            $encoder->withLinks($links);
74
        }
75
76
        return $encoder->encodeData($entities, $parameters);
77
    }
78
}
79