Completed
Push — master ( 856811...eec34e )
by Siim
12:20
created

AbstractResponseListDto::getCount()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: siim
5
 * Date: 29.01.19
6
 * Time: 9:36
7
 */
8
9
namespace Sf4\Api\Dto\Response;
10
11
use Doctrine\Common\Collections\ArrayCollection;
12
use Sf4\Api\Dto\DtoInterface;
13
use Sf4\Api\Dto\Filter\FilterInterface;
14
use Sf4\Api\Dto\Order\OrderInterface;
15
use Sf4\Api\Dto\Request\RequestListDtoInterface;
16
use Sf4\Api\Dto\Traits\FilterTrait;
17
use Sf4\Api\Dto\Traits\ItemsTrait;
18
use Sf4\Api\Dto\Traits\OrdersTrait;
19
use Sf4\Api\Dto\Traits\PaginationTrait;
20
21
abstract class AbstractResponseListDto extends AbstractResponseDto
22
{
23
    use ItemsTrait;
24
    use PaginationTrait;
25
    use FilterTrait;
26
    use OrdersTrait;
27
28
    public function __construct()
29
    {
30
        $this->items = new ArrayCollection();
31
        $this->orders = new ArrayCollection();
32
        parent::__construct();
33
    }
34
35
    public abstract function getListItemClass(): string;
36
37
    /**
38
     * @return array
39
     */
40
    public function toArray(): array
41
    {
42
        $data = parent::toArray();
43
        $data['items'] = $this->getItemsData();
44
        $data[RequestListDtoInterface::FIELD_FILTER] = $this->getFilterData();
45
        $data[RequestListDtoInterface::FIELD_SORT] = $this->getSortData();
46
        return $data;
47
    }
48
49
    protected function getItemsData(): array
50
    {
51
        $data = [];
52
        /** @var DtoInterface $item */
53
        foreach($this->items as $item) {
54
            $data[] = $item->toArray();
55
        }
56
57
        return $data;
58
    }
59
60
    protected function getFilterData(): array
61
    {
62
        $data = [];
63
        if($this->getFilter() instanceof FilterInterface) {
0 ignored issues
show
introduced by
$this->getFilter() is always a sub-type of Sf4\Api\Dto\Filter\FilterInterface.
Loading history...
64
            $data = $this->getFilter()->toArray();
65
        }
66
67
        return $data;
68
    }
69
70
    protected function getSortData(): array
71
    {
72
        $data = [];
73
        /** @var OrderInterface $order */
74
        foreach($this->getOrders() as $order) {
75
            $data[] = $order->toArray();
76
        }
77
78
        return $data;
79
    }
80
81
    /**
82
     * @param array $data
83
     * @throws \ReflectionException
84
     */
85
    public function populate(array $data): void
86
    {
87
        $listItemClass = $this->getListItemClass();
88
        foreach($data as $item) {
89
            if(is_array($item)) {
90
                $dto = new $listItemClass();
91
                if($dto instanceof DtoInterface) {
92
                    $dto->populate($item);
93
                    $this->addItem($dto);
94
                }
95
            }
96
        }
97
98
        $this->setCount($this->items->count());
99
        if($this->getTotal() <= 0) {
100
            $this->setTotal($this->getCount());
101
        }
102
103
        $this->setTotalPages(ceil($this->getTotal() / $this->getItemsPerPage()));
0 ignored issues
show
Bug introduced by
ceil($this->getTotal() /...his->getItemsPerPage()) of type double is incompatible with the type integer expected by parameter $totalPages of Sf4\Api\Dto\Response\Abs...istDto::setTotalPages(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

103
        $this->setTotalPages(/** @scrutinizer ignore-type */ ceil($this->getTotal() / $this->getItemsPerPage()));
Loading history...
104
105
        if($this->getTotalPages() > $this->getCurrentPage()) {
106
            $this->setNextPage($this->getCurrentPage() + 1);
107
        }
108
        if($this->getCurrentPage() > 1) {
109
            $this->setPreviousPage($this->getCurrentPage() - 1);
110
        }
111
    }
112
}
113