Passed
Pull Request — master (#407)
by Kirill
07:03
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
     * @var DateTimeFactoryInterface|null
26
     */
27
    private $factory;
28
29
    /**
30
     * @param DateTimeFactoryInterface|null $factory
31
     */
32
    public function __construct(DateTimeFactoryInterface $factory = null)
33
    {
34
        $this->factory = $factory ?? new DateTimeFactory();
35
    }
36
37
    /**
38
     * {@inheritDoc}
39
     */
40
    public function create($duration): \DateInterval
41
    {
42
        try {
43
            return $this->createOrFail($duration);
44
        } catch (\InvalidArgumentException $e) {
45
            throw $e;
46
        } catch (\Throwable $e) {
47
            throw new \InvalidArgumentException($e->getMessage(), (int)$e->getCode(), $e);
48
        }
49
    }
50
51
    /**
52
     * {@inheritDoc}
53
     */
54
    public function toDateTime(\DateInterval $interval): \DateTimeImmutable
55
    {
56
        $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

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