FixtureTarget   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 20
dl 0
loc 54
ccs 0
cts 19
cp 0
rs 10
c 0
b 0
f 0
wmc 7

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 1
A getFixtureList() 0 7 2
A import() 0 11 2
A isEnabled() 0 3 1
A getKey() 0 3 1
1
<?php declare(strict_types=1);
2
/**
3
 * This file is part of the daikon-cqrs/boot project.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
9
namespace Daikon\Boot\Fixture;
10
11
use Daikon\Interop\Assertion;
12
use Daikon\MessageBus\MessageBusInterface;
13
14
final class FixtureTarget implements FixtureTargetInterface
15
{
16
    private string $key;
17
18
    private bool $enabled;
19
20
    private FixtureLoaderInterface $fixtureLoader;
21
22
    private MessageBusInterface $messageBus;
23
24
    private ?FixtureList $fixtureList;
25
26
    public function __construct(
27
        string $key,
28
        bool $enabled,
29
        FixtureLoaderInterface $fixtureLoader,
30
        MessageBusInterface $messageBus
31
    ) {
32
        $this->key = $key;
33
        $this->enabled = $enabled;
34
        $this->fixtureLoader = $fixtureLoader;
35
        $this->messageBus = $messageBus;
36
    }
37
38
    public function import(FixtureInterface $fixture): bool
39
    {
40
        Assertion::true($this->isEnabled(), sprintf("Fixture '%s' is not enabled.", $fixture->getName()));
41
42
        $index = $this->getFixtureList()->find($fixture);
43
44
        if ($index !== false) {
45
            $fixture($this->messageBus);
46
        }
47
48
        return $index !== false;
49
    }
50
51
    public function getKey(): string
52
    {
53
        return $this->key;
54
    }
55
56
    public function isEnabled(): bool
57
    {
58
        return $this->enabled === true;
59
    }
60
61
    public function getFixtureList(): FixtureList
62
    {
63
        if (!isset($this->fixtureList)) {
64
            //@todo fix fixture class loading to avoid this
65
            $this->fixtureList = $this->fixtureLoader->load();
66
        }
67
        return $this->fixtureList;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->fixtureList could return the type null which is incompatible with the type-hinted return Daikon\Boot\Fixture\FixtureList. Consider adding an additional type-check to rule them out.
Loading history...
68
    }
69
}
70