Completed
Push — master ( b23bf5...05c479 )
by Mike
25:24
created

fromDefaultProxyFacyoryConfiguration()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
ccs 5
cts 5
cp 1
rs 9.4285
cc 1
eloc 5
nc 1
nop 1
crap 1
1
<?php
2
/*
3
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14
 *
15
 * This software consists of voluntary contributions made by many individuals
16
 * and is licensed under the LGPL. For more information, see
17
 * <http://www.doctrine-project.org>.
18
 */
19
20
namespace Doctrine\DBAL\Migrations\Provider;
21
22
use Doctrine\DBAL\Migrations\Provider\SchemaDiffProviderInterface;
23
use Doctrine\DBAL\Schema\Schema;
24
use ProxyManager\Configuration;
25
use ProxyManager\Factory\LazyLoadingValueHolderFactory;
26
use ProxyManager\GeneratorStrategy\EvaluatingGeneratorStrategy;
27
use ProxyManager\Proxy\LazyLoadingInterface;
28
29
class LazySchemaDiffProvider implements SchemaDiffProviderInterface
30
{
31
    /** @var  LazyLoadingValueHolderFactory */
32
    private $proxyFactory;
33
34
    /** @var SchemaDiffProviderInterface */
35
    private $originalSchemaManipulator;
36
37 92
    public function __construct(LazyLoadingValueHolderFactory $proxyFactory, SchemaDiffProviderInterface $originalSchemaManipulator)
38
    {
39 92
        $this->proxyFactory = $proxyFactory;
40 92
        $this->originalSchemaManipulator = $originalSchemaManipulator;
41 92
    }
42
43 92
    public static function fromDefaultProxyFacyoryConfiguration(SchemaDiffProviderInterface $originalSchemaManipulator)
44
    {
45 92
        $proxyConfig = new Configuration();
46 92
        $proxyConfig->setGeneratorStrategy(new EvaluatingGeneratorStrategy());
47 92
        $proxyFactory = new LazyLoadingValueHolderFactory($proxyConfig);
48
49 92
        return new LazySchemaDiffProvider($proxyFactory, $originalSchemaManipulator);
50
    }
51
52
    /**
53
     * @return Schema
54
     */
55 34
    public function createFromSchema()
56
    {
57 34
        $originalSchemaManipulator = $this->originalSchemaManipulator;
58
59 34
        return $this->proxyFactory->createProxy(
60 34
            Schema::class,
61
            function (& $wrappedObject, $proxy, $method, array $parameters, & $initializer) use ($originalSchemaManipulator) {
62 12
                $initializer   = null;
63 12
                $wrappedObject = $originalSchemaManipulator->createFromSchema();
64
65 12
                return true;
66
            }
67 34
        );
68
    }
69
70
    /**
71
     * @param Schema $fromSchema
72
     * @return Schema
73
     */
74 33
    public function createToSchema(Schema $fromSchema)
75
    {
76 33
        $originalSchemaManipulator = $this->originalSchemaManipulator;
77
78 33
        if ($fromSchema instanceof LazyLoadingInterface && ! $fromSchema->isProxyInitialized()) {
79 32
            return $this->proxyFactory->createProxy(
80 32
                Schema::class,
81 11
                function (& $wrappedObject, $proxy,  $method, array $parameters, & $initializer) use ($originalSchemaManipulator, $fromSchema) {
82 11
                    $initializer   = null;
83 11
                    $wrappedObject = $originalSchemaManipulator->createToSchema($fromSchema);
84
85 11
                    return true;
86
                }
87 32
            );
88
        }
89
90 1
        return $this->originalSchemaManipulator->createToSchema($fromSchema);
91
    }
92
93
    /**
94
     * @param Schema $fromSchema
95
     * @param Schema $toSchema
96
     *
97
     * @return string[]
98
     */
99 32
    public function getSqlDiffToMigrate(Schema $fromSchema, Schema $toSchema)
100
    {
101
        if (
102
            $toSchema instanceof LazyLoadingInterface
103 32
            && ! $toSchema->isProxyInitialized()
104 32
        ) {
105 25
            return [];
106
        }
107
108 12
        return $this->originalSchemaManipulator->getSqlDiffToMigrate($fromSchema, $toSchema);
109
    }
110
}
111