Passed
Pull Request — master (#5)
by Alex
02:16
created

AbstractEntityValidatorFactory::__invoke()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 36
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 3
eloc 19
c 1
b 0
f 1
nc 3
nop 3
dl 0
loc 36
rs 9.6333
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Arp\LaminasDoctrine\Factory\Validator;
6
7
use Arp\LaminasDoctrine\Repository\RepositoryManager;
8
use Arp\LaminasDoctrine\Validator\AbstractEntityValidator;
9
use Arp\LaminasFactory\AbstractFactory;
10
use Laminas\ServiceManager\Exception\InvalidServiceException;
11
use Laminas\ServiceManager\Exception\ServiceNotCreatedException;
12
use Laminas\ServiceManager\Exception\ServiceNotFoundException;
13
use Psr\Container\ContainerExceptionInterface;
14
use Psr\Container\ContainerInterface;
15
16
abstract class AbstractEntityValidatorFactory extends AbstractFactory
17
{
18
    /**
19
     * @return class-string<AbstractEntityValidator>
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string<AbstractEntityValidator> at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string<AbstractEntityValidator>.
Loading history...
20
     */
21
    abstract protected function getDefaultClassName(): string;
22
23
    /**
24
     * @throws ServiceNotCreatedException
25
     * @throws InvalidServiceException
26
     * @throws ServiceNotFoundException
27
     * @throws ContainerExceptionInterface
28
     */
29
    public function __invoke(
30
        ContainerInterface $container,
31
        string $requestedName,
32
        array $options = null
33
    ): AbstractEntityValidator {
34
        $options = $options ?? $this->getServiceOptions($container, $requestedName, 'validators');
35
36
        $entityName = $options['entity_name'] ?? null;
37
        if (null === $entityName) {
38
            throw new ServiceNotCreatedException(
39
                sprintf(
40
                    'The required \'entity_name\' configuration option is missing for service \'%s\'',
41
                    $requestedName
42
                )
43
            );
44
        }
45
46
        $fieldNames = $options['field_names'] ?? [];
47
        if (empty($fieldNames)) {
48
            throw new ServiceNotCreatedException(
49
                sprintf(
50
                    'The required \'field_names\' configuration option is missing for service \'%s\'',
51
                    $requestedName
52
                )
53
            );
54
        }
55
56
        /** @var RepositoryManager $repositoryManager */
57
        $repositoryManager = $this->getService($container, RepositoryManager::class, $requestedName);
58
59
        /** @var class-string<AbstractEntityValidator> $className */
60
        $className = $options['class_name'] ?? $this->getDefaultClassName();
61
        return new $className(
62
            $repositoryManager->get($entityName),
63
            $fieldNames,
64
            $options
65
        );
66
    }
67
}
68