Completed
Push — master ( f2b8b4...a21374 )
by Tom
24s queued 11s
created

AbstractValidatorFactory::getObjectManager()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 5
cts 5
cp 1
rs 9.9332
c 0
b 0
f 0
cc 2
nc 2
nop 2
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace DoctrineModule\Validator\Service;
6
7
use Doctrine\Common\Persistence\ObjectManager;
8
use Doctrine\Common\Persistence\ObjectRepository;
9
use DoctrineModule\Validator\Service\Exception\ServiceCreationException;
10
use Interop\Container\ContainerInterface;
11
use Laminas\ServiceManager\FactoryInterface;
12
use Laminas\ServiceManager\ServiceLocatorAwareInterface;
13
use Laminas\ServiceManager\ServiceLocatorInterface;
14
use Laminas\Stdlib\ArrayUtils;
15
use function is_string;
16
use function sprintf;
17
18
/**
19
 * Factory for creating NoObjectExists instances
20
 *
21
 * @link    http://www.doctrine-project.org/
22
 */
23
// phpcs:disable SlevomatCodingStandard.Classes.SuperfluousAbstractClassNaming
24
abstract class AbstractValidatorFactory implements FactoryInterface
0 ignored issues
show
Deprecated Code introduced by
The interface Laminas\ServiceManager\FactoryInterface has been deprecated with message: Use Laminas\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...
25
{
26
// phpcs:enable SlevomatCodingStandard.Classes.SuperfluousAbstractClassNaming
27
    public const DEFAULT_OBJECTMANAGER_KEY = 'doctrine.entitymanager.orm_default';
28
29
    /** @var mixed[] */
30
    protected $creationOptions = [];
31
32
    /** @var string $validatorClass */
33
    protected $validatorClass;
34
35
    /**
36
     * @param mixed[] $options
37
     *
38
     * @throws ServiceCreationException
39
     */
40 3
    protected function getRepository(ContainerInterface $container, ?array $options = null) : ObjectRepository
41
    {
42 3
        if (empty($options['target_class'])) {
43 1
            throw new ServiceCreationException(sprintf(
44 1
                "Option 'target_class' is missing when creating validator %s",
45 1
                self::class
46
            ));
47
        }
48
49 2
        $objectManager   = $this->getObjectManager($container, $options);
50 2
        $targetClassName = $options['target_class'];
51
52 2
        return $objectManager->getRepository($targetClassName);
53
    }
54
55
    /**
56
     * @param mixed[] $options
57
     */
58 2
    protected function getObjectManager(ContainerInterface $container, ?array $options = null) : ObjectManager
59
    {
60 2
        $objectManager = $options['object_manager'] ?? self::DEFAULT_OBJECTMANAGER_KEY;
61
62 2
        if (is_string($objectManager)) {
63 1
            $objectManager = $container->get($objectManager);
64
        }
65
66 2
        return $objectManager;
67
    }
68
69
    /**
70
     * @param mixed[] $options
71
     *
72
     * @return mixed[]
73
     */
74 2
    protected function getFields(array $options) : array
75
    {
76 2
        if (isset($options['fields'])) {
77 2
            return (array) $options['fields'];
78
        }
79
80
        return [];
81
    }
82
83
    /**
84
     * Helper to merge options array passed to `__invoke`
85
     * together with the options array created based on the above
86
     * helper methods.
87
     *
88
     * @param mixed[] $previousOptions
89
     * @param mixed[] $newOptions
90
     *
91
     * @return mixed[]
92
     */
93 1
    protected function merge(array $previousOptions, array $newOptions) : array
94
    {
95 1
        return ArrayUtils::merge($previousOptions, $newOptions, true);
96
    }
97
98
    /**
99
     * Helper method for ZF2 compatiblity.
100
     *
101
     * In ZF2 the plugin manager instance if passed to `createService`
102
     * instead of the global service manager instance (as in ZF3).
103
     */
104 2
    protected function container(ContainerInterface $container) : ContainerInterface
105
    {
106 2
        if ($container instanceof ServiceLocatorAwareInterface) {
0 ignored issues
show
Bug introduced by
The class Laminas\ServiceManager\S...ceLocatorAwareInterface 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...
107
            $container = $container->getServiceLocator();
108
        }
109
110 2
        return $container;
111
    }
112
113
    /**
114
     * {@inheritDoc}
115
     */
116 1
    public function createService(ServiceLocatorInterface $serviceLocator)
117
    {
118 1
        return $this($serviceLocator, $this->validatorClass, $this->creationOptions);
119
    }
120
121
    /**
122
     * @param mixed[] $options
123
     */
124 1
    public function setCreationOptions(array $options) : void
125
    {
126 1
        $this->creationOptions = $options;
127 1
    }
128
}
129