Passed
Pull Request — master (#19)
by Kevin
03:46
created

TestState::bootFactory()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 17
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 4.5923

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 8
c 1
b 0
f 0
nc 8
nop 1
dl 0
loc 17
rs 10
ccs 6
cts 9
cp 0.6667
crap 4.5923
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 $alwaysAutoRefreshProxies = false;
25
26
    /** @var bool */
27
    private static $useBundle = true;
28
29
    /** @var callable[] */
30
    private static $globalStates = [];
31
32
    public static function setInstantiator(callable $instantiator): void
33
    {
34
        self::$instantiator = $instantiator;
35
    }
36
37
    public static function setFaker(Faker\Generator $faker): void
38
    {
39
        self::$faker = $faker;
40
    }
41
42
    public static function alwaysAutoRefreshProxies(): void
43
    {
44
        self::$alwaysAutoRefreshProxies = true;
45
    }
46
47
    public static function withoutBundle(): void
48
    {
49
        self::$useBundle = false;
50
    }
51
52
    public static function addGlobalState(callable $callback): void
53
    {
54
        self::$globalStates[] = $callback;
55
    }
56
57 182
    public static function bootFactory(Configuration $configuration): Configuration
58
    {
59 182
        if (self::$instantiator) {
60
            $configuration->setInstantiator(self::$instantiator);
61
        }
62
63 182
        if (self::$faker) {
64
            $configuration->setFaker(self::$faker);
65
        }
66
67 182
        if (self::$alwaysAutoRefreshProxies) {
68
            $configuration->alwaysAutoRefreshProxies();
69
        }
70
71 182
        Factory::boot($configuration);
72
73 182
        return $configuration;
74
    }
75
76
    /**
77
     * @internal
78
     */
79 182
    public static function bootFromContainer(ContainerInterface $container): Configuration
80
    {
81 182
        if (self::$useBundle) {
82
            try {
83 92
                return self::bootFactory($container->get(Configuration::class));
84
            } catch (NotFoundExceptionInterface $e) {
85
                throw new \LogicException('Could not boot Foundry, is the ZenstruckFoundryBundle installed/configured?', 0, $e);
86
            }
87
        }
88
89
        try {
90 90
            return self::bootFactory(new Configuration($container->get('doctrine'), new StoryManager([])));
91
        } catch (NotFoundExceptionInterface $e) {
92
            throw new \LogicException('Could not boot Foundry, is the DoctrineBundle installed/configured?', 0, $e);
93
        }
94
    }
95
96
    /**
97
     * @internal
98
     */
99 182
    public static function flushGlobalState(): void
100
    {
101 182
        StoryManager::globalReset();
102
103 182
        foreach (self::$globalStates as $callback) {
104 182
            $callback();
105
        }
106
107 182
        StoryManager::setGlobalState();
108 182
    }
109
}
110