Passed
Pull Request — master (#2)
by Alex
02:41
created

CascadeDeleteServiceFactory   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
eloc 18
c 1
b 0
f 0
dl 0
loc 58
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getLogger() 0 22 5
A __invoke() 0 11 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Arp\LaminasDoctrine\Factory\Repository\Persistence;
6
7
use Arp\DoctrineEntityRepository\Persistence\CascadeDeleteService;
8
use Arp\LaminasFactory\AbstractFactory;
9
use Laminas\ServiceManager\Exception\ServiceNotCreatedException;
10
use Laminas\ServiceManager\Exception\ServiceNotFoundException;
11
use Psr\Container\ContainerInterface;
12
use Psr\Log\LoggerInterface;
13
use Psr\Log\NullLogger;
14
15
/**
16
 * @author  Alex Patterson <[email protected]>
17
 * @package Arp\LaminasDoctrine\Factory\Repository\Persistence
18
 */
19
final class CascadeDeleteServiceFactory extends AbstractFactory
20
{
21
    /**
22
     * @param ContainerInterface $container
23
     * @param string             $requestedName
24
     * @param array<mixed>|null  $options
25
     *
26
     * @return CascadeDeleteService
27
     *
28
     * @throws ServiceNotCreatedException
29
     * @throws ServiceNotFoundException
30
     */
31
    public function __invoke(
32
        ContainerInterface $container,
33
        string $requestedName,
34
        array $options = null
35
    ): CascadeDeleteService {
36
        $options = $options ?? $this->getServiceOptions($container, $requestedName);
37
38
        return new CascadeDeleteService(
39
            $this->getLogger($container, $options['logger'] ?? null, $requestedName),
40
            $options['options'] ?? [],
41
            $options['collection_options'] ?? []
42
        );
43
    }
44
45
    /**
46
     * @param ContainerInterface          $container
47
     * @param LoggerInterface|string|null $logger
48
     * @param string                      $serviceName
49
     *
50
     * @return LoggerInterface
51
     *
52
     * @throws ServiceNotCreatedException
53
     * @throws ServiceNotFoundException
54
     */
55
    private function getLogger(ContainerInterface $container, $logger, string $serviceName): LoggerInterface
56
    {
57
        if (null === $logger) {
58
            return new NullLogger();
59
        }
60
61
        if (is_string($logger)) {
62
            $logger = $this->getService($container, $logger, $serviceName);
63
        }
64
65
        if (!$logger instanceof LoggerInterface) {
66
            throw new ServiceNotCreatedException(
67
                sprintf(
68
                    'The logger must be of type \'%s\'; \'%s\' provided for service \'%s\'',
69
                    LoggerInterface::class,
70
                    is_object($logger) ? get_class($logger) : gettype($logger),
71
                    $serviceName
72
                )
73
            );
74
        }
75
76
        return $logger;
77
    }
78
}
79