Passed
Pull Request — master (#67)
by Daniel
05:36
created

persistTimestampedFields()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 1
nop 2
dl 0
loc 8
ccs 0
cts 7
cp 0
crap 6
rs 10
c 0
b 0
f 0
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