Passed
Pull Request — master (#407)
by Kirill
08:54
created

DateTimeIntervalFactory::createOrFail()   A

Complexity

Conditions 6
Paths 6

Size

Total Lines 21
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 6
eloc 14
nc 6
nop 1
dl 0
loc 21
rs 9.2222
c 1
b 0
f 1
1
<?php
2
3
/**
4
 * This file is part of Spiral Framework package.
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
declare(strict_types=1);
11
12
namespace Spiral\Distribution\Internal;
13
14
/**
15
 * @internal DateTimeIntervalFactory is an internal library class, please do not use it in your code.
16
 * @psalm-internal Spiral\Distribution
17
 */
18
final class DateTimeIntervalFactory implements DateTimeIntervalFactoryInterface
19
{
20
    /**
21
     * @var string
22
     */
23
    private const ERROR_INVALID_INTERVAL_TYPE = 'The value of type `%s` is not a valid date interval type';
24
25
    /**
26
     * @var DateTimeFactoryInterface|null
27
     */
28
    private $factory;
29
30
    /**
31
     * @param DateTimeFactoryInterface|null $factory
32
     */
33
    public function __construct(DateTimeFactoryInterface $factory = null)
34
    {
35
        $this->factory = $factory ?? new DateTimeFactory();
36
    }
37
38
    /**
39
     * {@inheritDoc}
40
     */
41
    public function create($duration): \DateInterval
42
    {
43
        try {
44
            return $this->createOrFail($duration);
45
        } catch (\InvalidArgumentException $e) {
46
            throw $e;
47
        } catch (\Throwable $e) {
48
            throw new \InvalidArgumentException($e->getMessage(), (int)$e->getCode(), $e);
49
        }
50
    }
51
52
    /**
53
     * {@inheritDoc}
54
     */
55
    public function toDateTime(\DateInterval $interval): \DateTimeImmutable
56
    {
57
        $now = $this->factory->now();
0 ignored issues
show
Bug introduced by
The method now() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

57
        /** @scrutinizer ignore-call */ 
58
        $now = $this->factory->now();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
58
59
        return $now->add($interval);
60
    }
61
62
    /**
63
     * @param mixed $duration
64
     * @return \DateInterval
65
     * @throws \Exception
66
     */
67
    private function createOrFail($duration): \DateInterval
68
    {
69
        switch (true) {
70
            case $duration instanceof \DateInterval:
71
                return $duration;
72
73
            case $duration instanceof \DateTimeInterface:
74
                return $duration->diff($this->factory->now());
75
76
            case \is_string($duration):
77
                return new \DateInterval($duration);
78
79
            case \is_int($duration):
80
                return new \DateInterval('PT' . $duration . 'S');
81
82
            case $duration === null:
83
                return new \DateInterval('PT0S');
84
85
            default:
86
                $type = \get_debug_type($duration);
87
                throw new \InvalidArgumentException(\sprintf(self::ERROR_INVALID_INTERVAL_TYPE, $type));
88
        }
89
    }
90
}
91