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

FixedClockFactory::__invoke()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 22
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 11
c 1
b 0
f 0
nc 3
nop 3
dl 0
loc 22
rs 9.9
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Arp\LaminasDateTime\Factory\Psr;
6
7
use Arp\DateTime\DateTimeImmutableFactory;
8
use Arp\DateTime\Exception\DateTimeFactoryException;
9
use Arp\DateTime\Psr\FixedClock;
10
use Arp\LaminasFactory\AbstractFactory;
11
use Laminas\ServiceManager\Exception\ServiceNotCreatedException;
12
use Psr\Container\ContainerExceptionInterface;
13
use Psr\Container\ContainerInterface;
14
15
final class FixedClockFactory extends AbstractFactory
16
{
17
    /**
18
     * @throws ContainerExceptionInterface
19
     * @throws DateTimeFactoryException
20
     */
21
    public function __invoke(ContainerInterface $container, string $requestedName, array $options = null): FixedClock
22
    {
23
        $options = $options ?? $this->getServiceOptions($container, $requestedName, 'laminas_date_time');
24
25
        if (empty($options['date_time_immutable'])) {
26
            throw new ServiceNotCreatedException(
27
                sprintf(
28
                    'The required \'date_time_immutable\' configuration option is missing for service \'%s\'',
29
                    $requestedName
30
                ),
31
            );
32
        }
33
34
        if (is_string($options['date_time_immutable'])) {
35
            /** @var DateTimeImmutableFactory $dateTimeImmutableFactory */
36
            $dateTimeImmutableFactory = $this->getService($container, DateTimeImmutableFactory::class, $requestedName);
37
            $options['date_time_immutable'] = $dateTimeImmutableFactory->createDateTime(
38
                $options['date_time_immutable'],
39
            );
40
        }
41
42
        return new FixedClock($options['date_time_immutable']);
43
    }
44
}
45