OptionFixturePool   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A get() 0 9 4
A rollback() 0 4 1
A add() 0 6 2
1
<?php
2
declare(strict_types=1);
3
4
namespace TddWizard\Fixtures\Catalog;
5
6
use Magento\Eav\Model\Entity\Attribute\Option as AttributeOption;
7
use function array_values as values;
8
9
/**
10
 * Class OptionFixturePool
11
 */
12
class OptionFixturePool
13
{
14
15
    /**
16
     * @var OptionFixture[]
17
     */
18
    private $optionFixtures = [];
19
20 5
    public function add(AttributeOption $option, string $attributecode, string $key = null): void
21
    {
22 5
        if ($key === null) {
23 3
            $this->optionFixtures[] = new OptionFixture($option, $attributecode);
24
        } else {
25 2
            $this->optionFixtures[$key] = new OptionFixture($option, $attributecode);
26
        }
27 5
    }
28
29
    /**
30
     * Returns option fixture by key, or last added if key not specified
31
     *
32
     * @param string|null $key
33
     * @return OptionFixture
34
     */
35 5
    public function get(string $key = null): OptionFixture
36
    {
37 5
        if ($key === null) {
38 3
            $key = \array_key_last($this->optionFixtures);
39
        }
40 5
        if ($key === null || !\array_key_exists($key, $this->optionFixtures)) {
41 3
            throw new \OutOfBoundsException('No matching option found in fixture pool');
42
        }
43 2
        return $this->optionFixtures[$key];
44
    }
45
46 2
    public function rollback(): void
47
    {
48 2
        OptionFixtureRollback::create()->execute(...values($this->optionFixtures));
49 2
        $this->optionFixtures = [];
50 2
    }
51
}
52