Completed
Pull Request — master (#479)
by Michał
04:19
created

DBALConfigurationFactory::getOptionsClass()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 2
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 MIT license. For more information, see
17
 * <http://www.doctrine-project.org>.
18
 */
19
20
namespace DoctrineORMModule\Service;
21
22
use Doctrine\DBAL\Configuration;
23
use Doctrine\DBAL\Types\Type;
24
use DoctrineOrmModule\Options\Configuration as OptionsConfiguration;
25
use Interop\Container\ContainerInterface;
26
use Zend\ServiceManager\Factory\FactoryInterface;
27
28
/**
29
 * DBAL Configuration ServiceManager factory
30
 *
31
 * @license MIT
32
 * @link    http://www.doctrine-project.org/
33
 * @author  Kyle Spraggs <[email protected]>
34
 */
35
class DBALConfigurationFactory implements FactoryInterface
36
{
37
    /**
38
     * @var string
39
     */
40
    protected $name;
41
42
    /**
43
     * @param string $name
44
     */
45 72
    public function __construct($name)
46
    {
47 72
        $this->name = $name;
48 72
    }
49
50
    /**
51
     * {@inheritDoc}
52
     *
53
     * @return Configuration
54
     */
55
    public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
56
    {
57
        $config  = new Configuration();
58
        $this->setupDBALConfiguration($container, $config);
59
60
        return $config;
61
    }
62
63
    /**
64
     * @param ContainerInterface $container
65
     * @param Configuration      $config
66
     */
67 70
    public function setupDBALConfiguration(ContainerInterface $container, Configuration $config)
68
    {
69 70
        $options = $this->getOptions($container);
70 70
        $config->setResultCacheImpl($container->get($options->resultCache));
0 ignored issues
show
Documentation introduced by
The property $resultCache is declared protected in DoctrineORMModule\Options\Configuration. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
71
72 70
        $sqlLogger = $options->sqlLogger;
0 ignored issues
show
Documentation introduced by
The property $sqlLogger is declared protected in DoctrineORMModule\Options\DBALConfiguration. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
73 70
        if (is_string($sqlLogger) and $container->has($sqlLogger)) {
74
            $sqlLogger = $container->get($sqlLogger);
75
        }
76 70
        $config->setSQLLogger($sqlLogger);
77
78 70
        foreach ($options->types as $name => $class) {
0 ignored issues
show
Documentation introduced by
The property $types is declared protected in DoctrineORMModule\Options\DBALConfiguration. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
79
            if (Type::hasType($name)) {
80
                Type::overrideType($name, $class);
81
            } else {
82
                Type::addType($name, $class);
83
            }
84 70
        }
85 70
    }
86
87
    /**
88
     * @param  ContainerInterface $serviceLocator
89
     * @return OptionsConfiguration
90
     * @throws \RuntimeException
91
     */
92 72
    public function getOptions(ContainerInterface $serviceLocator)
93
    {
94 72
        $options = $serviceLocator->get('Config');
95 72
        $options = $options['doctrine'];
96 72
        $options = isset($options['configuration'][$this->name]) ? $options['configuration'][$this->name] : null;
97
98 72
        if (null === $options) {
99
            throw new \RuntimeException(
100
                sprintf(
101
                    'Configuration with name "%s" could not be found in "doctrine.configuration".',
102
                    $this->name
103
                )
104
            );
105
        }
106
107 72
        $optionsClass = $this->getOptionsClass();
108
109 72
        return new $optionsClass($options);
110
    }
111
112
    /**
113
     * @return string
114
     */
115
    protected function getOptionsClass()
116
    {
117
        return OptionsConfiguration::class;
118
    }
119
}
120