Comparer::getNumberOfProductsInComparer()   A
last analyzed

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
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
It seems like $comparerProduct->getProduct() can also be of type null; however, parameter $product of Locastic\SyliusComparerP...\Comparer::hasProduct() does only seem to accept Sylius\Component\Core\Model\ProductInterface, maybe add an additional type check? ( Ignorable by Annotation )

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

85
        if ($this->hasProduct(/** @scrutinizer ignore-type */ $comparerProduct->getProduct())) {
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