ItemCodeResolver::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 1
c 1
b 0
f 1
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Setono\SyliusStrandsPlugin\Resolver;
6
7
use Sylius\Component\Core\Model\OrderItemInterface;
8
9
final class ItemCodeResolver implements ItemCodeResolverInterface
10
{
11
    /** @var bool */
12
    private $variantBased;
13
14
    public function __construct(bool $variantBased)
15
    {
16
        $this->variantBased = $variantBased;
17
    }
18
19
    public function resolve(OrderItemInterface $item): string
20
    {
21
        if ($this->variantBased) {
22
            $variant = $item->getVariant();
23
            if (null === $variant) {
24
                return '';
25
            }
26
27
            return (string) $variant->getCode();
28
        }
29
30
        $product = $item->getProduct();
31
        if (null === $product) {
32
            return '';
33
        }
34
35
        return (string) $product->getCode();
36
    }
37
}
38