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

DateFactoryFactory   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 15 3
A __construct() 0 6 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Arp\DateTime\Factory;
6
7
use Arp\DateTime\DateFactory;
8
use Arp\DateTime\DateIntervalFactoryInterface;
9
use Arp\DateTime\DateTimeFactoryInterface;
10
use Arp\Factory\Exception\FactoryException;
11
use Arp\Factory\FactoryInterface;
12
13
/**
14
 * @author  Alex Patterson <[email protected]>
15
 * @package Arp\DateTime\Factory
16
 */
17
final class DateFactoryFactory implements FactoryInterface
18
{
19
    /**
20
     * @var FactoryInterface
21
     */
22
    private FactoryInterface $dateTimeFactoryFactory;
23
24
    /**
25
     * @var FactoryInterface
26
     */
27
    private FactoryInterface $dateIntervalFactoryFactory;
28
29
    /**
30
     * @param FactoryInterface|null $dateTimeFactoryFactory
31
     * @param FactoryInterface|null $dateIntervalFactoryFactory
32
     */
33
    public function __construct(
34
        FactoryInterface $dateTimeFactoryFactory = null,
35
        FactoryInterface $dateIntervalFactoryFactory = null
36
    ) {
37
        $this->dateTimeFactoryFactory = $dateTimeFactoryFactory ?? new DateTimeFactoryFactory();
38
        $this->dateIntervalFactoryFactory = $dateIntervalFactoryFactory ?? new DateIntervalFactoryFactory();
39
    }
40
41
    /**
42
     * @param array $config
43
     *
44
     * @return DateFactory
45
     *
46
     * @throws FactoryException
47
     */
48
    public function create(array $config = []): DateFactory
49
    {
50
        /** @var DateTimeFactoryInterface|array $dateTimeFactory */
51
        $dateTimeFactory = $config['date_time_factory'] ?? [];
52
        if (is_array($dateTimeFactory)) {
0 ignored issues
show
introduced by
The condition is_array($dateTimeFactory) is always true.
Loading history...
53
            $dateTimeFactory = $this->dateTimeFactoryFactory->create($dateTimeFactory);
54
        }
55
56
        /** @var DateIntervalFactoryInterface|array $dateIntervalFactory */
57
        $dateIntervalFactory = $config['date_interval_factory'] ?? [];
58
        if (is_array($dateIntervalFactory)) {
0 ignored issues
show
introduced by
The condition is_array($dateIntervalFactory) is always true.
Loading history...
59
            $dateIntervalFactory = $this->dateIntervalFactoryFactory->create($dateIntervalFactory);
60
        }
61
62
        return new DateFactory($dateTimeFactory, $dateIntervalFactory);
63
    }
64
}
65