GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( fab3df...c703cd )
by Sergey
03:15
created

ListQueryParameters::getPaginationParameters()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 9.4285
cc 3
eloc 4
nc 4
nop 0
crap 3
1
<?php
2
/*
3
 * This file is part of the reva2/jsonapi.
4
 *
5
 * (c) OrbitScripts LLC <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
12
namespace Reva2\JsonApi\Http\Query;
13
14
use Doctrine\Common\Proxy\Exception\InvalidArgumentException;
15
use Neomerx\JsonApi\Contracts\Encoder\Parameters\SortParameterInterface;
16
use Neomerx\JsonApi\Encoder\Parameters\SortParameter;
17
use Symfony\Component\Validator\Context\ExecutionContextInterface;
18
19
/**
20
 * JSON API resources list parameters
21
 *
22
 * @package Reva2\JsonApi\Http\Query
23
 * @author Sergey Revenko <[email protected]>
24
 */
25
class ListQueryParameters extends QueryParameters
26
{
27
    const INVALID_PAGE_SIZE = '400b395f-ee4f-4138-aad8-89f9a1872291';
28
    const INVALID_SORTING = 'e57fef44-21e4-48c4-a30d-b92009a0c16a';
29
30
    /**
31
     * @var integer|null
32
     * @Reva2\JsonApi\Annotations\Property(type="integer", path="[page][number]")
33
     * @Symfony\Component\Validator\Constraints\Type(type="integer")
34
     * @Symfony\Component\Validator\Constraints\GreaterThan(value=0)
35
     */
36
    protected $pageNumber;
37
38
    /**
39
     * @var integer|null
40
     * @Reva2\JsonApi\Annotations\Property(type="integer", path="[page][size]")
41
     * @Symfony\Component\Validator\Constraints\Type(type="integer")
42
     * @Symfony\Component\Validator\Constraints\GreaterThan(value=0)
43
     */
44
    protected $pageSize;
45
46
    /**
47
     * @var SortParameterInterface[]|null
48
     * @Reva2\JsonApi\Annotations\Property(path="[sort]", parser="parseSortingParameters")
49
     * @Symfony\Component\Validator\Constraints\Type(type="array")
50
     */
51
    protected $sortParameters;
52
53
    /**
54
     * @param int|null $pageNumber
55
     * @return $this
56
     */
57 1
    public function setPageNumber($pageNumber = null)
58
    {
59 1
        $this->pageNumber = $pageNumber;
60
61 1
        return $this;
62
    }
63
64
    /**
65
     * @param int|null $pageSize
66
     * @return $this
67
     */
68 2
    public function setPageSize($pageSize = null)
69
    {
70 2
        $this->pageSize = $pageSize;
71
72 2
        return $this;
73
    }
74
75
    /**
76
     * @inheritdoc
77
     */
78 1
    public function getPaginationParameters()
79
    {
80
        return [
81 1
            'number' => ($this->pageNumber) ? $this->pageNumber : 1,
82 1
            'size' => ($this->pageSize) ? $this->pageSize : $this->getDefaultPageSize()
83 1
        ];
84
    }
85
86
    /**
87
     * Sets sorting parameters
88
     *
89
     * @param SortParameterInterface[]|null $sorting
90
     * @return $this
91
     */
92 2
    public function setSortParameters(array $sorting = null)
93
    {
94 2
        $this->sortParameters = $sorting;
95
96 2
        return $this;
97
    }
98
99
    /**
100
     * @inheritdoc
101
     */
102 1
    public function getSortParameters()
103
    {
104 1
        return $this->sortParameters;
105
    }
106
107
    /**
108
     * Parse sorting parameters string
109
     *
110
     * @param string $value
111
     * @return array
112
     */
113 3
    public function parseSortingParameters($value)
114
    {
115 3
        if (empty($value)) {
116 1
            return null;
117
        }
118
119 3
        if (!is_string($value)) {
120 1
            throw new InvalidArgumentException('Sorting parameters must be a string', 400);
121
        }
122
123 2
        $sorting = [];
124
125 2
        $fields = explode(',', $value);
126 2
        foreach ($fields as $field) {
127 2
            $isAsc = ('-' !== $field[0]) ? true : false;
128 2
            if (false === $isAsc) {
129 2
                $field = mb_substr($field, 1);
130 2
            }
131
132 2
            $sorting[] = new SortParameter($field, $isAsc);
133 2
        }
134
135 2
        return (!empty($sorting)) ? $sorting : null;
136
    }
137
138
    /**
139
     * Validate sort parameters
140
     *
141
     * @param ExecutionContextInterface $context
142
     * @Symfony\Component\Validator\Constraints\Callback()
143
     */
144 1
    public function validateSortParameters(ExecutionContextInterface $context)
145
    {
146 1
        if (empty($this->sortParameters)) {
147 1
            return;
148
        }
149
150 1
        $fields = [];
151 1
        foreach ($this->sortParameters as $parameter) {
152 1
            $fields[] = $parameter->getField();
153 1
        }
154
155 1
        $invalidFields = array_diff($fields, $this->getSortableFields());
156 1 View Code Duplication
        if (count($invalidFields) > 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...
157
            $context
158 1
                ->buildViolation('Sorting by following fields is not supported: %fields%')
159 1
                ->setParameter('%fields%', sprintf("'%s'", implode("', '", $invalidFields)))
160 1
                ->setPlural(count($invalidFields))
161 1
                ->setInvalidValue($invalidFields)
162 1
                ->setCode(self::INVALID_SORTING)
163 1
                ->atPath('sortParameters')
164 1
                ->addViolation();
165 1
        }
166 1
    }
167
168
    /**
169
     * Validate page size
170
     *
171
     * @param ExecutionContextInterface $context
172
     * @Symfony\Component\Validator\Constraints\Callback()
173
     */
174 1
    public function validatePageSize(ExecutionContextInterface $context)
175
    {
176 1
        if (!empty($this->pageSize) &&
177 1
            (null !== ($maxSize = $this->getMaxPageSize())) &&
178 1
            ($this->pageSize > $maxSize)
179 1
        ) {
180
            $context
181 1
                ->buildViolation('Page size must be leas or equal than %size%')
182 1
                ->setParameter('%size%', $maxSize)
183 1
                ->setInvalidValue($this->pageSize)
184 1
                ->setCode(self::INVALID_PAGE_SIZE)
185 1
                ->atPath('pageSize')
186 1
                ->addViolation();
187 1
        }
188 1
    }
189
190
    /**
191
     * Returns default page size
192
     *
193
     * @return int|null
194
     */
195
    protected function getDefaultPageSize()
196
    {
197
        return 10;
198
    }
199
200
    /**
201
     * Returns max page size
202
     *
203
     * @return int|null
204
     */
205 1
    protected function getMaxPageSize()
206
    {
207 1
        return 100;
208
    }
209
210
    /**
211
     * Returns list of supported sorting fields
212
     *
213
     * @return string[]
214
     */
215 1
    protected function getSortableFields()
216
    {
217 1
        return [];
218
    }
219
}