Completed
Push — master ( ab9bf2...7d6b3f )
by Fabian
09:56 queued 01:25
created

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