Completed
Push — master ( 4f3de5...9b9e7a )
by Pavel
05:51
created

JsonApiEncoder::encode()   C

Complexity

Conditions 9
Paths 40

Size

Total Lines 49
Code Lines 29

Duplication

Lines 6
Ratio 12.24 %

Importance

Changes 0
Metric Value
cc 9
eloc 29
nc 40
nop 4
dl 6
loc 49
rs 5.7446
c 0
b 0
f 0
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
        $encodeEntities = $this->settings['encoder']['default'];
41
42
        if ($user && $user->role_id == User::ROLE_ADMIN) {
43
            $encodeEntities = $this->settings['encoder']['extended'];
44
        }
45
46
        $encoder = Encoder::instance(
47
            $encodeEntities,
48
            new EncoderOptions(
49
                JSON_PRETTY_PRINT,
50
                $this->settings['params']['host'].'/api'
51
            )
52
        );
53
54
        if (isset($pageNumber) && isset($pageSize)) {
55
            $links = [
56
                LinkInterface::SELF  => new Link('?page[number]='.$pageNumber.'&page[size]='.$pageSize, null, false),
57
                LinkInterface::FIRST => new Link('?page[number]=1&page[size]='.$pageSize, null, false),
58
                LinkInterface::LAST  => new Link('?page[number]='.$entities->lastPage().'&page[size]='.$pageSize, null, false),
59
            ];
60
61
            $meta = [
62
                'total' => $entities->total(),
63
                'count' => $entities->count(),
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
74
        if (isset($links)) {
75
            $encoder->withLinks($links);
76
        }
77
78
        if (isset($meta)) {
79
            $encoder->withMeta($meta);
80
        }
81
82
        return $encoder->encodeData($entities, $parameters);
83
    }
84
}
85