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

ProductFixturePool::add()   A

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