setCreationOptions()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php // @codingStandardsIgnoreFile
2
/**
3
 * Polder Knowledge / entityservice-zend-validator (https://polderknowledge.com)
4
 *
5
 * @link https://github.com/polderknowledge/entityservice-zend-validator for the canonical source repository
6
 * @copyright Copyright (c) 2016 Polder Knowledge (https://polderknowledge.com)
7
 * @license https://github.com/polderknowledge/entityservice-zend-validator/blob/master/LICENSE.md MIT
8
 */
9
10
namespace PolderKnowledge\EntityService\Validator\Service;
11
12
use Interop\Container\ContainerInterface;
13
use InvalidArgumentException;
14
use PolderKnowledge\EntityService\EntityServiceInterface;
15
use RuntimeException;
16
use Zend\ServiceManager\FactoryInterface;
17
use Zend\ServiceManager\ServiceLocatorInterface;
18
use Zend\Validator\ValidatorInterface;
19
20
// @codeCoverageIgnoreStart
21
if (!interface_exists('Zend\ServiceManager\MutableCreationOptionsInterface')) {
22
    interface NewInterface { }
23
    class_alias(NewInterface::class, 'Zend\ServiceManager\MutableCreationOptionsInterface', false);
24
}
25
// @codeCoverageIgnoreEnd
26
27
/**
28
 * Base class for Entity validator Factories.
29
 * Contains a until method to create the EntityService Required for these validators
30
 */
31
abstract class AbstractEntityValidatorFactory implements
32
    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...
33
    \Zend\ServiceManager\MutableCreationOptionsInterface
34
{
35
    protected $options = [];
36
37
    /**
38
     * Uses the EntityServiceManager to fetch an EntityService for the entity set in the options array.
39
     *
40
     * @param ContainerInterface $container
41
     * @param string $requestedName
42
     * @param array|null $options The options passed to the service manager.
43
     * @return ValidatorInterface
44
     */
45 9
    public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
46
    {
47 9
        if (!$options || !is_array($options)) {
48 3
            throw new InvalidArgumentException('You must provide options in order to create the validator.');
49
        }
50
51 6
        if (!array_key_exists('entity', $options)) {
52 3
            throw new RuntimeException('The "entity" option is required when creating the validator.');
53
        }
54
55 3
        $entityService = $this->createEntityService($container, $options['entity']);
56
57 3
        return $this->createValidator($entityService, $options);
58
    }
59
60 3
    public function createService(ServiceLocatorInterface $serviceLocator)
61
    {
62
        // @codeCoverageIgnoreStart
63
        if (method_exists($serviceLocator, 'getServiceLocator')) {
64
            $serviceLocator = $serviceLocator->getServiceLocator();
65
        }
66
        // @codeCoverageIgnoreEnd
67
68 3
        $validator = $this($serviceLocator, '', $this->options);
69
70 3
        $this->options = null;
0 ignored issues
show
Documentation Bug introduced by
It seems like null of type null is incompatible with the declared type array of property $options.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
71
72 3
        return $validator;
73
    }
74
75 3
    public function setCreationOptions(array $options)
76
    {
77 3
        $this->options = $options;
78 3
    }
79
80
    /**
81
     * Fetches a EntityServiceInterface instance from the EntityServiceManager
82
     *
83
     * @param ContainerInterface $container
84
     * @param string $entityName
85
     * @return EntityServiceInterface
86
     */
87 6
    protected function createEntityService(ContainerInterface $container, $entityName)
88
    {
89 6
        $entityServiceManager = $container->get('EntityServiceManager');
90
91 6
        return $entityServiceManager->get($entityName);
92
    }
93
94
    abstract protected function createValidator(EntityServiceInterface $entityService, array $options = null);
95
}
96