Locastic /
SyliusComparerPlugin
| 1 | <?php |
||
| 2 | |||
| 3 | declare(strict_types=1); |
||
| 4 | |||
| 5 | namespace Locastic\SyliusComparerPlugin\Entity; |
||
| 6 | |||
| 7 | use Doctrine\Common\Collections\ArrayCollection; |
||
| 8 | use Doctrine\Common\Collections\Collection; |
||
| 9 | use Sylius\Component\Core\Model\ProductInterface; |
||
| 10 | use Sylius\Component\Core\Model\ShopUserInterface; |
||
| 11 | |||
| 12 | class Comparer implements ComparerInterface |
||
| 13 | { |
||
| 14 | /** @var int */ |
||
| 15 | private $id; |
||
| 16 | |||
| 17 | /** @var ArrayCollection */ |
||
| 18 | private $comparerProducts; |
||
| 19 | |||
| 20 | /** @var ShopUserInterface */ |
||
| 21 | private $shopUser; |
||
| 22 | |||
| 23 | /** @var string|null */ |
||
| 24 | private $token; |
||
| 25 | |||
| 26 | public function __construct() |
||
| 27 | { |
||
| 28 | $this->comparerProducts = new ArrayCollection(); |
||
| 29 | } |
||
| 30 | |||
| 31 | public function getId(): ?int |
||
| 32 | { |
||
| 33 | return $this->id; |
||
| 34 | } |
||
| 35 | |||
| 36 | public function hasProduct(ProductInterface $product): bool |
||
| 37 | { |
||
| 38 | /** @var ArrayCollection $result */ |
||
| 39 | $result = $this->comparerProducts->filter(function (ComparerProductInterface $comparerProduct) use ($product) { |
||
| 40 | return $product === $comparerProduct->getProduct(); |
||
| 41 | }); |
||
| 42 | |||
| 43 | return !$result->isEmpty(); |
||
| 44 | } |
||
| 45 | |||
| 46 | public function getProducts(): Collection |
||
| 47 | { |
||
| 48 | /** @var ArrayCollection $products */ |
||
| 49 | $products = $this->comparerProducts->map(function (ComparerProductInterface $comparerProduct) { |
||
| 50 | return $comparerProduct->getProduct(); |
||
| 51 | }); |
||
| 52 | |||
| 53 | return $products; |
||
| 54 | } |
||
| 55 | |||
| 56 | public function hasComparerProduct(ComparerProductInterface $comparerProduct): bool |
||
| 57 | { |
||
| 58 | return $this->comparerProducts->contains($comparerProduct); |
||
| 59 | } |
||
| 60 | |||
| 61 | public function getComparerProduct(ProductInterface $product): ?ComparerProductInterface |
||
| 62 | { |
||
| 63 | /** @var ComparerProductInterface $comparerProduct */ |
||
| 64 | foreach ($this->getComparerProducts() as $comparerProduct) { |
||
| 65 | if ($comparerProduct->getProduct() === $product) { |
||
| 66 | return $comparerProduct; |
||
| 67 | } |
||
| 68 | } |
||
| 69 | |||
| 70 | return null; |
||
| 71 | } |
||
| 72 | |||
| 73 | public function getComparerProducts(): Collection |
||
| 74 | { |
||
| 75 | return $this->comparerProducts; |
||
| 76 | } |
||
| 77 | |||
| 78 | public function getNumberOfProductsInComparer(): int |
||
| 79 | { |
||
| 80 | return $this->getProducts()->count(); |
||
| 81 | } |
||
| 82 | |||
| 83 | public function addComparerProduct(ComparerProductInterface $comparerProduct): bool |
||
| 84 | { |
||
| 85 | if ($this->hasProduct($comparerProduct->getProduct())) { |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 86 | return false; |
||
| 87 | } |
||
| 88 | |||
| 89 | $comparerProduct->setComparer($this); |
||
| 90 | $this->comparerProducts->add($comparerProduct); |
||
| 91 | |||
| 92 | return true; |
||
| 93 | } |
||
| 94 | |||
| 95 | public function getComparerAttributes(Collection $products, string $locale, string $defaultLocale): ?Collection |
||
| 96 | { |
||
| 97 | $attributes = new ArrayCollection(); |
||
| 98 | /** @var ProductInterface $product */ |
||
| 99 | foreach ($products as $product) { |
||
| 100 | foreach ($product->getAttributesByLocale( |
||
| 101 | $locale, |
||
| 102 | $defaultLocale |
||
| 103 | ) as $attribute) { |
||
| 104 | if (!$attributes->contains($attribute->getName())) { |
||
| 105 | $attributes->add($attribute->getName()); |
||
| 106 | } |
||
| 107 | } |
||
| 108 | } |
||
| 109 | |||
| 110 | return $attributes; |
||
| 111 | } |
||
| 112 | |||
| 113 | public function getToken(): string |
||
| 114 | { |
||
| 115 | return (string) $this->token; |
||
| 116 | } |
||
| 117 | |||
| 118 | public function setToken(string $token): void |
||
| 119 | { |
||
| 120 | $this->token = $token; |
||
| 121 | } |
||
| 122 | |||
| 123 | public function getShopUser(): ShopUserInterface |
||
| 124 | { |
||
| 125 | return $this->shopUser; |
||
| 126 | } |
||
| 127 | |||
| 128 | public function setShopUser(ShopUserInterface $shopUser): void |
||
| 129 | { |
||
| 130 | $this->shopUser = $shopUser; |
||
| 131 | } |
||
| 132 | } |
||
| 133 |