Completed
Push — master ( 938e21...ab9bf2 )
by Fabian
25s
created

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