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

CategoryFixturePool::rollback()   A

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