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
|
|
|
|