PaginatorConfiguration::find()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 5
nc 3
nop 1
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * This file is part of laravel.su package.
4
 * For the full copyright and license information, please view the LICENSE
5
 * file that was distributed with this source code.
6
 */
7
declare(strict_types=1);
8
9
namespace App\GraphQL\Feature\Paginator;
10
11
use GraphQL\Type\Definition\Type;
12
use Illuminate\Support\Collection;
13
use Illuminate\Database\Eloquent\Builder;
14
use App\GraphQL\Serializers\RawSerializer;
15
use App\GraphQL\Serializers\AbstractSerializer;
16
17
/**
18
 * Class PaginatorConfiguration
19
 * @package App\GraphQL\Kernel\Paginator
20
 */
21
class PaginatorConfiguration implements \IteratorAggregate
22
{
23
    public const QUERY_LIMIT = '_limit';
24
    public const QUERY_PAGE  = '_page';
25
26
    private const LIMIT_DEFAULT = 10;
27
    private const LIMIT_MIN  = 1;
28
    private const LIMIT_MAX  = 100;
29
30
    /**
31
     * @var array|PaginatorConfiguration[]
32
     */
33
    private static $configs = [];
34
35
    /**
36
     * @var null|AbstractSerializer
37
     */
38
    protected $serializer = null;
39
40
    /**
41
     * @var null|Builder
42
     */
43
    protected $query;
44
45
    /**
46
     * @var int
47
     */
48
    protected $limit = self::LIMIT_DEFAULT;
49
50
    /**
51
     * @var int
52
     */
53
    protected $page = 1;
54
55
    /**
56
     * @var null|string
57
     */
58
    private $alias;
59
60
    /**
61
     * @var int
62
     */
63
    private $perPage = self::LIMIT_DEFAULT;
64
65
    /**
66
     * @var int
67
     */
68
    private $count;
69
70
    /**
71
     * PaginatorConfiguration constructor.
72
     * @param Builder $builder
73
     */
74
    public function __construct(Builder $builder, int $count)
75
    {
76
        self::$configs[] = $this;
77
        $this->query = $builder;
78
        $this->count = $count;
79
    }
80
81
    /**
82
     * @param string $alias
83
     * @return PaginatorConfiguration
84
     */
85
    public static function find(string $alias): ?PaginatorConfiguration
86
    {
87
        foreach (self::$configs as $config) {
88
            if ($config->getAlias() === $alias) {
89
                return $config;
90
            }
91
        }
92
93
        return null;
94
    }
95
96
    /**
97
     * @return int
98
     */
99
    public function getCount(): int
100
    {
101
        return $this->count;
102
    }
103
104
    /**
105
     * @return int
106
     */
107
    public function getPerPage(): int
108
    {
109
        return $this->perPage;
110
    }
111
112
    /**
113
     * @return int
114
     */
115
    public function getPages(): int
116
    {
117
        return (int)ceil($this->count / $this->getLimit());
118
    }
119
120
    /**
121
     * @return array
122
     */
123
    public static function getConfigs(): array
124
    {
125
        return self::$configs;
126
    }
127
128
    /**
129
     * @return AbstractSerializer|null
130
     */
131
    public function getSerializer()
132
    {
133
        return $this->serializer;
134
    }
135
136
    /**
137
     * @return Builder
138
     */
139
    public function getQuery(): Builder
140
    {
141
        return $this->query;
142
    }
143
144
    /**
145
     * @return int|null
146
     */
147
    public function getLimit()
148
    {
149
        return $this->limit;
150
    }
151
152
    /**
153
     * @return int
154
     */
155
    public function getPage(): int
156
    {
157
        return $this->page;
158
    }
159
160
    /**
161
     * @return int
162
     */
163
    public function getSkip(): int
164
    {
165
        return (int)($this->page - 1) * $this->limit;
166
    }
167
168
    /**
169
     * @return null|string
170
     */
171
    public function getAlias()
172
    {
173
        return $this->alias;
174
    }
175
176
    /**
177
     * @param array $args
178
     * @return $this
179
     */
180
    public function withArgs(array $args)
181
    {
182
        $this->limit = $args[static::QUERY_LIMIT] ?? self::LIMIT_DEFAULT;
183
        $this->limit = min(self::LIMIT_MAX, $this->limit);
184
        $this->limit = max(self::LIMIT_MIN, $this->limit);
185
186
187
        $this->page  = max(1, $args[static::QUERY_PAGE] ?? 1);
188
189
        return $this;
190
    }
191
192
    /**
193
     * @param $data
194
     * @return Collection
195
     */
196
    private function serializeCollection($data)
197
    {
198
        if ($this->serializer === null) {
199
            return RawSerializer::collection($data);
200
        }
201
202
        return $this->serializer::collection($data);
203
    }
204
205
    /**
206
     * @param null|string $serializer
207
     * @return $this|PaginatorConfiguration
208
     */
209
    public function use(?string $serializer): PaginatorConfiguration
210
    {
211
        $this->serializer = $serializer;
0 ignored issues
show
Documentation Bug introduced by
It seems like $serializer can also be of type string. However, the property $serializer is declared as type null|object<App\GraphQL\...ers\AbstractSerializer>. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
212
213
        return $this;
214
    }
215
216
    /**
217
     * @param string $graphQlQuery
218
     * @return PaginatorConfiguration
219
     */
220
    public function as(string $graphQlQuery): PaginatorConfiguration
221
    {
222
        $this->alias = $graphQlQuery;
223
224
        return $this;
225
    }
226
227
    /**
228
     * @return Collection
229
     */
230
    public function getIterator()
231
    {
232
        $query = clone $this->query;
233
234
        if ($this->limit !== null) {
235
            $query = $query->take((int)$this->limit);
236
        }
237
238
        if ($this->page > 0 && $this->limit !== null) {
239
            $query = $query->skip($this->getSkip());
240
        }
241
242
        $result = $query->get();
243
244
        $this->perPage = $result->count();
245
246
        return $this->serializeCollection($result);
247
    }
248
}