Failed Conditions
Push — master ( 51f284...368556 )
by Jonathan
02:26
created

DependencyFactory::getDependency()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 2
dl 0
loc 7
ccs 4
cts 4
cp 1
crap 2
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\Migrations;
6
7
use Doctrine\DBAL\Connection;
8
use Doctrine\Migrations\Configuration\Configuration;
9
use Doctrine\Migrations\Finder\RecursiveRegexFinder;
10
use Doctrine\Migrations\Provider\LazySchemaDiffProvider;
11
use Doctrine\Migrations\Provider\SchemaDiffProvider;
12
use Doctrine\Migrations\Provider\SchemaDiffProviderInterface;
13
14
/**
15
 * @internal
16
 */
17
final class DependencyFactory
18
{
19
    /** @var Configuration */
20
    private $configuration;
21
22
    /** @var object[] */
23
    private $dependencies = [];
24
25 180
    public function __construct(Configuration $configuration)
26
    {
27 180
        $this->configuration = $configuration;
28 180
    }
29
30 52
    public function getEventDispatcher() : EventDispatcher
31
    {
32
        return $this->getDependency(EventDispatcher::class, function () {
33 52
            return new EventDispatcher(
34 52
                $this->configuration,
35 52
                $this->getConnection()->getEventManager()
36
            );
37 52
        });
38
    }
39
40 131
    public function getSchemaDiffProvider() : SchemaDiffProviderInterface
41
    {
42
        return $this->getDependency(SchemaDiffProviderInterface::class, function () {
43 131
            return LazySchemaDiffProvider::fromDefaultProxyFactoryConfiguration(
44 131
                new SchemaDiffProvider(
45 131
                    $this->getConnection()->getSchemaManager(),
46 131
                    $this->getConnection()->getDatabasePlatform()
47
                )
48
            );
49 131
        });
50
    }
51
52 7
    public function getMigrationFileBuilder() : MigrationFileBuilder
53
    {
54
        return $this->getDependency(MigrationFileBuilder::class, function () {
55 7
            return new MigrationFileBuilder(
56 7
                $this->configuration->getMigrationsTableName(),
57 7
                $this->configuration->getQuotedMigrationsColumnName()
58
            );
59 7
        });
60
    }
61
62 131
    public function getParameterFormatter() : ParameterFormatterInterface
63
    {
64
        return $this->getDependency(ParameterFormatter::class, function () {
65 131
            return new ParameterFormatter($this->getConnection());
66 131
        });
67
    }
68
69 131
    public function getMigrationRepository() : MigrationRepository
70
    {
71
        return $this->getDependency(MigrationRepository::class, function () {
72 131
            return new MigrationRepository(
73 131
                $this->configuration,
74 131
                $this->getConnection(),
75 131
                $this->configuration->getMigrationsFinder(),
76 131
                new VersionFactory($this->configuration, $this->getVersionExecutor())
77
            );
78 131
        });
79
    }
80
81 71
    public function getMigrationTableCreator() : MigrationTableCreator
82
    {
83
        return $this->getDependency(MigrationTableCreator::class, function () {
84 71
            return new MigrationTableCreator(
85 71
                $this->configuration,
86 71
                $this->getConnection()->getSchemaManager()
87
            );
88 71
        });
89
    }
90
91 131
    public function getVersionExecutor() : VersionExecutor
92
    {
93
        return $this->getDependency(VersionExecutor::class, function () {
94 131
            return new VersionExecutor(
95 131
                $this->configuration,
96 131
                $this->getConnection(),
97 131
                $this->getSchemaDiffProvider(),
98 131
                $this->getOutputWriter(),
99 131
                $this->getParameterFormatter()
100
            );
101 131
        });
102
    }
103
104 7
    public function getQueryWriter() : FileQueryWriter
105
    {
106
        return $this->getDependency(FileQueryWriter::class, function () {
107 7
            return new FileQueryWriter(
108 7
                $this->getOutputWriter(),
109 7
                $this->getMigrationFileBuilder()
110
            );
111 7
        });
112
    }
113
114 148
    public function getOutputWriter() : OutputWriter
115
    {
116
        return $this->getDependency(OutputWriter::class, function () {
117 148
            return new OutputWriter();
118 148
        });
119
    }
120
121 9
    public function getVersionAliasResolver() : VersionAliasResolver
122
    {
123
        return $this->getDependency(VersionAliasResolver::class, function () {
124 9
            return new VersionAliasResolver(
125 9
                $this->getMigrationRepository()
126
            );
127 9
        });
128
    }
129
130 33
    public function getMigrationPlanCalculator() : MigrationPlanCalculator
131
    {
132
        return $this->getDependency(MigrationPlanCalculator::class, function () {
133 33
            return new MigrationPlanCalculator($this->getMigrationRepository());
134 33
        });
135
    }
136
137 145
    public function getRecursiveRegexFinder() : RecursiveRegexFinder
138
    {
139
        return $this->getDependency(RecursiveRegexFinder::class, function () {
140 145
            return new RecursiveRegexFinder();
141 145
        });
142
    }
143
144
    /**
145
     * @return mixed
146
     */
147 180
    private function getDependency(string $className, callable $callback)
148
    {
149 180
        if (! isset($this->dependencies[$className])) {
150 180
            $this->dependencies[$className] = $callback();
151
        }
152
153 180
        return $this->dependencies[$className];
154
    }
155
156 155
    private function getConnection() : Connection
157
    {
158 155
        return $this->configuration->getConnection();
159
    }
160
}
161