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

LazySchemaDiffProvider   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 93
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 85%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 4
dl 0
loc 93
ccs 34
cts 40
cp 0.85
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getSqlDiffToMigrate() 0 9 3
A __construct() 0 5 1
A fromDefaultProxyFacyoryConfiguration() 0 12 1
A fromDefaultProxyFactoryConfiguration() 0 8 1
A createFromSchema() 0 14 1
A createToSchema() 0 18 3
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