Completed
Push — master ( ab9bf2...7d6b3f )
by Fabian
09:56 queued 01:25
created

AddressBuilder::aCompanyAddress()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 5
c 1
b 0
f 0
nc 2
nop 3
dl 0
loc 12
ccs 0
cts 6
cp 0
crap 6
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\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...
13
14
/**
15
 * Builder to be used by fixtures
16
 */
17
class AddressBuilder
18
{
19
    /**
20
     * @var AddressInterface
21
     */
22
    private $address;
23
24
    /**
25
     * @var AddressRepositoryInterface
26
     */
27
    private $addressRepository;
28
29
    public function __construct(AddressRepositoryInterface $addressRepository, AddressInterface $address)
30
    {
31
        $this->address = $address;
32
        $this->addressRepository = $addressRepository;
33
    }
34
35
    public function __clone()
36
    {
37
        $this->address = clone $this->address;
38
    }
39
40
    public static function anAddress(
41
        string $locale = 'de_DE'
42
    ): AddressBuilder {
43
        $objectManager = Bootstrap::getObjectManager();
44
45
        $address = self::prepareFakeAddress($objectManager, $locale);
46
        return new self($objectManager->create(AddressRepositoryInterface::class), $address);
47
    }
48
49
    public static function aCompanyAddress(
50
        ObjectManagerInterface $objectManager = null,
0 ignored issues
show
Bug introduced by
The type TddWizard\Fixtures\Customer\ObjectManagerInterface 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...
51
        string $locale = 'de_DE',
52
        string $vatId = '1234567890'
53
    ): AddressBuilder {
54
        if ($objectManager === null) {
55
            $objectManager = Bootstrap::getObjectManager();
56
        }
57
58
        $address = self::prepareFakeAddress($objectManager, $locale);
59
        $address->setVatId($vatId);
60
        return new self($objectManager->create(AddressRepositoryInterface::class), $address);
61
    }
62
63
    public function asDefaultShipping(): AddressBuilder
64
    {
65
        $builder = clone $this;
66
        $builder->address->setIsDefaultShipping(true);
67
        return $builder;
68
    }
69
70
    public function asDefaultBilling(): AddressBuilder
71
    {
72
        $builder = clone $this;
73
        $builder->address->setIsDefaultBilling(true);
74
        return $builder;
75
    }
76
77
    public function withPrefix(string $prefix): AddressBuilder
78
    {
79
        $builder = clone $this;
80
        $builder->address->setPrefix($prefix);
81
        return $builder;
82
    }
83
84
    public function withFirstname(string $firstname): AddressBuilder
85
    {
86
        $builder = clone $this;
87
        $builder->address->setFirstname($firstname);
88
        return $builder;
89
    }
90
91
    public function withLastname(string $lastname): AddressBuilder
92
    {
93
        $builder = clone $this;
94
        $builder->address->setLastname($lastname);
95
        return $builder;
96
    }
97
98
    public function withStreet(string $street): AddressBuilder
99
    {
100
        $builder = clone $this;
101
        $builder->address->setStreet((array)$street);
102
        return $builder;
103
    }
104
105
    public function withCompany(string $company): AddressBuilder
106
    {
107
        $builder = clone $this;
108
        $builder->address->setCompany($company);
109
        return $builder;
110
    }
111
112
    public function withTelephone(string $telephone): AddressBuilder
113
    {
114
        $builder = clone $this;
115
        $builder->address->setTelephone($telephone);
116
        return $builder;
117
    }
118
119
    public function withPostcode(string $postcode): AddressBuilder
120
    {
121
        $builder = clone $this;
122
        $builder->address->setPostcode($postcode);
123
        return $builder;
124
    }
125
126
    public function withCity(string $city): AddressBuilder
127
    {
128
        $builder = clone $this;
129
        $builder->address->setCity($city);
130
        return $builder;
131
    }
132
133
    public function withCountryId(string $countryId): AddressBuilder
134
    {
135
        $builder = clone $this;
136
        $builder->address->setCountryId($countryId);
137
        return $builder;
138
    }
139
140
    public function withRegionId(int $regionId): AddressBuilder
141
    {
142
        $builder = clone $this;
143
        $builder->address->setRegionId($regionId);
144
        return $builder;
145
    }
146
147
    /**
148
     * @param mixed[] $values
149
     * @return AddressBuilder
150
     */
151
    public function withCustomAttributes(array $values): AddressBuilder
152
    {
153
        $builder = clone $this;
154
        foreach ($values as $code => $value) {
155
            $builder->address->setCustomAttribute($code, $value);
156
        }
157
        return $builder;
158
    }
159
160
    /**
161
     * @return AddressInterface
162
     * @throws LocalizedException
163
     */
164
    public function build(): AddressInterface
165
    {
166
        return $this->addressRepository->save($this->address);
167
    }
168
169
    public function buildWithoutSave(): AddressInterface
170
    {
171
        return clone $this->address;
172
    }
173
174
    private static function prepareFakeAddress(
175
        ObjectManagerInterface $objectManager,
176
        string $locale = 'de_DE'
177
    ): AddressInterface {
178
        $faker = FakerFactory::create($locale);
179
        $countryCode = substr($locale, -2);
180
181
        try {
182
            $region = $faker->province;
183
        } 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...
184
            $region = $faker->state;
185
        }
186
187
        $regionId = $objectManager->create(Region::class)->loadByName($region, $countryCode)->getId();
188
189
        /** @var AddressInterface $address */
190
        $address = $objectManager->create(AddressInterface::class);
191
        $address
192
            ->setTelephone($faker->phoneNumber)
193
            ->setPostcode($faker->postcode)
194
            ->setCountryId($countryCode)
195
            ->setCity($faker->city)
196
            ->setCompany($faker->company)
197
            ->setStreet([$faker->streetAddress])
198
            ->setLastname($faker->lastName)
199
            ->setFirstname($faker->firstName)
200
            ->setRegionId($regionId);
201
202
        return $address;
203
    }
204
}
205