Passed
Pull Request — master (#7)
by Alex
12:47
created

CurrentDateTimeProviderFactory   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
wmc 3
eloc 7
c 1
b 1
f 0
dl 0
loc 31
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A create() 0 9 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Arp\DateTime\Factory;
6
7
use Arp\DateTime\CurrentDateTimeProvider;
8
use Arp\DateTime\DateTimeFactoryInterface;
9
use Arp\Factory\Exception\FactoryException;
10
use Arp\Factory\FactoryInterface;
11
12
/**
13
 * @author  Alex Patterson <[email protected]>
14
 * @package Arp\DateTime\Factory
15
 */
16
class CurrentDateTimeProviderFactory implements FactoryInterface
17
{
18
    /**
19
     * @var FactoryInterface
20
     */
21
    private FactoryInterface $dateTimeFactoryFactory;
22
23
    /**
24
     * @param FactoryInterface|null $dateTimeFactoryFactory
25
     */
26
    public function __construct(FactoryInterface $dateTimeFactoryFactory = null)
27
    {
28
        $this->dateTimeFactoryFactory = $dateTimeFactoryFactory ?? new DateTimeFactoryFactory();
29
    }
30
31
    /**
32
     * @param array $config
33
     *
34
     * @return CurrentDateTimeProvider
35
     *
36
     * @throws FactoryException If the date time provider cannot be created.
37
     */
38
    public function create(array $config = []): CurrentDateTimeProvider
39
    {
40
        /** @var DateTimeFactoryInterface|array $dateTimeFactory */
41
        $dateTimeFactory = $config['date_time_factory'] ?? [];
42
        if (is_array($dateTimeFactory)) {
0 ignored issues
show
introduced by
The condition is_array($dateTimeFactory) is always true.
Loading history...
43
            $dateTimeFactory = $this->dateTimeFactoryFactory->create($dateTimeFactory);
44
        }
45
46
        return new CurrentDateTimeProvider($dateTimeFactory);
47
    }
48
}
49