NowArgumentResolver::doResolve()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2.032

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 8
ccs 4
cts 5
cp 0.8
crap 2.032
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