Passed
Pull Request — master (#63)
by Laura
44:11 queued 10s
created

OptionFixturePool   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 12
c 1
b 0
f 0
dl 0
loc 38
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
    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