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

AvailableProductOptionValuesResolver::resolve()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 24
rs 9.536
c 0
b 0
f 0
cc 4
nc 2
nop 2
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