Passed
Push — master ( 7dfdd5...21a87f )
by Kevin
02:34
created

TestState::bootFromContainer()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 6

Importance

Changes 3
Bugs 0 Features 1
Metric Value
cc 4
eloc 9
nc 4
nop 1
dl 0
loc 14
ccs 4
cts 8
cp 0.5
crap 6
rs 9.9666
c 3
b 0
f 1
1
<?php
2
3
namespace Zenstruck\Foundry\Test;
4
5
use Faker;
6
use Psr\Container\ContainerInterface;
7
use Psr\Container\NotFoundExceptionInterface;
8
use Zenstruck\Foundry\Configuration;
9
use Zenstruck\Foundry\Factory;
10
use Zenstruck\Foundry\StoryManager;
11
12
/**
13
 * @author Kevin Bond <[email protected]>
14
 */
15
final class TestState
16
{
17
    /** @var callable|null */
18
    private static $instantiator;
19
20
    /** @var Faker\Generator|null */
21
    private static $faker;
22
23
    /** @var bool */
24
    private static $useBundle = true;
25
26
    /** @var callable[] */
27
    private static $globalStates = [];
28
29
    public static function setInstantiator(callable $instantiator): void
30
    {
31
        self::$instantiator = $instantiator;
32
    }
33
34
    public static function setFaker(Faker\Generator $faker): void
35
    {
36
        self::$faker = $faker;
37
    }
38
39
    public static function withoutBundle(): void
40
    {
41
        self::$useBundle = false;
42
    }
43
44
    public static function addGlobalState(callable $callback): void
45
    {
46
        self::$globalStates[] = $callback;
47
    }
48
49 162
    public static function bootFactory(Configuration $configuration): Configuration
50
    {
51 162
        if (self::$instantiator) {
52
            $configuration->setInstantiator(self::$instantiator);
53
        }
54
55 162
        if (self::$faker) {
56
            $configuration->setFaker(self::$faker);
57
        }
58
59 162
        Factory::boot($configuration);
60
61 162
        return $configuration;
62
    }
63
64
    /**
65
     * @internal
66
     */
67 162
    public static function bootFromContainer(ContainerInterface $container): Configuration
68
    {
69 162
        if (self::$useBundle) {
70
            try {
71 82
                return self::bootFactory($container->get(Configuration::class));
72
            } catch (NotFoundExceptionInterface $e) {
73
                throw new \LogicException('Could not boot Foundry, is the ZenstruckFoundryBundle installed/configured?', 0, $e);
74
            }
75
        }
76
77
        try {
78 80
            return self::bootFactory(new Configuration($container->get('doctrine'), new StoryManager([])));
79
        } catch (NotFoundExceptionInterface $e) {
80
            throw new \LogicException('Could not boot Foundry, is the DoctrineBundle installed/configured?', 0, $e);
81
        }
82
    }
83
84
    /**
85
     * @internal
86
     */
87 162
    public static function flushGlobalState(): void
88
    {
89 162
        StoryManager::globalReset();
90
91 162
        foreach (self::$globalStates as $callback) {
92 162
            $callback();
93
        }
94
95 162
        StoryManager::setGlobalState();
96 162
    }
97
}
98