Completed
Push — master ( a5022e...49928c )
by Fabian
14s queued 10s
created

CustomerFixturePool::rollback()   A

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 0
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 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