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

CategoryFixturePool   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 13
c 1
b 0
f 0
dl 0
loc 38
ccs 14
cts 14
cp 1
rs 10
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A add() 0 6 2
A rollback() 0 4 1
A get() 0 9 3
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