Completed
Push — master ( 882947...a4a53d )
by Mike
13:52 queued 13:46
created

LazySchemaDiffProvider::createFromSchema()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 9
cts 9
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 8
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Doctrine\DBAL\Migrations\Provider;
4
5
use Doctrine\DBAL\Schema\Schema;
6
use ProxyManager\Configuration;
7
use ProxyManager\Factory\LazyLoadingValueHolderFactory;
8
use ProxyManager\GeneratorStrategy\EvaluatingGeneratorStrategy;
9
use ProxyManager\Proxy\LazyLoadingInterface;
10
11
class LazySchemaDiffProvider implements SchemaDiffProviderInterface
12
{
13
    /** @var  LazyLoadingValueHolderFactory */
14
    private $proxyFactory;
15
16
    /** @var SchemaDiffProviderInterface */
17
    private $originalSchemaManipulator;
18
19 114
    public function __construct(LazyLoadingValueHolderFactory $proxyFactory, SchemaDiffProviderInterface $originalSchemaManipulator)
20
    {
21 114
        $this->proxyFactory              = $proxyFactory;
22 114
        $this->originalSchemaManipulator = $originalSchemaManipulator;
23 114
    }
24
25
    public static function fromDefaultProxyFacyoryConfiguration(SchemaDiffProviderInterface $originalSchemaManipulator)
26
    {
27
        $message = 'Function %s::fromDefaultProxyFacyoryConfiguration() deprecated due to typo.'
28
            . 'Use %s::fromDefaultProxyFactoryConfiguration() instead';
29
30
        trigger_error(
31
            sprintf($message, self::class),
32
            E_USER_DEPRECATED
33
        );
34
35
        return self::fromDefaultProxyFactoryConfiguration($originalSchemaManipulator);
36
    }
37
38 114
    public static function fromDefaultProxyFactoryConfiguration(SchemaDiffProviderInterface $originalSchemaManipulator)
39
    {
40 114
        $proxyConfig = new Configuration();
41 114
        $proxyConfig->setGeneratorStrategy(new EvaluatingGeneratorStrategy());
42 114
        $proxyFactory = new LazyLoadingValueHolderFactory($proxyConfig);
43
44 114
        return new LazySchemaDiffProvider($proxyFactory, $originalSchemaManipulator);
45
    }
46
47
    /**
48
     * @return Schema
49
     */
50 44
    public function createFromSchema()
51
    {
52 44
        $originalSchemaManipulator = $this->originalSchemaManipulator;
53
54 44
        return $this->proxyFactory->createProxy(
55 44
            Schema::class,
56 44
            function (& $wrappedObject, $proxy, $method, array $parameters, & $initializer) use ($originalSchemaManipulator) {
57 13
                $initializer   = null;
58 13
                $wrappedObject = $originalSchemaManipulator->createFromSchema();
59
60 13
                return true;
61 44
            }
62
        );
63
    }
64
65
    /**
66
     * @param Schema $fromSchema
67
     * @return Schema
68
     */
69 42
    public function createToSchema(Schema $fromSchema)
70
    {
71 42
        $originalSchemaManipulator = $this->originalSchemaManipulator;
72
73 42
        if ($fromSchema instanceof LazyLoadingInterface && ! $fromSchema->isProxyInitialized()) {
74 41
            return $this->proxyFactory->createProxy(
75 41
                Schema::class,
76 41
                function (& $wrappedObject, $proxy, $method, array $parameters, & $initializer) use ($originalSchemaManipulator, $fromSchema) {
77 12
                    $initializer   = null;
78 12
                    $wrappedObject = $originalSchemaManipulator->createToSchema($fromSchema);
79
80 12
                    return true;
81 41
                }
82
            );
83
        }
84
85 1
        return $this->originalSchemaManipulator->createToSchema($fromSchema);
86
    }
87
88
    /**
89
     * @param Schema $fromSchema
90
     * @param Schema $toSchema
91
     *
92
     * @return string[]
93
     */
94 41
    public function getSqlDiffToMigrate(Schema $fromSchema, Schema $toSchema)
95
    {
96 41
        if ($toSchema instanceof LazyLoadingInterface
97 41
            && ! $toSchema->isProxyInitialized()) {
98 33
            return [];
99
        }
100
101 13
        return $this->originalSchemaManipulator->getSqlDiffToMigrate($fromSchema, $toSchema);
102
    }
103
}
104