Failed Conditions
Pull Request — master (#675)
by Jonathan
06:31
created

DependencyFactory::getSchemaDiffProvider()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 0
dl 0
loc 7
ccs 5
cts 5
cp 1
crap 1
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
use Doctrine\Migrations\Tools\Console\Helper\MigrationStatusInfosHelper;
14
15
/**
16
 * @internal
17
 */
18
class DependencyFactory
19
{
20
    /** @var Configuration */
21
    private $configuration;
22
23
    /** @var object[] */
24
    private $dependencies = [];
25
26 189
    public function __construct(Configuration $configuration)
27
    {
28 189
        $this->configuration = $configuration;
29 189
    }
30
31 53
    public function getEventDispatcher() : EventDispatcher
32
    {
33
        return $this->getDependency(EventDispatcher::class, function () : EventDispatcher {
34 53
            return new EventDispatcher(
35 53
                $this->configuration,
36 53
                $this->getConnection()->getEventManager()
37
            );
38 53
        });
39
    }
40
41 133
    public function getSchemaDiffProvider() : SchemaDiffProviderInterface
42
    {
43
        return $this->getDependency(SchemaDiffProviderInterface::class, function () : LazySchemaDiffProvider {
44 133
            return LazySchemaDiffProvider::fromDefaultProxyFactoryConfiguration(
45 133
                new SchemaDiffProvider(
46 133
                    $this->getConnection()->getSchemaManager(),
47 133
                    $this->getConnection()->getDatabasePlatform()
48
                )
49
            );
50 133
        });
51
    }
52
53 6
    public function getMigrationFileBuilder() : MigrationFileBuilder
54
    {
55
        return $this->getDependency(MigrationFileBuilder::class, function () : MigrationFileBuilder {
56 6
            return new MigrationFileBuilder(
57 6
                $this->getConnection()->getDatabasePlatform(),
58 6
                $this->configuration->getMigrationsTableName(),
59 6
                $this->configuration->getQuotedMigrationsColumnName(),
60 6
                $this->configuration->getQuotedMigrationsExecutedAtColumnName()
61
            );
62 6
        });
63
    }
64
65 133
    public function getParameterFormatter() : ParameterFormatterInterface
66
    {
67
        return $this->getDependency(ParameterFormatter::class, function () : ParameterFormatter {
68 133
            return new ParameterFormatter($this->getConnection());
69 133
        });
70
    }
71
72 133
    public function getMigrationRepository() : MigrationRepository
73
    {
74
        return $this->getDependency(MigrationRepository::class, function () : MigrationRepository {
75 133
            return new MigrationRepository(
76 133
                $this->configuration,
77 133
                $this->getConnection(),
78 133
                $this->configuration->getMigrationsFinder(),
79 133
                new VersionFactory($this->configuration, $this->getVersionExecutor())
80
            );
81 133
        });
82
    }
83
84 66
    public function getMigrationTableManipulator() : MigrationTableManipulator
85
    {
86
        return $this->getDependency(MigrationTableManipulator::class, function () : MigrationTableManipulator {
87 66
            return new MigrationTableManipulator(
88 66
                $this->configuration,
89 66
                $this->getConnection()->getSchemaManager(),
90 66
                $this->getMigrationTable(),
91 66
                $this->getMigrationTableStatus(),
92 66
                $this->getMigrationTableUpdater()
93
            );
94 66
        });
95
    }
96
97 73
    public function getMigrationTable() : MigrationTable
98
    {
99
        return $this->getDependency(MigrationTable::class, function () : MigrationTable {
100 73
            return new MigrationTable(
101 73
                $this->configuration->getMigrationsTableName(),
102 73
                $this->configuration->getMigrationsColumnName(),
103 73
                $this->configuration->getMigrationsColumnLength(),
104 73
                $this->configuration->getMigrationsExecutedAtColumnName()
105
            );
106 73
        });
107
    }
108
109 66
    public function getMigrationTableStatus() : MigrationTableStatus
110
    {
111
        return $this->getDependency(MigrationTableStatus::class, function () : MigrationTableStatus {
112 66
            return new MigrationTableStatus(
113 66
                $this->getConnection()->getSchemaManager(),
114 66
                $this->getMigrationTable()
115
            );
116 66
        });
117
    }
118
119 66
    public function getMigrationTableUpdater() : MigrationTableUpdater
120
    {
121
        return $this->getDependency(MigrationTableUpdater::class, function () : MigrationTableUpdater {
122 66
            return new MigrationTableUpdater(
123 66
                $this->getConnection(),
124 66
                $this->getConnection()->getSchemaManager(),
125 66
                $this->getMigrationTable(),
126 66
                $this->getConnection()->getDatabasePlatform()
127
            );
128 66
        });
129
    }
130
131 133
    public function getVersionExecutor() : VersionExecutor
132
    {
133
        return $this->getDependency(VersionExecutor::class, function () : VersionExecutor {
134 133
            return new VersionExecutor(
135 133
                $this->configuration,
136 133
                $this->getConnection(),
137 133
                $this->getSchemaDiffProvider(),
138 133
                $this->getOutputWriter(),
139 133
                $this->getParameterFormatter()
140
            );
141 133
        });
142
    }
143
144 6
    public function getQueryWriter() : FileQueryWriter
145
    {
146
        return $this->getDependency(FileQueryWriter::class, function () : FileQueryWriter {
147 6
            return new FileQueryWriter(
148 6
                $this->getOutputWriter(),
149 6
                $this->getMigrationFileBuilder()
150
            );
151 6
        });
152
    }
153
154 154
    public function getOutputWriter() : OutputWriter
155
    {
156
        return $this->getDependency(OutputWriter::class, function () : OutputWriter {
157 154
            return new OutputWriter();
158 154
        });
159
    }
160
161 8
    public function getVersionAliasResolver() : VersionAliasResolver
162
    {
163
        return $this->getDependency(VersionAliasResolver::class, function () : VersionAliasResolver {
164 8
            return new VersionAliasResolver(
165 8
                $this->getMigrationRepository()
166
            );
167 8
        });
168
    }
169
170 33
    public function getMigrationPlanCalculator() : MigrationPlanCalculator
171
    {
172
        return $this->getDependency(MigrationPlanCalculator::class, function () : MigrationPlanCalculator {
173 33
            return new MigrationPlanCalculator($this->getMigrationRepository());
174 33
        });
175
    }
176
177 147
    public function getRecursiveRegexFinder() : RecursiveRegexFinder
178
    {
179
        return $this->getDependency(RecursiveRegexFinder::class, function () : RecursiveRegexFinder {
180 147
            return new RecursiveRegexFinder();
181 147
        });
182
    }
183
184 7
    public function getMigrationGenerator() : MigrationGenerator
185
    {
186
        return $this->getDependency(MigrationGenerator::class, function () : MigrationGenerator {
187 7
            return new MigrationGenerator($this->configuration);
188 7
        });
189
    }
190
191 5
    public function getMigrationSqlGenerator() : MigrationSqlGenerator
192
    {
193
        return $this->getDependency(MigrationSqlGenerator::class, function () : MigrationSqlGenerator {
194 5
            return new MigrationSqlGenerator(
195 5
                $this->configuration,
196 5
                $this->getConnection()->getDatabasePlatform()
197
            );
198 5
        });
199
    }
200
201 8
    public function getMigrationStatusInfosHelper() : MigrationStatusInfosHelper
202
    {
203
        return $this->getDependency(MigrationStatusInfosHelper::class, function () : MigrationStatusInfosHelper {
204 8
            return new MigrationStatusInfosHelper(
205 8
                $this->configuration,
206 8
                $this->getMigrationRepository()
207
            );
208 8
        });
209
    }
210
211 1
    public function getMigrator() : Migrator
212
    {
213
        return $this->getDependency(Migrator::class, function () : Migrator {
214 1
            return new Migrator(
215 1
                $this->configuration,
216 1
                $this->getMigrationRepository(),
217 1
                $this->getOutputWriter()
218
            );
219 1
        });
220
    }
221
222
    /**
223
     * @return mixed
224
     */
225 189
    private function getDependency(string $className, callable $callback)
226
    {
227 189
        if (! isset($this->dependencies[$className])) {
228 189
            $this->dependencies[$className] = $callback();
229
        }
230
231 189
        return $this->dependencies[$className];
232
    }
233
234 160
    private function getConnection() : Connection
235
    {
236 160
        return $this->configuration->getConnection();
237
    }
238
}
239