AbstractValidatorFactory   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 105
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 92.86%

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 4
dl 0
loc 105
ccs 26
cts 28
cp 0.9286
rs 10
c 0
b 0
f 0

7 Methods

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