Passed
Pull Request — master (#67)
by Daniel
06:12
created

TimestampedDataPersister   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 20
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 11
dl 0
loc 20
ccs 0
cts 11
cp 0
rs 10
c 0
b 0
f 0
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
    public function __construct(ManagerRegistry $registry, TimestampedAnnotationReader $annotationReader)
30
    {
31
        $this->initRegistry($registry);
32
        $this->annotationReader = $annotationReader;
33
    }
34
35
    public function persistTimestampedFields(object $timestamped, bool $isNew): void
36
    {
37
        $configuration = $this->annotationReader->getConfiguration($timestamped);
38
        $classMetadata = $this->getClassMetadata($timestamped);
39
        $classMetadata->setFieldValue($timestamped, $configuration->createdAtField, $isNew ?
40
            new \DateTimeImmutable() :
41
            $classMetadata->getFieldValue($timestamped, $configuration->createdAtField));
42
        $classMetadata->setFieldValue($timestamped, $configuration->modifiedAtField, new \DateTime());
43
    }
44
}
45