Test Setup Failed
Push — master ( eaec61...fed08a )
by Ion
02:23
created

DoctrineObjectStorageAdapter::get()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 9
ccs 5
cts 5
cp 1
crap 2
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Infrastructure\ObjectStorage;
6
7
use App\Infrastructure\Exception\ObjectNotFoundException;
8
use App\Infrastructure\Persistence\Entity\ObjectEntry as EntryEntity;
9
use App\Infrastructure\Persistence\Repository\ObjectEntryRepository;
10
use DateTime;
11
use DateTimeZone;
12
13
class DoctrineObjectStorageAdapter implements ObjectStorageAdapter
14
{
15
    protected ObjectEntryRepository $objectEntryRepository;
16
17 11
    public function __construct(ObjectEntryRepository $objectEntryRepository)
18
    {
19 11
        $this->objectEntryRepository = $objectEntryRepository;
20 11
    }
21
22
    /**
23
     * {@inheritdoc}
24
     */
25 8
    public function store(string $key, $data, DateTime $timestamp): void
26
    {
27 8
        $objectEntry = new EntryEntity();
28 8
        $objectEntry->setKey($key);
29 8
        $objectEntry->setValue($data);
30 8
        $objectEntry->setCreatedAt($this->toUTC($timestamp));
31
32 8
        $this->objectEntryRepository->save($objectEntry);
33 8
    }
34
35
    /**
36
     * {@inheritdoc}
37
     */
38 9
    public function get(string $key, DateTime $timestamp)
39
    {
40 9
        $entry = $this->objectEntryRepository->findByKeyAtTime($key, $this->toUTC($timestamp));
41
42 9
        if (!$entry) {
43 4
            throw new ObjectNotFoundException($key, $timestamp);
44
        }
45
46 8
        return $entry->getValue();
47
    }
48
49
    /**
50
     * {@inheritdoc}
51
     */
52 7
    public function clear(): void
53
    {
54 7
        $this->objectEntryRepository->deleteAll();
55 7
    }
56
57 10
    private function toUTC(DateTime $dateTime): DateTime
58
    {
59 10
        return (clone $dateTime)->setTimezone(new DateTimeZone('UTC'));
60
    }
61
}
62