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

OrderFixturePool   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Test Coverage

Coverage 78.56%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 13
c 1
b 0
f 0
dl 0
loc 38
ccs 11
cts 14
cp 0.7856
rs 10
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A add() 0 6 2
A get() 0 9 3
A rollback() 0 4 1
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