Completed
Push — master ( 960666...619cc6 )
by Tom
14s
created

Validator/Service/AbstractValidatorFactory.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
/*
4
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
5
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
6
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
7
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
8
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
9
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
10
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
11
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
12
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
13
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
14
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
15
 *
16
 * This software consists of voluntary contributions made by many individuals
17
 * and is licensed under the MIT license. For more information, see
18
 * <http://www.doctrine-project.org>.
19
 */
20
21
namespace DoctrineModule\Validator\Service;
22
23
use Zend\ServiceManager\FactoryInterface;
24
use Interop\Container\ContainerInterface;
25
use Zend\ServiceManager\ServiceLocatorInterface;
26
use Zend\ServiceManager\ServiceLocatorAwareInterface;
27
use DoctrineModule\Validator\Service\Exception\ServiceCreationException;
28
use Zend\Stdlib\ArrayUtils;
29
30
/**
31
 * Factory for creating NoObjectExists instances
32
 *
33
 * @license MIT
34
 * @link    http://www.doctrine-project.org/
35
 * @since   1.3.0
36
 * @author  Fabian Grutschus <[email protected]>
37
 */
38
abstract class AbstractValidatorFactory implements FactoryInterface
0 ignored issues
show
Deprecated Code introduced by
The interface Zend\ServiceManager\FactoryInterface has been deprecated with message: Use Zend\ServiceManager\Factory\FactoryInterface instead.

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
39
{
40
    const DEFAULT_OBJECTMANAGER_KEY = 'doctrine.entitymanager.orm_default';
41
42
    protected $creationOptions = [];
43
44
    protected $validatorClass;
45
46
    /**
47
     * @param ContainerInterface $container
48
     * @param array $options
49
     * @return \Doctrine\Common\Persistence\ObjectRepository
50
     * @throws ServiceCreationException
51
     */
52 3
    protected function getRepository(ContainerInterface $container, array $options)
53
    {
54 3
        if (empty($options['target_class'])) {
55 1
            throw new ServiceCreationException(sprintf(
56 1
                "Option 'target_class' is missing when creating validator %s",
57
                __CLASS__
58 1
            ));
59
        }
60
61 2
        $objectManager    = $this->getObjectManager($container, $options);
62 2
        $targetClassName  = $options['target_class'];
63 2
        $objectRepository = $objectManager->getRepository($targetClassName);
64
65 2
        return $objectRepository;
66
    }
67
68
    /**
69
     * @param ContainerInterface $container
70
     * @param array $options
71
     * @return \Doctrine\Common\Persistence\ObjectManager
72
     */
73 2
    protected function getObjectManager(ContainerInterface $container, array $options)
74
    {
75 2
        $objectManager = isset($options['object_manager']) ? $options['object_manager'] : self::DEFAULT_OBJECTMANAGER_KEY;
76 2
        if (is_string($objectManager)) {
77 1
            $objectManager = $container->get($objectManager);
78 1
        }
79
80 2
        return $objectManager;
81
    }
82
83
    /**
84
     * @param array $options
85
     * @return array
86
     */
87 2
    protected function getFields(array $options)
88
    {
89 2
        if (isset($options['fields'])) {
90 2
            return (array) $options['fields'];
91
        }
92
93
        return [];
94
    }
95
96
    /**
97
     * Helper to merge options array passed to `__invoke`
98
     * together with the options array created based on the above
99
     * helper methods.
100
     *
101
     * @param array $previousOptions
102
     * @param array $newOptions
103
     * @return array
104
     */
105 1
    protected function merge($previousOptions, $newOptions)
106
    {
107 1
        return ArrayUtils::merge($previousOptions, $newOptions, true);
108
    }
109
110
    /**
111
     * Helper method for ZF2 compatiblity.
112
     *
113
     * In ZF2 the plugin manager instance if passed to `createService`
114
     * instead of the global service manager instance (as in ZF3).
115
     *
116
     * @param ContainerInterface $container
117
     * @return ContainerInterface
118
     */
119 2
    protected function container(ContainerInterface $container)
120
    {
121 2
        if ($container instanceof ServiceLocatorAwareInterface) {
0 ignored issues
show
The class Zend\ServiceManager\ServiceLocatorAwareInterface does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
122
            $container = $container->getServiceLocator();
123
        }
124
125 2
        return $container;
126
    }
127
128 1
    public function createService(ServiceLocatorInterface $serviceLocator)
129
    {
130 1
        return $this($serviceLocator, $this->validatorClass, $this->creationOptions);
131
    }
132
133 1
    public function setCreationOptions(array $options)
134
    {
135 1
        $this->creationOptions = $options;
136 1
    }
137
}
138