Passed
Branch master (836442)
by Kevin
02:28
created

TestState::bootFoundry()   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 0
Metric Value
eloc 8
c 0
b 0
f 0
dl 0
loc 17
ccs 6
cts 9
cp 0.6667
rs 10
cc 4
nc 8
nop 1
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 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 alwaysAutoRefreshProxies(): void
40
    {
41
        self::$alwaysAutoRefreshProxies = true;
42
    }
43
44
    /**
45
     * @deprecated Foundry now auto-detects if the bundle is installed
46
     */
47
    public static function withoutBundle(): void
48
    {
49
        trigger_deprecation('zenstruck\foundry', '1.4.0', 'TestState::withoutBundle() is deprecated, the bundle is now auto-detected.');
50
    }
51
52
    public static function addGlobalState(callable $callback): void
53
    {
54
        self::$globalStates[] = $callback;
55
    }
56
57 964
    public static function bootFoundry(?Configuration $configuration = null): void
58
    {
59 964
        $configuration = $configuration ?? new Configuration();
60
61 964
        if (self::$instantiator) {
62
            $configuration->setInstantiator(self::$instantiator);
63
        }
64
65 964
        if (self::$faker) {
66
            $configuration->setFaker(self::$faker);
67
        }
68
69 964
        if (self::$alwaysAutoRefreshProxies) {
70
            $configuration->alwaysAutoRefreshProxies();
71
        }
72
73 964
        Factory::boot($configuration);
74 964
    }
75
76 964
    public static function shutdownFoundry(): void
77
    {
78 964
        Factory::shutdown();
79 964
        StoryManager::reset();
80 964
    }
81
82
    /**
83
     * @deprecated use TestState::bootFoundry()
84
     */
85
    public static function bootFactory(Configuration $configuration): Configuration
86
    {
87
        trigger_deprecation('zenstruck\foundry', '1.4.0', 'TestState::bootFactory() is deprecated, use TestState::bootFoundry().');
88
89
        self::bootFoundry($configuration);
90
91
        return Factory::configuration();
92
    }
93
94
    /**
95
     * @internal
96
     */
97 634
    public static function bootFromContainer(ContainerInterface $container): void
98
    {
99 634
        if ($container->has(Configuration::class)) {
100 256
            self::bootFoundry($container->get(Configuration::class));
101
102 256
            return;
103
        }
104
105 378
        $configuration = new Configuration();
106
107
        try {
108 378
            $configuration->setManagerRegistry($container->get('doctrine'));
109
        } catch (NotFoundExceptionInterface $e) {
110
            throw new \LogicException('Could not boot Foundry, is the DoctrineBundle installed/configured?', 0, $e);
111
        }
112
113 378
        self::bootFoundry($configuration);
114 378
    }
115
116
    /**
117
     * @internal
118
     */
119 381
    public static function flushGlobalState(): void
120
    {
121 381
        StoryManager::globalReset();
122
123 381
        foreach (self::$globalStates as $callback) {
124 381
            $callback();
125
        }
126
127 381
        StoryManager::setGlobalState();
128 381
    }
129
}
130