IsEntityValidatorFactory::__invoke()   A
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 34
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 19
c 1
b 0
f 0
nc 5
nop 3
dl 0
loc 34
rs 9.3222
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Arp\LaminasEntity\Factory\Validator;
6
7
use Arp\DoctrineEntityRepository\EntityRepositoryInterface;
8
use Arp\LaminasEntity\Service\EntityRepositoryManager;
9
use Arp\LaminasEntity\Validator\IsEntityValidator;
10
use Arp\LaminasFactory\AbstractFactory;
11
use Arp\LaminasFactory\Exception\ServiceNotCreatedException;
12
use Interop\Container\ContainerInterface;
13
14
/**
15
 * @author  Alex Patterson <[email protected]>
16
 * @package Arp\LaminasEntity\Factory\Validator
17
 */
18
class IsEntityValidatorFactory extends AbstractFactory
19
{
20
    /**
21
     * @var string
22
     */
23
    protected $defaultClassName = IsEntityValidator::class;
24
25
    /**
26
     * @param ContainerInterface $container
27
     * @param string             $requestedName
28
     * @param array|null         $options
29
     *
30
     * @return IsEntityValidator
31
     */
32
    public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
33
    {
34
        $options = $options ?? $this->getServiceOptions($container, $requestedName, 'validators');
35
36
        $className  = $options['class_name']  ?? $this->defaultClassName;
37
        $entityName = $options['entity_name'] ?? null;
38
        $fieldNames = $options['field_names'] ?? null;
39
        $options    = $options['options'] ?? [];
40
41
        if (null === $entityName || ! is_string($entityName)) {
42
            throw new ServiceNotCreatedException(
43
                sprintf(
44
                    'The required \'entity_name\' configuration option is missing or invalid for service \'%s\'',
45
                    $requestedName
46
                )
47
            );
48
        }
49
50
        /** @var EntityRepositoryInterface $repository */
51
        $repository = $this->getService(
52
            $container->get(EntityRepositoryManager::class),
53
            $entityName,
54
            $requestedName
55
        );
56
57
        if (empty($fieldNames)) {
58
            $fieldNames[] = 'id';
59
        }
60
61
        if (! array_key_exists('useContext', $options)) {
62
            $options['useContext'] = true;
63
        }
64
65
        return new $className($repository, $fieldNames, $options);
66
    }
67
}
68