Completed
Push — master ( 860366...55daec )
by Kamil
19:37
created

UserContext::myDefaultShippingAddressIs()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 8
rs 9.4285
cc 1
eloc 5
nc 1
nop 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\Addressing\Model\AddressInterface;
17
use Sylius\Component\Core\Test\Factory\TestUserFactoryInterface;
18
use Sylius\Component\Core\Test\Services\SharedStorageInterface;
19
use Sylius\Component\Resource\Factory\FactoryInterface;
20
use Sylius\Component\Resource\Repository\RepositoryInterface;
21
use Symfony\Component\Intl\Intl;
22
23
/**
24
 * @author Arkadiusz Krakowiak <[email protected]>
25
 */
26
final class UserContext implements Context
27
{
28
    /**
29
     * @var RepositoryInterface
30
     */
31
    private $userRepository;
32
33
    /**
34
     * @var SharedStorageInterface
35
     */
36
    private $sharedStorage;
37
38
    /**
39
     * @var TestUserFactoryInterface
40
     */
41
    private $userFactory;
42
43
    /**
44
     * @var FactoryInterface
45
     */
46
    private $addressFactory;
47
48
    /**
49
     * @var ObjectManager
50
     */
51
    private $userManager;
52
53
    /**
54
     * @param RepositoryInterface $userRepository
55
     * @param SharedStorageInterface $sharedStorage
56
     * @param TestUserFactoryInterface $userFactory
57
     * @param FactoryInterface $addressFactory
58
     * @param ObjectManager $userManager
59
     */
60
    public function __construct(
61
        RepositoryInterface $userRepository,
62
        SharedStorageInterface $sharedStorage,
63
        TestUserFactoryInterface $userFactory,
64
        FactoryInterface $addressFactory,
65
        ObjectManager $userManager
66
    ) {
67
        $this->userRepository = $userRepository;
68
        $this->sharedStorage = $sharedStorage;
69
        $this->userFactory = $userFactory;
70
        $this->addressFactory = $addressFactory;
71
        $this->userManager = $userManager;
72
    }
73
74
    /**
75
     * @Given there is user :email identified by :password
76
     */
77
    public function thereIsUserIdentifiedBy($email, $password)
78
    {
79
        $user = $this->userFactory->create($email, $password);
80
81
        $this->sharedStorage->setCurrentResource('user', $user);
82
        $this->userRepository->add($user);
83
    }
84
85
    /**
86
     * @Given there is user :email identified by :password, with :country as shipping country
87
     */
88
    public function thereIsUserWithShippingCountry($email, $password, $country)
89
    {
90
        $user = $this->userFactory->create($email, $password);
91
92
        $customer = $user->getCustomer();
93
        $customer->setShippingAddress($this->createAddress($customer->getFirstName(), $customer->getLastName(), $country));
94
95
        $this->sharedStorage->setCurrentResource('user', $user);
96
        $this->userRepository->add($user);
97
    }
98
99
    /**
100
     * @Given my default shipping address is :country
101
     */
102
    public function myDefaultShippingAddressIs($country)
103
    {
104
        $user = $this->sharedStorage->getCurrentResource('user');
105
        $customer = $user->getCustomer();
106
        $customer->setShippingAddress($this->createAddress($customer->getFirstName(), $customer->getLastName(), $country));
107
108
        $this->userManager->flush();
109
    }
110
111
    /**
112
     * @param string $firstName
113
     * @param string $lastName
114
     * @param string $country
115
     * @param string $street
116
     * @param string $city
117
     * @param string $postcode
118
     *
119
     * @return AddressInterface
120
     */
121
    private function createAddress(
122
        $firstName,
123
        $lastName,
124
        $country = 'United States',
125
        $street = 'Jones St. 114',
126
        $city = 'Paradise City',
127
        $postcode = '99999'
128
    ) {
129
        $address = $this->addressFactory->createNew();
130
        $address->setFirstName($firstName);
131
        $address->setLastName($lastName);
132
        $address->setStreet($street);
133
        $address->setCity($city);
134
        $address->setPostcode($postcode);
135
        $address->setCountryCode($this->getCountryCodeByEnglishCountryName($country));
136
137
        return $address;
138
    }
139
140
    /**
141
     * @param string $name
142
     *
143
     * @return string
144
     *
145
     * @throws \InvalidArgumentException If name is not found in country code registry.
146
     */
147
    private function getCountryCodeByEnglishCountryName($name)
148
    {
149
        $names = Intl::getRegionBundle()->getCountryNames('en');
150
        $countryCode = array_search(trim($name), $names);
151
152
        if (null === $countryCode) {
153
            throw new \InvalidArgumentException(sprintf(
154
                'Country "%s" not found! Available names: %s.', $name, implode(', ', $names)
155
            ));
156
        }
157
158
        return $countryCode;
159
    }
160
}
161