Passed
Push — master ( c595fe...762937 )
by Siim
14:53
created

AbstractResponseListDto   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 125
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 36
dl 0
loc 125
rs 10
c 0
b 0
f 0
wmc 18

10 Methods

Rating   Name   Duplication   Size   Complexity  
A getFilter() 0 3 1
B populate() 0 25 7
A setCount() 0 3 1
A toArray() 0 10 3
A __construct() 0 4 1
A setFilter() 0 3 1
A addItem() 0 3 1
A setTotal() 0 3 1
A getTotal() 0 3 1
A getCount() 0 3 1
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\Traits\PaginationTrait;
14
15
abstract class AbstractResponseListDto extends AbstractResponseDto
16
{
17
    use PaginationTrait;
18
19
    /** @var ArrayCollection $items */
20
    protected $items;
21
22
    /** @var int $total */
23
    protected $total = 0;
24
25
    /** @var int $count */
26
    protected $count = 0;
27
28
    /** @var array $filter */
29
    protected $filter = [];
30
31
    public function __construct()
32
    {
33
        $this->items = new ArrayCollection();
34
        parent::__construct();
35
    }
36
37
    public abstract function getListItemClass(): string;
38
39
    /**
40
     * @param DtoInterface $dto
41
     */
42
    public function addItem(DtoInterface $dto)
43
    {
44
        $this->items->add($dto);
45
    }
46
47
    /**
48
     * @return array
49
     */
50
    public function toArray(): array
51
    {
52
        $data = parent::toArray();
53
        $data['items'] = [];
54
        foreach($this->items as $item) {
55
            if($item instanceof DtoInterface) {
56
                $data['items'][] = $item->toArray();
57
            }
58
        }
59
        return $data;
60
    }
61
62
    /**
63
     * @param array $data
64
     * @throws \ReflectionException
65
     */
66
    public function populate(array $data): void
67
    {
68
        $listItemClass = $this->getListItemClass();
69
        foreach($data as $item) {
70
            if(is_array($item)) {
71
                $dto = new $listItemClass();
72
                if($dto instanceof DtoInterface) {
73
                    $dto->populate($item);
74
                    $this->addItem($dto);
75
                }
76
            }
77
        }
78
79
        $this->setCount($this->items->count());
80
        if($this->getTotal() <= 0) {
81
            $this->setTotal($this->getCount());
82
        }
83
84
        $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

84
        $this->setTotalPages(/** @scrutinizer ignore-type */ ceil($this->getTotal() / $this->getItemsPerPage()));
Loading history...
85
86
        if($this->getTotalPages() > $this->getCurrentPage()) {
87
            $this->setNextPage($this->getCurrentPage() + 1);
88
        }
89
        if($this->getCurrentPage() > 1) {
90
            $this->setPreviousPage($this->getCurrentPage() - 1);
91
        }
92
    }
93
94
    /**
95
     * @return int
96
     */
97
    public function getTotal(): int
98
    {
99
        return $this->total;
100
    }
101
102
    /**
103
     * @param int $total
104
     */
105
    public function setTotal(int $total): void
106
    {
107
        $this->total = $total;
108
    }
109
110
    /**
111
     * @return int
112
     */
113
    public function getCount(): int
114
    {
115
        return $this->count;
116
    }
117
118
    /**
119
     * @param int $count
120
     */
121
    public function setCount(int $count): void
122
    {
123
        $this->count = $count;
124
    }
125
126
    /**
127
     * @return array
128
     */
129
    public function getFilter(): array
130
    {
131
        return $this->filter;
132
    }
133
134
    /**
135
     * @param array $filter
136
     */
137
    public function setFilter(array $filter): void
138
    {
139
        $this->filter = $filter;
140
    }
141
}
142