CustomPaginatedCollection   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 97
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 94.44%

Importance

Changes 0
Metric Value
dl 0
loc 97
c 0
b 0
f 0
wmc 12
lcom 1
cbo 1
ccs 34
cts 36
cp 0.9444
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 15 2
A getItems() 0 4 1
A getItemsIds() 0 4 1
A getTotal() 0 4 1
A getOffset() 0 4 1
A getLimit() 0 4 1
A sortItems() 0 13 2
A getIds() 0 15 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Pagination;
6
7
use Doctrine\ORM\Query;
8
9
class CustomPaginatedCollection implements PaginatedCollectionInterface
10
{
11
    private $offset = 0;
12
    private $limit = 20;
13
    private $items;
14
    private $itemsCount;
15
    private $itemsIds;
16
17
    /**
18
     * CustomPaginatedCollection constructor.
19
     *
20
     * @param Query    $itemsQuery
21
     * @param Query    $idsQuery
22
     * @param Query    $countQuery
23
     * @param int      $offset
24
     * @param int|null $limit
25
     *
26
     * @throws \Doctrine\ORM\NonUniqueResultException
27
     */
28 36
    public function __construct(Query $itemsQuery, Query $idsQuery, Query $countQuery, int $offset, ?int $limit = null)
29
    {
30 36
        $this->offset = abs($offset);
0 ignored issues
show
Documentation Bug introduced by
It seems like abs($offset) can also be of type double. However, the property $offset is declared as type integer. 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...
31
32 36
        if ($limit !== null) {
33
            $this->limit = abs($limit);
0 ignored issues
show
Documentation Bug introduced by
It seems like abs($limit) can also be of type double. However, the property $limit is declared as type integer. 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...
34
        }
35
36 36
        $idsQuery = $idsQuery->setFirstResult($this->offset)->setMaxResults($this->limit);
37 36
        $this->itemsIds = $idsQuery->getArrayResult();
38 36
        $this->itemsCount = $countQuery->getSingleScalarResult();
39 36
        $ids = $this->getIds($this->itemsIds);
40 36
        $itemsQuery->setParameter('ids', $ids);
41 36
        $this->items = $this->sortItems($itemsQuery->getArrayResult(), $ids);
42 36
    }
43
44 36
    public function getItems()
45
    {
46 36
        return $this->items;
47
    }
48
49 2
    public function getItemsIds(): array
50
    {
51 2
        return $this->itemsIds;
52
    }
53
54 36
    public function getTotal(): int
55
    {
56 36
        return (int) $this->itemsCount;
57
    }
58
59 36
    public function getOffset(): int
60
    {
61 36
        return $this->offset;
62
    }
63
64 36
    public function getLimit(): int
65
    {
66 36
        return $this->limit;
67
    }
68
69
    /**
70
     * We are doing next things to improve performance:
71
     * 1. Fetch ids of movies in correct order, lets say [1, 2, 3]
72
     * 2. Then we are loading all information with joins for these movies: SELECT WITH JOINS ... WHERE id IN (1,2,3)
73
     * 3. As a result of step 2 we get movies but ordering is broken on this step: [3 => [data], 1 => [data], 2 => [data]]
74
     * 4. So that's why we need sort items again according to ids that we fetch on step 1.
75
     */
76 36
    private function sortItems(array $items, array $ids): array
77
    {
78 36
        if (\count($ids) === 0) {
79 5
            return [];
80
        }
81
82 33
        $ids = array_flip($ids);
83
        usort($items, function (array $item1, array $item2) use ($ids) {
84 27
            return $ids[$item1['id']] <=> $ids[$item2['id']];
85 33
        });
86
87 33
        return $items;
88
    }
89
90 36
    private function getIds(array $ids): array
91
    {
92 36
        if (\count($ids) === 0) {
93 5
            return [];
94
        }
95
96 33
        $firstValue = reset($ids);
97 33
        if (\is_array($firstValue) === true) {
98
            return array_map(function ($id) {
99 33
                return reset($id);
100 33
            }, $ids);
101
        }
102
103
        return $ids;
104
    }
105
}
106