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