Completed
Push — master ( 7d6b3f...2df636 )
by Fabian
14s queued 10s
created

AddressBuilder::withStreet()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 5
ccs 4
cts 4
cp 1
crap 1
rs 10
1
<?php
2
declare(strict_types=1);
3
4
namespace TddWizard\Fixtures\Customer;
5
6
use Faker\Factory as FakerFactory;
7
use InvalidArgumentException;
8
use Magento\Customer\Api\AddressRepositoryInterface;
9
use Magento\Customer\Api\Data\AddressInterface;
10
use Magento\Directory\Model\Region;
11
use Magento\Framework\Exception\LocalizedException;
12
use Magento\Framework\ObjectManagerInterface;
13
use Magento\TestFramework\Helper\Bootstrap;
0 ignored issues
show
Bug introduced by
The type Magento\TestFramework\Helper\Bootstrap was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
14
15
/**
16
 * Builder to be used by fixtures
17
 */
18
class AddressBuilder
19
{
20
    /**
21
     * @var AddressInterface
22
     */
23
    private $address;
24
25
    /**
26
     * @var AddressRepositoryInterface
27
     */
28
    private $addressRepository;
29
30 19
    public function __construct(AddressRepositoryInterface $addressRepository, AddressInterface $address)
31
    {
32 19
        $this->address = $address;
33 19
        $this->addressRepository = $addressRepository;
34 19
    }
35
36 19
    public function __clone()
37
    {
38 19
        $this->address = clone $this->address;
39 19
    }
40
41 18
    public static function anAddress(
42
        string $locale = 'de_DE'
43
    ): AddressBuilder {
44 18
        $objectManager = Bootstrap::getObjectManager();
45
46 18
        $address = self::prepareFakeAddress($objectManager, $locale);
47 18
        return new self($objectManager->create(AddressRepositoryInterface::class), $address);
48
    }
49
50 1
    public static function aCompanyAddress(
51
        string $locale = 'de_DE',
52
        string $vatId = '1234567890'
53
    ): AddressBuilder {
54 1
        $objectManager = Bootstrap::getObjectManager();
55
56 1
        $address = self::prepareFakeAddress($objectManager, $locale);
57 1
        $address->setVatId($vatId);
58 1
        return new self($objectManager->create(AddressRepositoryInterface::class), $address);
59
    }
60
61 18
    public function asDefaultShipping(): AddressBuilder
62
    {
63 18
        $builder = clone $this;
64 18
        $builder->address->setIsDefaultShipping(true);
65 18
        return $builder;
66
    }
67
68 19
    public function asDefaultBilling(): AddressBuilder
69
    {
70 19
        $builder = clone $this;
71 19
        $builder->address->setIsDefaultBilling(true);
72 19
        return $builder;
73
    }
74
75
    public function withPrefix(string $prefix): AddressBuilder
76
    {
77
        $builder = clone $this;
78
        $builder->address->setPrefix($prefix);
79
        return $builder;
80
    }
81
82 1
    public function withFirstname(string $firstname): AddressBuilder
83
    {
84 1
        $builder = clone $this;
85 1
        $builder->address->setFirstname($firstname);
86 1
        return $builder;
87
    }
88
89 1
    public function withLastname(string $lastname): AddressBuilder
90
    {
91 1
        $builder = clone $this;
92 1
        $builder->address->setLastname($lastname);
93 1
        return $builder;
94
    }
95
96 1
    public function withStreet(string $street): AddressBuilder
97
    {
98 1
        $builder = clone $this;
99 1
        $builder->address->setStreet((array)$street);
100 1
        return $builder;
101
    }
102
103 1
    public function withCompany(string $company): AddressBuilder
104
    {
105 1
        $builder = clone $this;
106 1
        $builder->address->setCompany($company);
107 1
        return $builder;
108
    }
109
110 1
    public function withTelephone(string $telephone): AddressBuilder
111
    {
112 1
        $builder = clone $this;
113 1
        $builder->address->setTelephone($telephone);
114 1
        return $builder;
115
    }
116
117 1
    public function withPostcode(string $postcode): AddressBuilder
118
    {
119 1
        $builder = clone $this;
120 1
        $builder->address->setPostcode($postcode);
121 1
        return $builder;
122
    }
123
124 1
    public function withCity(string $city): AddressBuilder
125
    {
126 1
        $builder = clone $this;
127 1
        $builder->address->setCity($city);
128 1
        return $builder;
129
    }
130
131 1
    public function withCountryId(string $countryId): AddressBuilder
132
    {
133 1
        $builder = clone $this;
134 1
        $builder->address->setCountryId($countryId);
135 1
        return $builder;
136
    }
137
138 1
    public function withRegionId(int $regionId): AddressBuilder
139
    {
140 1
        $builder = clone $this;
141 1
        $builder->address->setRegionId($regionId);
142 1
        return $builder;
143
    }
144
145
    /**
146
     * @param mixed[] $values
147
     * @return AddressBuilder
148
     */
149
    public function withCustomAttributes(array $values): AddressBuilder
150
    {
151
        $builder = clone $this;
152
        foreach ($values as $code => $value) {
153
            $builder->address->setCustomAttribute($code, $value);
154
        }
155
        return $builder;
156
    }
157
158
    /**
159
     * @return AddressInterface
160
     * @throws LocalizedException
161
     */
162
    public function build(): AddressInterface
163
    {
164
        return $this->addressRepository->save($this->address);
165
    }
166
167 19
    public function buildWithoutSave(): AddressInterface
168
    {
169 19
        return clone $this->address;
170
    }
171
172 19
    private static function prepareFakeAddress(
173
        ObjectManagerInterface $objectManager,
174
        string $locale = 'de_DE'
175
    ): AddressInterface {
176 19
        $faker = FakerFactory::create($locale);
177 19
        $countryCode = substr($locale, -2);
178
179
        try {
180 19
            $region = $faker->province;
181 19
        } catch (InvalidArgumentException $exception) {
0 ignored issues
show
Unused Code introduced by
catch (\InvalidArgumentException $exception) is not reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
182 19
            $region = $faker->state;
183
        }
184
185 19
        $regionId = $objectManager->create(Region::class)->loadByName($region, $countryCode)->getId();
186
187
        /** @var AddressInterface $address */
188 19
        $address = $objectManager->create(AddressInterface::class);
189
        $address
190 19
            ->setTelephone($faker->phoneNumber)
191 19
            ->setPostcode($faker->postcode)
192 19
            ->setCountryId($countryCode)
193 19
            ->setCity($faker->city)
194 19
            ->setCompany($faker->company)
195 19
            ->setStreet([$faker->streetAddress])
196 19
            ->setLastname($faker->lastName)
197 19
            ->setFirstname($faker->firstName)
198 19
            ->setRegionId($regionId);
199
200 19
        return $address;
201
    }
202
}
203