ProductFixturePool::rollback()   A
last analyzed

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