UserProfileViewFactory   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A create() 0 24 3
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\User;
14
15
use BitBag\SyliusVueStorefrontPlugin\Factory\Common\AddressViewFactoryInterface;
16
use BitBag\SyliusVueStorefrontPlugin\Helper\DateHelper;
17
use BitBag\SyliusVueStorefrontPlugin\Sylius\Provider\ChannelProviderInterface;
18
use BitBag\SyliusVueStorefrontPlugin\View\User\UserProfileView;
19
use Sylius\Component\Core\Model\CustomerInterface as SyliusCustomerInterface;
20
21
final class UserProfileViewFactory implements UserProfileViewFactoryInterface
22
{
23
    /** @var string */
24
    private $userProfileViewClass;
25
26
    /** @var AddressViewFactoryInterface */
27
    private $addressViewFactory;
28
29
    /** @var ChannelProviderInterface */
30
    private $channelProvider;
31
32
    public function __construct(
33
        string $userProfileViewClass,
34
        AddressViewFactoryInterface $addressViewFactory,
35
        ChannelProviderInterface $channelProvider
36
    ) {
37
        $this->userProfileViewClass = $userProfileViewClass;
38
        $this->addressViewFactory = $addressViewFactory;
39
        $this->channelProvider = $channelProvider;
40
    }
41
42
    public function create(SyliusCustomerInterface $syliusCustomer): UserProfileView
43
    {
44
        /** @var UserProfileView $userProfileView */
45
        $userProfileView = new $this->userProfileViewClass();
46
        $userProfileView->id = $syliusCustomer->getId();
47
        $userProfileView->group_id = 1;
48
        $userProfileView->default_shipping = $syliusCustomer->getDefaultAddress() ? (string) $syliusCustomer->getDefaultAddress()->getId() : '';
49
        $userProfileView->created_at = $syliusCustomer->getCreatedAt()->format(DateHelper::DATE_TIME_FORMAT);
50
        $userProfileView->updated_at = $syliusCustomer->getUpdatedAt()->format(DateHelper::DATE_TIME_FORMAT);
51
        $userProfileView->created_in = $this->channelProvider->provide()->getName();
52
        $userProfileView->email = $syliusCustomer->getEmail();
53
        $userProfileView->firstname = $syliusCustomer->getFirstName();
54
        $userProfileView->lastname = $syliusCustomer->getLastName();
55
        $userProfileView->store_id = 1;
56
        $userProfileView->website_id = 1;
57
        $userProfileView->addresses = [];
58
59
        foreach ($syliusCustomer->getAddresses() as $address) {
60
            $userProfileView->addresses[] = $this->addressViewFactory->create($address);
61
        }
62
        $userProfileView->disable_auto_group_change = 0;
63
64
        return $userProfileView;
65
    }
66
}
67