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

ObjectStore   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 24
c 1
b 0
f 0
dl 0
loc 42
ccs 25
cts 25
cp 1
rs 10
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A store() 0 8 1
A get() 0 20 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Application\Service;
6
7
use App\Domain\Model\ObjectEntry;
8
use App\Domain\Service\ObjectStoreInterface;
9
use App\Infrastructure\Exception\ObjectNotFoundException;
10
use App\Infrastructure\ObjectStorage\ObjectStorageAdapter;
11
use Psr\Log\LoggerAwareInterface;
12
use Psr\Log\LoggerAwareTrait;
13
14
class ObjectStore implements ObjectStoreInterface, LoggerAwareInterface
15
{
16
    use LoggerAwareTrait;
17
18
    private ObjectStorageAdapter $adapter;
19
20 4
    public function __construct(ObjectStorageAdapter $adapter)
21
    {
22 4
        $this->adapter = $adapter;
23 4
    }
24
25 2
    public function store(ObjectEntry $entry, \DateTime $timestamp): void
26
    {
27 2
        $this->adapter->store($entry->getKey(), $entry->getValue(), $timestamp);
28 2
        $this->logger->info('Object stored', [
29 2
            'key' => $entry->getKey(),
30 2
            'value' => $entry->getValue(),
31 2
            'adapter' => get_class($this->adapter),
32 2
            'timestamp' => $timestamp->getTimestamp(),
33
        ]);
34 2
    }
35
36 3
    public function get(string $key, \DateTime $timestamp): ?ObjectEntry
37
    {
38
        try {
39 3
            $value = $this->adapter->get($key, $timestamp);
40 2
            $this->logger->info('Object fetched', [
41 2
                'key' => $key,
42 2
                'value' => $value,
43 2
                'adapter' => get_class($this->adapter),
44 2
                'timestamp' => $timestamp->getTimestamp(),
45
            ]);
46
47 2
            return new ObjectEntry($key, $value);
48 2
        } catch (ObjectNotFoundException $e) {
49 2
            $this->logger->warning('Object not found', [
50 2
                'key' => $key,
51 2
                'adapter' => get_class($this->adapter),
52 2
                'timestamp' => $timestamp->getTimestamp(),
53
            ]);
54
55 2
            return null;
56
        }
57
    }
58
}
59