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

TimestampedDataPersister   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 20
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 11
c 0
b 0
f 0
dl 0
loc 20
ccs 11
cts 11
cp 1
rs 10
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A persistTimestampedFields() 0 8 2
A __construct() 0 4 1
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