Passed
Push — master ( def3b9...9746d2 )
by Daniel
06:04
created

persistTimestampedFields()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 6
c 0
b 0
f 0
nc 1
nop 2
dl 0
loc 8
ccs 7
cts 7
cp 1
crap 2
rs 10
1
<?php
2
3
/*
4
 * This file is part of the Silverback API Components Bundle Project
5
 *
6
 * (c) Daniel West <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace Silverback\ApiComponentsBundle\Helper\Timestamped;
15
16
use Doctrine\Persistence\ManagerRegistry;
17
use Silverback\ApiComponentsBundle\AnnotationReader\TimestampedAnnotationReader;
18
use Silverback\ApiComponentsBundle\Utility\ClassMetadataTrait;
19
20
/**
21
 * @author Daniel West <[email protected]>
22
 */
23
class TimestampedDataPersister
24
{
25
    use ClassMetadataTrait;
26
27
    private TimestampedAnnotationReader $annotationReader;
28
29 7
    public function __construct(ManagerRegistry $registry, TimestampedAnnotationReader $annotationReader)
30
    {
31 7
        $this->initRegistry($registry);
32 7
        $this->annotationReader = $annotationReader;
33 7
    }
34
35 2
    public function persistTimestampedFields(object $timestamped, bool $isNew): void
36
    {
37 2
        $configuration = $this->annotationReader->getConfiguration($timestamped);
38 2
        $classMetadata = $this->getClassMetadata($timestamped);
39 2
        $classMetadata->setFieldValue($timestamped, $configuration->createdAtField, $isNew ?
40 2
            new \DateTimeImmutable() :
41 2
            $classMetadata->getFieldValue($timestamped, $configuration->createdAtField));
42 2
        $classMetadata->setFieldValue($timestamped, $configuration->modifiedAtField, new \DateTime());
43 2
    }
44
}
45