InvoiceFixturePool::add()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

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