AddressBuilder::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 4
ccs 3
cts 3
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 20
    public function __construct(AddressRepositoryInterface $addressRepository, AddressInterface $address)
31
    {
32 20
        $this->address = $address;
33 20
        $this->addressRepository = $addressRepository;
34 20
    }
35
36 20
    public function __clone()
37
    {
38 20
        $this->address = clone $this->address;
39 20
    }
40
41 19
    public static function anAddress(
42
        string $locale = 'de_DE'
43
    ): AddressBuilder {
44 19
        $objectManager = Bootstrap::getObjectManager();
45
46 19
        $address = self::prepareFakeAddress($objectManager, $locale);
47 19
        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 19
    public function asDefaultShipping(): AddressBuilder
62
    {
63 19
        $builder = clone $this;
64 19
        $builder->address->setIsDefaultShipping(true);
65 19
        return $builder;
66
    }
67
68 20
    public function asDefaultBilling(): AddressBuilder
69
    {
70 20
        $builder = clone $this;
71 20
        $builder->address->setIsDefaultBilling(true);
72 20
        return $builder;
73
    }
74
75 1
    public function withPrefix(string $prefix): AddressBuilder
76
    {
77 1
        $builder = clone $this;
78 1
        $builder->address->setPrefix($prefix);
79 1
        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 withMiddlename(string $middlename): AddressBuilder
90
    {
91 1
        $builder = clone $this;
92 1
        $builder->address->setMiddlename($middlename);
93 1
        return $builder;
94
    }
95
96 1
    public function withLastname(string $lastname): AddressBuilder
97
    {
98 1
        $builder = clone $this;
99 1
        $builder->address->setLastname($lastname);
100 1
        return $builder;
101
    }
102
103 1
    public function withSuffix(string $suffix): AddressBuilder
104
    {
105 1
        $builder = clone $this;
106 1
        $builder->address->setSuffix($suffix);
107 1
        return $builder;
108
    }
109
110 1
    public function withStreet(string $street): AddressBuilder
111
    {
112 1
        $builder = clone $this;
113 1
        $builder->address->setStreet((array)$street);
114 1
        return $builder;
115
    }
116
117 1
    public function withCompany(string $company): AddressBuilder
118
    {
119 1
        $builder = clone $this;
120 1
        $builder->address->setCompany($company);
121 1
        return $builder;
122
    }
123
124 1
    public function withTelephone(string $telephone): AddressBuilder
125
    {
126 1
        $builder = clone $this;
127 1
        $builder->address->setTelephone($telephone);
128 1
        return $builder;
129
    }
130
131 1
    public function withPostcode(string $postcode): AddressBuilder
132
    {
133 1
        $builder = clone $this;
134 1
        $builder->address->setPostcode($postcode);
135 1
        return $builder;
136
    }
137
138 1
    public function withCity(string $city): AddressBuilder
139
    {
140 1
        $builder = clone $this;
141 1
        $builder->address->setCity($city);
142 1
        return $builder;
143
    }
144
145 1
    public function withCountryId(string $countryId): AddressBuilder
146
    {
147 1
        $builder = clone $this;
148 1
        $builder->address->setCountryId($countryId);
149 1
        return $builder;
150
    }
151
152 1
    public function withRegionId(int $regionId): AddressBuilder
153
    {
154 1
        $builder = clone $this;
155 1
        $builder->address->setRegionId($regionId);
156 1
        return $builder;
157
    }
158
159
    /**
160
     * @param mixed[] $values
161
     * @return AddressBuilder
162
     */
163
    public function withCustomAttributes(array $values): AddressBuilder
164
    {
165
        $builder = clone $this;
166
        foreach ($values as $code => $value) {
167
            $builder->address->setCustomAttribute($code, $value);
168
        }
169
        return $builder;
170
    }
171
172
    /**
173
     * @return AddressInterface
174
     * @throws LocalizedException
175
     */
176
    public function build(): AddressInterface
177
    {
178
        return $this->addressRepository->save($this->address);
179
    }
180
181 20
    public function buildWithoutSave(): AddressInterface
182
    {
183 20
        return clone $this->address;
184
    }
185
186 20
    private static function prepareFakeAddress(
187
        ObjectManagerInterface $objectManager,
188
        string $locale = 'de_DE'
189
    ): AddressInterface {
190 20
        $faker = FakerFactory::create($locale);
191 20
        $countryCode = substr($locale, -2);
192
193
        try {
194 20
            $region = $faker->province;
195 20
        } 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...
196 20
            $region = $faker->state;
197
        }
198
199 20
        $regionId = $objectManager->create(Region::class)->loadByName($region, $countryCode)->getId();
200
201
        /** @var AddressInterface $address */
202 20
        $address = $objectManager->create(AddressInterface::class);
203
        $address
204 20
            ->setTelephone($faker->phoneNumber)
205 20
            ->setPostcode($faker->postcode)
206 20
            ->setCountryId($countryCode)
207 20
            ->setCity($faker->city)
208 20
            ->setCompany($faker->company)
209 20
            ->setStreet([$faker->streetAddress])
210 20
            ->setLastname($faker->lastName)
211 20
            ->setFirstname($faker->firstName)
212 20
            ->setRegionId($regionId);
213
214 20
        return $address;
215
    }
216
}
217