FixedTimeService   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 25
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 7
dl 0
loc 25
rs 10
c 0
b 0
f 0
wmc 5

4 Methods

Rating   Name   Duplication   Size   Complexity  
A fromStamp() 0 3 1
A __construct() 0 3 1
A fromString() 0 8 2
A measure() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Termyn\Timekeeper;
6
7
use DateTimeImmutable;
8
use InvalidArgumentException;
9
10
final class FixedTimeService implements TimeService
11
{
12
    public function __construct(
13
        private readonly DateTimeImmutable $dateTime,
14
    ) {
15
    }
16
17
    public static function fromStamp(int $seconds): self
18
    {
19
        return self::fromString(sprintf('%s', $seconds), 'U');
20
    }
21
22
    public static function fromString(string $dateTime, string $format): self
23
    {
24
        $dateTime = DateTimeImmutable::createFromFormat($format, $dateTime);
25
        if (! $dateTime) {
0 ignored issues
show
introduced by
$dateTime is of type DateTimeImmutable, thus it always evaluated to true.
Loading history...
26
            throw new InvalidArgumentException(sprintf('Invalid datetime "%s" (%s).', $dateTime, $format));
27
        }
28
29
        return new self($dateTime);
30
    }
31
32
    public function measure(): DateTimeImmutable
33
    {
34
        return $this->dateTime;
35
    }
36
}
37