Passed
Pull Request — master (#2)
by Alex
05:45 queued 03:25
created

DateTimeStrategyFactory   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 2
eloc 9
c 1
b 0
f 0
dl 0
loc 32
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 18 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Arp\LaminasDateTime\Factory\Hydrator\Strategy;
6
7
use Arp\LaminasFactory\AbstractFactory;
8
use Interop\Container\ContainerInterface;
9
use Laminas\Hydrator\Strategy\DateTimeFormatterStrategy;
10
use Laminas\Hydrator\Strategy\Exception\InvalidArgumentException;
11
use Laminas\ServiceManager\Exception\ServiceNotCreatedException;
12
13
/**
14
 * @author  Alex Patterson <[email protected]>
15
 * @package Arp\LaminasDateTime\Factory\Hydrator\Strategy
16
 */
17
final class DateTimeStrategyFactory extends AbstractFactory
18
{
19
    /**
20
     * @noinspection PhpMissingParamTypeInspection
21
     *
22
     * @param ContainerInterface $container
23
     * @param string             $requestedName
24
     * @param array|null         $options
25
     *
26
     * @return DateTimeFormatterStrategy
27
     *
28
     * @throws ServiceNotCreatedException
29
     * @throws InvalidArgumentException
30
     */
31
    public function __invoke(
32
        ContainerInterface $container,
33
        $requestedName,
34
        array $options = null
35
    ): DateTimeFormatterStrategy {
36
        $options = $options ?? $this->getServiceOptions($container, $requestedName);
37
38
        $format = $options['format'] ?? null;
39
        if (null === $format) {
40
            throw new ServiceNotCreatedException(
41
                sprintf(
42
                    'The required \'format\' configuration option is missing for service \'%s\'',
43
                    $requestedName
44
                )
45
            );
46
        }
47
48
        return new DateTimeFormatterStrategy($format, $options['timezone'] ?? null, $options['fallback'] ?? false);
49
    }
50
}
51