ShippingMethodsViewFactory   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A createList() 0 10 2
A createFromShippingMethod() 0 22 1
1
<?php
2
3
/*
4
 * This file has been created by developers from BitBag.
5
 * Feel free to contact us once you face any issues or want to start
6
 * another great project.
7
 * You can find more information about us on https://bitbag.io and write us
8
 * an email on [email protected].
9
 */
10
11
declare(strict_types=1);
12
13
namespace BitBag\SyliusVueStorefrontPlugin\Factory\Cart;
14
15
use BitBag\SyliusVueStorefrontPlugin\Sylius\Provider\ChannelProviderInterface;
16
use BitBag\SyliusVueStorefrontPlugin\View\Cart\ShippingMethodsView;
17
use Sylius\Component\Core\Model\ShippingMethodInterface;
18
19
final class ShippingMethodsViewFactory implements ShippingMethodsViewFactoryInterface
20
{
21
    /** @var string */
22
    private $shippingMethodsViewClass;
23
24
    /** @var ChannelProviderInterface */
25
    private $channelProvider;
26
27
    public function __construct(string $shippingMethodsViewClass, ChannelProviderInterface $channelProvider)
28
    {
29
        $this->shippingMethodsViewClass = $shippingMethodsViewClass;
30
        $this->channelProvider = $channelProvider;
31
    }
32
33
    public function createList(ShippingMethodInterface ...$shippingMethods): array
34
    {
35
        $shippingMethodList = [];
36
37
        foreach ($shippingMethods as $shippingMethod) {
38
            $shippingMethodList[] = $this->createFromShippingMethod($shippingMethod);
39
        }
40
41
        return $shippingMethodList;
42
    }
43
44
    private function createFromShippingMethod(ShippingMethodInterface $shippingMethod): ShippingMethodsView
45
    {
46
        /** @var ShippingMethodsView $shippingMethodsView */
47
        $shippingMethodsView = new $this->shippingMethodsViewClass();
48
        $shippingMethodsView->carrier_code = $shippingMethod->getCode();
49
        $shippingMethodsView->method_code = $shippingMethod->getCode();
50
        $shippingMethodsView->carrier_title = $shippingMethod->getTranslation()->getName();
51
        $shippingMethodsView->method_title = $shippingMethod->getTranslation()->getName();
52
53
        $configuration = $shippingMethod->getConfiguration();
54
        $channelCode = $this->channelProvider->provide()->getCode();
55
56
        $shippingMethodsView->amount = (int) $configuration[$channelCode]['amount'];
57
        $shippingMethodsView->base_amount = (int) $configuration[$channelCode]['amount'];
58
59
        $shippingMethodsView->available = $shippingMethod->isEnabled();
60
        $shippingMethodsView->error_message = '';
61
        $shippingMethodsView->price_excl_tax = 0;
62
        $shippingMethodsView->price_incl_tax = 0;
63
64
        return $shippingMethodsView;
65
    }
66
}
67