Completed
Push — remove-content-bundle ( 201341...8d07b3 )
by Kamil
52:29 queued 32:39
created

AddressContext   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 7
c 0
b 0
f 0
lcom 1
cbo 5
dl 0
loc 89
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A theirDefaultAddressIs() 0 4 1
A myDefaultAddressIsOf() 0 13 1
A iHaveAnAddressInAddressBook() 0 7 1
A thisCustomerHasAnAddressInAddressBook() 0 4 1
A addAddressToCustomer() 0 6 1
A setDefaultAddressOfCustomer() 0 6 1
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
namespace Sylius\Behat\Context\Setup;
13
14
use Behat\Behat\Context\Context;
15
use Doctrine\Common\Persistence\ObjectManager;
16
use Sylius\Component\Core\Model\AddressInterface;
17
use Sylius\Component\Core\Model\CustomerInterface;
18
use Sylius\Component\Core\Model\ShopUserInterface;
19
use Sylius\Component\Core\Repository\AddressRepositoryInterface;
20
use Webmozart\Assert\Assert;
21
22
/**
23
 * @author Jan Góralski <[email protected]>
24
 */
25
final class AddressContext implements Context
26
{
27
    /**
28
     * @var AddressRepositoryInterface
29
     */
30
    private $addressRepository;
31
32
    /**
33
     * @var ObjectManager
34
     */
35
    private $customerManager;
36
37
    /**
38
     * @param AddressRepositoryInterface $addressRepository
39
     * @param ObjectManager $customerManager
40
     */
41
    public function __construct(AddressRepositoryInterface $addressRepository, ObjectManager $customerManager)
42
    {
43
        $this->addressRepository = $addressRepository;
44
        $this->customerManager = $customerManager;
45
    }
46
47
    /**
48
     * @Given /^(their) default (address is "(?:[^"]+)", "(?:[^"]+)", "(?:[^"]+)", "(?:[^"]+)" for "(?:[^"]+)")$/
49
     * @Given /^(their) default (address is "([^"]+)", "([^"]+)", "([^"]+)", "([^"]+)", "([^"]+)", "([^"]+)")$/
50
     */
51
    public function theirDefaultAddressIs(CustomerInterface $customer, AddressInterface $address)
52
    {
53
        $this->setDefaultAddressOfCustomer($customer, $address);
54
    }
55
56
    /**
57
     * @Given /^(my) default address is of "([^"]+)"$/
58
     */
59
    public function myDefaultAddressIsOf(ShopUserInterface $user, $fullName)
60
    {
61
        list($firstName, $lastName) = explode(' ', $fullName);
62
63
        /** @var AddressInterface $address */
64
        $address = $this->addressRepository->findOneBy(['firstName' => $firstName, 'lastName' => $lastName]);
65
        Assert::notNull($address, sprintf('The address of "%s" has not been found.', $fullName));
66
67
        /** @var CustomerInterface $customer */
68
        $customer = $user->getCustomer();
69
70
        $this->setDefaultAddressOfCustomer($customer, $address);
71
    }
72
73
    /**
74
     * @Given /^(I) have an (address "[^"]+", "[^"]+", "[^"]+", "[^"]+", "[^"]+"(?:|, "[^"]+")) in my address book$/
75
     */
76
    public function iHaveAnAddressInAddressBook(ShopUserInterface $user, AddressInterface $address)
77
    {
78
        /** @var CustomerInterface $customer */
79
        $customer = $user->getCustomer();
80
81
        $this->addAddressToCustomer($customer, $address);
82
    }
83
84
    /**
85
     * @Given /^(this customer) has an (address "[^"]+", "[^"]+", "[^"]+", "[^"]+", "[^"]+"(?:|, "[^"]+")) in their address book$/
86
     */
87
    public function thisCustomerHasAnAddressInAddressBook(CustomerInterface $customer, AddressInterface $address)
88
    {
89
        $this->addAddressToCustomer($customer, $address);
90
    }
91
92
    /**
93
     * @param CustomerInterface $customer
94
     * @param AddressInterface $address
95
     */
96
    private function addAddressToCustomer(CustomerInterface $customer, AddressInterface $address)
97
    {
98
        $customer->addAddress($address);
99
100
        $this->customerManager->flush();
101
    }
102
103
    /**
104
     * @param CustomerInterface $customer
105
     * @param AddressInterface $address
106
     */
107
    private function setDefaultAddressOfCustomer(CustomerInterface $customer, AddressInterface $address)
108
    {
109
        $customer->setDefaultAddress($address);
110
111
        $this->customerManager->flush();
112
    }
113
}
114