| Total Complexity | 15 |
| Total Lines | 75 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
| 1 | <?php declare(strict_types=1); |
||
| 11 | final class Riverbank |
||
| 12 | { |
||
| 13 | /** @var bool */ |
||
| 14 | private $isStart; |
||
| 15 | /** @var Purchase[] */ |
||
| 16 | private $purchases; |
||
| 17 | |||
| 18 | public function __construct(bool $isStart, Purchase ...$purchases) |
||
| 19 | { |
||
| 20 | $this->isStart = $isStart; |
||
| 21 | $this->purchases = $purchases; |
||
| 22 | } |
||
| 23 | |||
| 24 | /** @return Purchase[] */ |
||
| 25 | public function purchases(): array |
||
| 26 | { |
||
| 27 | return $this->purchases; |
||
| 28 | } |
||
| 29 | |||
| 30 | public function isStart(): bool |
||
| 31 | { |
||
| 32 | return $this->isStart; |
||
| 33 | } |
||
| 34 | |||
| 35 | public function isGoal(): bool |
||
| 36 | { |
||
| 37 | return !$this->isStart; |
||
| 38 | } |
||
| 39 | |||
| 40 | public function isFull(): bool |
||
| 43 | } |
||
| 44 | |||
| 45 | public function canTakeAway(?Purchase $bringAlong): bool |
||
| 46 | { |
||
| 47 | $remaining = array_diff($this->purchases, [$bringAlong]); |
||
| 48 | foreach ($remaining as $theOne) { |
||
| 49 | foreach ($remaining as $theOther) { |
||
| 50 | if ($theOne->eats($theOther)) { |
||
| 51 | return false; |
||
| 52 | } |
||
| 53 | } |
||
| 54 | } |
||
| 55 | return true; |
||
| 56 | } |
||
| 57 | |||
| 58 | public function with(?Purchase $purchase): self |
||
| 59 | { |
||
| 60 | if (!$purchase) { |
||
| 61 | return $this; |
||
| 62 | } |
||
| 63 | return new self( |
||
| 64 | $this->isStart, |
||
| 65 | ...array_merge($this->purchases, [$purchase]) |
||
| 66 | ); |
||
| 67 | } |
||
| 68 | |||
| 69 | public function without(?Purchase $purchase): self |
||
| 70 | { |
||
| 71 | if (!$purchase) { |
||
| 72 | return $this; |
||
| 73 | } |
||
| 74 | return new self( |
||
| 75 | $this->isStart, |
||
| 76 | ...array_diff($this->purchases, [$purchase]) |
||
| 77 | ); |
||
| 78 | } |
||
| 79 | |||
| 80 | public function __toString(): string |
||
| 86 | ); |
||
| 87 | } |
||
| 88 | } |
||
| 89 |