Completed
Pull Request — master (#50)
by Fabian
41:36
created

AddressBuilder::aCompanyAddress()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 5
nc 2
nop 3
dl 0
loc 12
ccs 7
cts 7
cp 1
crap 2
rs 10
c 1
b 0
f 0
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 18
29
    public function __construct(AddressRepositoryInterface $addressRepository, AddressInterface $address)
30 18
    {
31 18
        $this->address = $address;
32 18
        $this->addressRepository = $addressRepository;
33
    }
34 18
35
    public function __clone()
36 18
    {
37 18
        $this->address = clone $this->address;
38
    }
39 18
40
    public static function anAddress(
41
        string $locale = 'de_DE'
42 18
    ): AddressBuilder {
43 18
        $objectManager = Bootstrap::getObjectManager();
44 18
45
        $address = self::prepareFakeAddress($objectManager, $locale);
46
        return new self($objectManager->create(AddressRepositoryInterface::class), $address);
47 18
    }
48 18
49 18
    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 18
        string $vatId = '1234567890'
53
    ): AddressBuilder {
54
        if ($objectManager === null) {
55 18
            $objectManager = Bootstrap::getObjectManager();
56
        }
57 18
58 18
        $address = self::prepareFakeAddress($objectManager, $locale);
59 18
        $address->setVatId($vatId);
60 18
        return new self($objectManager->create(AddressRepositoryInterface::class), $address);
61 18
    }
62 18
63 18
    public function asDefaultShipping(): AddressBuilder
64 18
    {
65 18
        $builder = clone $this;
66
        $builder->address->setIsDefaultShipping(true);
67 18
        return $builder;
68
    }
69
70 18
    public function asDefaultBilling(): AddressBuilder
71
    {
72 18
        $builder = clone $this;
73 18
        $builder->address->setIsDefaultBilling(true);
74 18
        return $builder;
75
    }
76
77 18
    public function withPrefix(string $prefix): AddressBuilder
78
    {
79 18
        $builder = clone $this;
80 18
        $builder->address->setPrefix($prefix);
81 18
        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 1
    public function withLastname(string $lastname): AddressBuilder
92
    {
93 1
        $builder = clone $this;
94 1
        $builder->address->setLastname($lastname);
95 1
        return $builder;
96
    }
97
98 1
    public function withStreet(string $street): AddressBuilder
99
    {
100 1
        $builder = clone $this;
101 1
        $builder->address->setStreet((array)$street);
102 1
        return $builder;
103
    }
104
105 1
    public function withCompany(string $company): AddressBuilder
106
    {
107 1
        $builder = clone $this;
108 1
        $builder->address->setCompany($company);
109 1
        return $builder;
110
    }
111
112 1
    public function withTelephone(string $telephone): AddressBuilder
113
    {
114 1
        $builder = clone $this;
115 1
        $builder->address->setTelephone($telephone);
116 1
        return $builder;
117
    }
118
119 1
    public function withPostcode(string $postcode): AddressBuilder
120
    {
121 1
        $builder = clone $this;
122 1
        $builder->address->setPostcode($postcode);
123 1
        return $builder;
124
    }
125
126 1
    public function withCity(string $city): AddressBuilder
127
    {
128 1
        $builder = clone $this;
129 1
        $builder->address->setCity($city);
130 1
        return $builder;
131
    }
132
133 1
    public function withCountryId(string $countryId): AddressBuilder
134
    {
135 1
        $builder = clone $this;
136 1
        $builder->address->setCountryId($countryId);
137 1
        return $builder;
138
    }
139
140 1
    public function withRegionId(int $regionId): AddressBuilder
141
    {
142 1
        $builder = clone $this;
143 1
        $builder->address->setRegionId($regionId);
144 1
        return $builder;
145
    }
146
147 1
    /**
148
     * @param mixed[] $values
149 1
     * @return AddressBuilder
150 1
     */
151 1
    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 18
        string $locale = 'de_DE'
177
    ): AddressInterface {
178 18
        $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