Passed
Pull Request — master (#681)
by Jonathan
02:01
created

DependencyFactory::getStopwatch()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 1
rs 10
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 193
    public function __construct(Configuration $configuration)
27
    {
28 193
        $this->configuration = $configuration;
29 193
    }
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 137
    public function getSchemaDiffProvider() : SchemaDiffProviderInterface
42
    {
43
        return $this->getDependency(SchemaDiffProviderInterface::class, function () : LazySchemaDiffProvider {
44 137
            return LazySchemaDiffProvider::fromDefaultProxyFactoryConfiguration(
45 137
                new SchemaDiffProvider(
46 137
                    $this->getConnection()->getSchemaManager(),
47 137
                    $this->getConnection()->getDatabasePlatform()
48
                )
49
            );
50 137
        });
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 137
    public function getParameterFormatter() : ParameterFormatterInterface
66
    {
67
        return $this->getDependency(ParameterFormatter::class, function () : ParameterFormatter {
68 137
            return new ParameterFormatter($this->getConnection());
69 137
        });
70
    }
71
72 137
    public function getMigrationRepository() : MigrationRepository
73
    {
74
        return $this->getDependency(MigrationRepository::class, function () : MigrationRepository {
75 137
            return new MigrationRepository(
76 137
                $this->configuration,
77 137
                $this->getConnection(),
78 137
                $this->configuration->getMigrationsFinder(),
79 137
                new VersionFactory($this->configuration, $this->getVersionExecutor())
80
            );
81 137
        });
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->getConnection()->getSchemaManager(),
102 73
                $this->configuration->getMigrationsTableName(),
103 73
                $this->configuration->getMigrationsColumnName(),
104 73
                $this->configuration->getMigrationsColumnLength(),
105 73
                $this->configuration->getMigrationsExecutedAtColumnName()
106
            );
107 73
        });
108
    }
109
110 66
    public function getMigrationTableStatus() : MigrationTableStatus
111
    {
112
        return $this->getDependency(MigrationTableStatus::class, function () : MigrationTableStatus {
113 66
            return new MigrationTableStatus(
114 66
                $this->getConnection()->getSchemaManager(),
115 66
                $this->getMigrationTable()
116
            );
117 66
        });
118
    }
119
120 66
    public function getMigrationTableUpdater() : MigrationTableUpdater
121
    {
122
        return $this->getDependency(MigrationTableUpdater::class, function () : MigrationTableUpdater {
123 66
            return new MigrationTableUpdater(
124 66
                $this->getConnection(),
125 66
                $this->getConnection()->getSchemaManager(),
126 66
                $this->getMigrationTable(),
127 66
                $this->getConnection()->getDatabasePlatform()
128
            );
129 66
        });
130
    }
131
132 137
    public function getVersionExecutor() : VersionExecutor
133
    {
134
        return $this->getDependency(VersionExecutor::class, function () : VersionExecutor {
135 137
            return new VersionExecutor(
136 137
                $this->configuration,
137 137
                $this->getConnection(),
138 137
                $this->getSchemaDiffProvider(),
139 137
                $this->getOutputWriter(),
140 137
                $this->getParameterFormatter(),
141 137
                $this->getStopwatch()
142
            );
143 137
        });
144
    }
145
146 6
    public function getQueryWriter() : FileQueryWriter
147
    {
148
        return $this->getDependency(FileQueryWriter::class, function () : FileQueryWriter {
149 6
            return new FileQueryWriter(
150 6
                $this->getOutputWriter(),
151 6
                $this->getMigrationFileBuilder()
152
            );
153 6
        });
154
    }
155
156 158
    public function getOutputWriter() : OutputWriter
157
    {
158
        return $this->getDependency(OutputWriter::class, function () : OutputWriter {
159 158
            return new OutputWriter();
160 158
        });
161
    }
162
163 8
    public function getVersionAliasResolver() : VersionAliasResolver
164
    {
165
        return $this->getDependency(VersionAliasResolver::class, function () : VersionAliasResolver {
166 8
            return new VersionAliasResolver(
167 8
                $this->getMigrationRepository()
168
            );
169 8
        });
170
    }
171
172 33
    public function getMigrationPlanCalculator() : MigrationPlanCalculator
173
    {
174
        return $this->getDependency(MigrationPlanCalculator::class, function () : MigrationPlanCalculator {
175 33
            return new MigrationPlanCalculator($this->getMigrationRepository());
176 33
        });
177
    }
178
179 151
    public function getRecursiveRegexFinder() : RecursiveRegexFinder
180
    {
181
        return $this->getDependency(RecursiveRegexFinder::class, function () : RecursiveRegexFinder {
182 151
            return new RecursiveRegexFinder();
183 151
        });
184
    }
185
186 7
    public function getMigrationGenerator() : MigrationGenerator
187
    {
188
        return $this->getDependency(MigrationGenerator::class, function () : MigrationGenerator {
189 7
            return new MigrationGenerator($this->configuration);
190 7
        });
191
    }
192
193 5
    public function getMigrationSqlGenerator() : MigrationSqlGenerator
194
    {
195
        return $this->getDependency(MigrationSqlGenerator::class, function () : MigrationSqlGenerator {
196 5
            return new MigrationSqlGenerator(
197 5
                $this->configuration,
198 5
                $this->getConnection()->getDatabasePlatform()
199
            );
200 5
        });
201
    }
202
203 8
    public function getMigrationStatusInfosHelper() : MigrationStatusInfosHelper
204
    {
205
        return $this->getDependency(MigrationStatusInfosHelper::class, function () : MigrationStatusInfosHelper {
206 8
            return new MigrationStatusInfosHelper(
207 8
                $this->configuration,
208 8
                $this->getMigrationRepository()
209
            );
210 8
        });
211
    }
212
213 1
    public function getMigrator() : Migrator
214
    {
215
        return $this->getDependency(Migrator::class, function () : Migrator {
216 1
            return new Migrator(
217 1
                $this->configuration,
218 1
                $this->getMigrationRepository(),
219 1
                $this->getOutputWriter(),
220 1
                $this->getStopwatch()
221
            );
222 1
        });
223
    }
224
225 137
    public function getStopwatch() : Stopwatch
226
    {
227
        return $this->getDependency(Stopwatch::class, function () : Stopwatch {
228 137
            return new Stopwatch();
229 137
        });
230
    }
231
232
    /**
233
     * @return mixed
234
     */
235 193
    private function getDependency(string $className, callable $callback)
236
    {
237 193
        if (! isset($this->dependencies[$className])) {
238 193
            $this->dependencies[$className] = $callback();
239
        }
240
241 193
        return $this->dependencies[$className];
242
    }
243
244 166
    private function getConnection() : Connection
245
    {
246 166
        return $this->configuration->getConnection();
247
    }
248
}
249