Passed
Pull Request — master (#5)
by Alex
09:53 queued 08:01
created

DateTimeImmutableFactoryFactory   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 16
c 1
b 0
f 0
dl 0
loc 30
rs 10
wmc 4

1 Method

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 25 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Arp\LaminasDateTime\Factory;
6
7
use Arp\DateTime\DateTimeImmutableFactory;
8
use Arp\DateTime\Exception\DateTimeFactoryException;
9
use Arp\LaminasFactory\AbstractFactory;
10
use Laminas\ServiceManager\Exception\ServiceNotCreatedException;
11
use Psr\Container\ContainerExceptionInterface;
12
use Psr\Container\ContainerInterface;
13
14
final class DateTimeImmutableFactoryFactory extends AbstractFactory
15
{
16
    /**
17
     * @throws ContainerExceptionInterface
18
     */
19
    public function __invoke(
20
        ContainerInterface $container,
21
        string $requestedName,
22
        array $options = null
23
    ): DateTimeImmutableFactory {
24
        $options = $options ?? $this->getServiceOptions($container, $requestedName, 'laminas_date_time');
25
26
        if (isset($options['date_time_zone_factory']) && is_string($options['date_time_zone_factory'])) {
27
            $options['date_time_zone_factory'] = $this->getService(
28
                $container,
29
                $options['date_time_zone_factory'],
30
                $requestedName
31
            );
32
        }
33
34
        try {
35
            return new DateTimeImmutableFactory(
36
                $options['class_name'] ?? null,
37
                $options['date_time_zone_factory'] ?? null,
38
            );
39
        } catch (DateTimeFactoryException $e) {
40
            throw new ServiceNotCreatedException(
41
                sprintf('Failed to create date time immutable factory service \'%s\'', $requestedName),
42
                $e->getCode(),
43
                $e,
44
            );
45
        }
46
    }
47
}
48