Completed
Push — master ( afd133...1df073 )
by Kamil
05:39 queued 11s
created

AvailableProductOptionValuesResolver   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 2
dl 0
loc 30
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A resolve() 0 24 4
1
<?php
2
3
/*
4
 * This file is part of the Sylius package.
5
 *
6
 * (c) Paweł Jędrzejewski
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace Sylius\Component\Product\Resolver;
15
16
use Doctrine\Common\Collections\Collection;
17
use Sylius\Component\Product\Model\ProductInterface;
18
use Sylius\Component\Product\Model\ProductOptionInterface;
19
use Sylius\Component\Product\Model\ProductOptionValueInterface;
20
21
final class AvailableProductOptionValuesResolver implements AvailableProductOptionValuesResolverInterface
22
{
23
    /**
24
     * {@inheritdoc}
25
     */
26
    public function resolve(ProductInterface $product, ProductOptionInterface $productOption): Collection
27
    {
28
        if (!$product->hasOption($productOption)) {
29
            throw new \InvalidArgumentException(
30
                sprintf(
31
                    'Cannot resolve available product option values. Option "%s" does not belong to product "%s".',
32
                    $product->getCode(),
33
                    $productOption->getCode()
34
                )
35
            );
36
        }
37
38
        return $productOption->getValues()->filter(
39
            static function (ProductOptionValueInterface $productOptionValue) use ($product) {
40
                foreach ($product->getEnabledVariants() as $productVariant) {
41
                    if ($productVariant->hasOptionValue($productOptionValue)) {
42
                        return true;
43
                    }
44
                }
45
46
                return false;
47
            }
48
        );
49
    }
50
}
51