NowArgumentResolver::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace Diezz\YamlToObjectMapper\Resolver;
4
5
use DateTime;
6
7
/**
8
 * Returns current timestamp in given format;
9
 * If format isn't provided by default `Y-m-d H:i:s` would be used.
10
 *
11
 * Example of usage:
12
 * $now::Y-m-d H:i:s
13
 */
14
class NowArgumentResolver extends CustomArgumentResolver
15
{
16
    private ?ScalarArgumentResolver $format;
17
18
    /**
19
     * @param ScalarArgumentResolver|null $format
20
     */
21 1
    public function __construct(?ScalarArgumentResolver $format)
22
    {
23 1
        $this->format = $format;
24 1
    }
25
26 1
    protected function doResolve($context = null): mixed
27
    {
28 1
        $now = new DateTime();
29 1
        if ($this->format !== null) {
30 1
            return $now->format($this->format->resolve($context));
31
        }
32
33
        return $now;
34
    }
35
}
36