| Total Complexity | 40 |
| Total Lines | 246 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like DeltaPager often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use DeltaPager, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 8 | final class DeltaPager implements PagerInterface |
||
| 9 | { |
||
| 10 | /** |
||
| 11 | * @var PagerInterface |
||
| 12 | */ |
||
| 13 | private $pager; |
||
| 14 | |||
| 15 | /** |
||
| 16 | * @var int |
||
| 17 | */ |
||
| 18 | private $delta; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * @var bool |
||
| 22 | */ |
||
| 23 | private $showFirstPage; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * @var bool |
||
| 27 | */ |
||
| 28 | private $showLastPage; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * DeltaPager constructor. |
||
| 32 | * @param PagerInterface $pager |
||
| 33 | * @param int $delta |
||
| 34 | */ |
||
| 35 | public function __construct(PagerInterface $pager, int $delta = 0, bool $showFirstPage = true, bool $showLastPage = true) |
||
| 41 | } |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @inheritDoc |
||
| 45 | */ |
||
| 46 | public function getCurrentPageNumber(): int |
||
| 47 | { |
||
| 48 | return $this->pager->getCurrentPageNumber(); |
||
| 49 | } |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @inheritDoc |
||
| 53 | */ |
||
| 54 | public function getIterator(): iterable |
||
| 55 | { |
||
| 56 | $pager = $this->pager; |
||
| 57 | foreach ($pager as $page) { |
||
| 58 | if ($this->isValid($page)) { |
||
| 59 | yield $page; |
||
| 60 | } |
||
| 61 | } |
||
| 62 | } |
||
| 63 | |||
| 64 | public function asArray(): array |
||
| 65 | { |
||
| 66 | return iterator_to_array($this); |
||
| 67 | } |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @param PageInterface $page |
||
| 71 | * @return bool |
||
| 72 | */ |
||
| 73 | private function isValid(PageInterface $page): bool |
||
| 74 | { |
||
| 75 | $pager = $this->pager; |
||
| 76 | $currentPageNumber = $pager->getCurrentPage()->getPageNumber(); |
||
| 77 | $leftBorder = $pager->getFirstPage()->getPageNumber() + ($this->delta * 2); |
||
| 78 | $rightBorder = $pager->getLastPage()->getPageNumber() - ($this->delta * 2); |
||
| 79 | switch (true) { |
||
| 80 | case true === $this->showFirstPage && $pager->isFirstPage($page): |
||
| 81 | case true === $this->showLastPage && $pager->isLastPage($page): |
||
| 82 | case true === $pager->isCurrentPage($page): |
||
| 83 | case $page->getPageNumber() < $currentPageNumber && $page->getPageNumber() >= ($currentPageNumber - $this->delta): |
||
| 84 | case $page->getPageNumber() > $currentPageNumber && $page->getPageNumber() <= ($currentPageNumber + $this->delta): |
||
| 85 | case $currentPageNumber <= $leftBorder && $page->getPageNumber() <= $leftBorder: |
||
| 86 | case $currentPageNumber >= $rightBorder && $page->getPageNumber() >= $rightBorder: |
||
| 87 | return true; |
||
| 88 | } |
||
| 89 | return false; |
||
| 90 | } |
||
| 91 | |||
| 92 | /** |
||
| 93 | * @inheritDoc |
||
| 94 | */ |
||
| 95 | public function setPerPage(int $perPage): PagerInterface |
||
| 96 | { |
||
| 97 | $this->pager->setPerPage($perPage); |
||
| 98 | return $this; |
||
| 99 | } |
||
| 100 | |||
| 101 | /** |
||
| 102 | * @inheritDoc |
||
| 103 | */ |
||
| 104 | public function setNumFound(int $numFound): PagerInterface |
||
| 108 | } |
||
| 109 | |||
| 110 | /** |
||
| 111 | * @inheritDoc |
||
| 112 | */ |
||
| 113 | public function setCurrentPageNumber(int $currentPageNumber): PagerInterface |
||
| 114 | { |
||
| 115 | $this->pager->setCurrentPageNumber($currentPageNumber); |
||
| 116 | return $this; |
||
| 117 | } |
||
| 118 | |||
| 119 | /** |
||
| 120 | * @inheritDoc |
||
| 121 | */ |
||
| 122 | public function getPerPage(): int |
||
| 123 | { |
||
| 124 | return $this->pager->getPerPage(); |
||
| 125 | } |
||
| 126 | |||
| 127 | /** |
||
| 128 | * @inheritDoc |
||
| 129 | */ |
||
| 130 | public function getNumFound(): int |
||
| 131 | { |
||
| 132 | return $this->pager->getNumFound(); |
||
| 133 | } |
||
| 134 | |||
| 135 | /** |
||
| 136 | * @inheritDoc |
||
| 137 | */ |
||
| 138 | public function getOffset(): int |
||
| 139 | { |
||
| 140 | return $this->pager->getOffset(); |
||
| 141 | } |
||
| 142 | |||
| 143 | /** |
||
| 144 | * @inheritDoc |
||
| 145 | */ |
||
| 146 | public function count(): int |
||
| 147 | { |
||
| 148 | return $this->pager->count(); |
||
| 149 | } |
||
| 150 | |||
| 151 | /** |
||
| 152 | * @inheritDoc |
||
| 153 | */ |
||
| 154 | public function getCurrentPage(): PageInterface |
||
| 155 | { |
||
| 156 | return $this->pager->getCurrentPage(); |
||
| 157 | } |
||
| 158 | |||
| 159 | /** |
||
| 160 | * @inheritDoc |
||
| 161 | */ |
||
| 162 | public function isCurrentPage(PageInterface $page): bool |
||
| 163 | { |
||
| 164 | return $this->pager->isCurrentPage($page); |
||
| 165 | } |
||
| 166 | |||
| 167 | /** |
||
| 168 | * @inheritDoc |
||
| 169 | */ |
||
| 170 | public function getPreviousPage(): ?PageInterface |
||
| 171 | { |
||
| 172 | return $this->pager->getPreviousPage(); |
||
| 173 | } |
||
| 174 | |||
| 175 | /** |
||
| 176 | * @inheritDoc |
||
| 177 | */ |
||
| 178 | public function isPreviousPage(PageInterface $page): bool |
||
| 179 | { |
||
| 180 | return $this->pager->isPreviousPage($page); |
||
| 181 | } |
||
| 182 | |||
| 183 | /** |
||
| 184 | * @inheritDoc |
||
| 185 | */ |
||
| 186 | public function getNextPage(): ?PageInterface |
||
| 187 | { |
||
| 188 | return $this->pager->getNextPage(); |
||
| 189 | } |
||
| 190 | |||
| 191 | /** |
||
| 192 | * @inheritDoc |
||
| 193 | */ |
||
| 194 | public function isNextPage(PageInterface $page): bool |
||
| 197 | } |
||
| 198 | |||
| 199 | /** |
||
| 200 | * @inheritDoc |
||
| 201 | */ |
||
| 202 | public function getFirstPage(): PageInterface |
||
| 205 | } |
||
| 206 | |||
| 207 | /** |
||
| 208 | * @inheritDoc |
||
| 209 | */ |
||
| 210 | public function isFirstPage(PageInterface $page): bool |
||
| 211 | { |
||
| 212 | return $this->pager->isFirstPage($page); |
||
| 213 | } |
||
| 214 | |||
| 215 | /** |
||
| 216 | * @inheritDoc |
||
| 217 | */ |
||
| 218 | public function getLastPage(): PageInterface |
||
| 219 | { |
||
| 220 | return $this->pager->getLastPage(); |
||
| 221 | } |
||
| 222 | |||
| 223 | /** |
||
| 224 | * @inheritDoc |
||
| 225 | */ |
||
| 226 | public function isLastPage(PageInterface $page): bool |
||
| 227 | { |
||
| 228 | return $this->pager->isLastPage($page); |
||
| 229 | } |
||
| 230 | |||
| 231 | /** |
||
| 232 | * @inheritDoc |
||
| 233 | */ |
||
| 234 | public function getPage(int $pageNumber): PageInterface |
||
| 237 | } |
||
| 238 | |||
| 239 | /** |
||
| 240 | * @inheritDoc |
||
| 241 | */ |
||
| 242 | public function getPageOffset(PageInterface $page): int |
||
| 245 | } |
||
| 246 | |||
| 247 | /** |
||
| 248 | * @param PageInterface $page |
||
| 249 | * @return string |
||
| 250 | */ |
||
| 251 | public function getUrl(PageInterface $page): string |
||
| 252 | { |
||
| 253 | return $this->pager->getUrl($page); |
||
| 254 | } |
||
| 255 | } |
||
| 256 |