Completed
Push — master ( 6d8f19...633314 )
by Fabian
28:44 queued 28:42
created

OptionFixturePool::add()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 1
b 0
f 0
nc 2
nop 3
dl 0
loc 6
rs 10
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
    public function add(AttributeOption $option, string $attributecode, string $key = null): void
21
    {
22
        if ($key === null) {
23
            $this->optionFixtures[] = new OptionFixture($option, $attributecode);
24
        } else {
25
            $this->optionFixtures[$key] = new OptionFixture($option, $attributecode);
26
        }
27
    }
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
    public function get(string $key = null): OptionFixture
36
    {
37
        if ($key === null) {
38
            $key = \array_key_last($this->optionFixtures);
39
        }
40
        if ($key === null || !\array_key_exists($key, $this->optionFixtures)) {
41
            throw new \OutOfBoundsException('No matching option found in fixture pool');
42
        }
43
        return $this->optionFixtures[$key];
44
    }
45
46
    public function rollback(): void
47
    {
48
        OptionFixtureRollback::create()->execute(...values($this->optionFixtures));
49
        $this->optionFixtures = [];
50
    }
51
}
52