Passed
Pull Request — master (#52)
by Fabian
19:55
created

CustomerFixturePool::get()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 5
c 1
b 0
f 0
nc 4
nop 1
dl 0
loc 9
ccs 6
cts 6
cp 1
crap 3
rs 10
1
<?php
2
declare(strict_types=1);
3
4
namespace TddWizard\Fixtures\Customer;
5
6
use Magento\Customer\Api\Data\CustomerInterface;
7
8
class CustomerFixturePool
9
{
10
11
    /**
12
     * @var CustomerFixture[]
13
     */
14
    private $customerFixtures = [];
15
16 5
    public function add(CustomerInterface $customer, string $key = null): void
17
    {
18 5
        if ($key === null) {
19 3
            $this->customerFixtures[] = new CustomerFixture($customer);
20
        } else {
21 2
            $this->customerFixtures[$key] = new CustomerFixture($customer);
22
        }
23 5
    }
24
25
    /**
26
     * Returns customer fixture by key, or last added if key not specified
27
     *
28
     * @param string|null $key
29
     * @return CustomerFixture
30
     */
31 5
    public function get(string $key = null): CustomerFixture
32
    {
33 5
        if ($key === null) {
34 3
            $key = \array_key_last($this->customerFixtures);
35
        }
36 5
        if (!array_key_exists($key, $this->customerFixtures)) {
37 3
            throw new \OutOfBoundsException('No matching customer found in fixture pool');
38
        }
39 2
        return $this->customerFixtures[$key];
40
    }
41
42 2
    public function rollback(): void
43
    {
44 2
        CustomerFixtureRollback::create()->execute(...$this->customerFixtures);
45 2
        $this->customerFixtures = [];
46 2
    }
47
}
48