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

OrderFixturePool::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\Sales;
5
6
use Magento\Sales\Model\Order;
7
8
class OrderFixturePool
9
{
10
11
    /**
12
     * @var OrderFixture[]
13
     */
14
    private $orderFixtures = [];
15
16 5
    public function add(Order $order, string $key = null): void
17
    {
18 5
        if ($key === null) {
19 3
            $this->orderFixtures[] = new OrderFixture($order);
20
        } else {
21 2
            $this->orderFixtures[$key] = new OrderFixture($order);
22
        }
23 5
    }
24
25
    /**
26
     * Returns order fixture by key, or last added if key not specified
27
     *
28
     * @param string|null $key
29
     * @return OrderFixture
30
     */
31 5
    public function get(string $key = null): OrderFixture
32
    {
33 5
        if ($key === null) {
34 3
            $key = \array_key_last($this->orderFixtures);
35
        }
36 5
        if (!array_key_exists($key, $this->orderFixtures)) {
37 3
            throw new \OutOfBoundsException('No matching order found in fixture pool');
38
        }
39 2
        return $this->orderFixtures[$key];
40
    }
41
42 2
    public function rollback(): void
43
    {
44 2
        OrderFixtureRollback::create()->execute(...$this->orderFixtures);
45 2
        $this->orderFixtures = [];
46 2
    }
47
}
48