ItemCodeResolver   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 5
eloc 12
c 1
b 0
f 1
dl 0
loc 27
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A resolve() 0 17 4
A __construct() 0 3 1
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