|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Wambo\Catalog; |
|
4
|
|
|
|
|
5
|
|
|
use Wambo\Catalog\Cache\CacheInterface; |
|
6
|
|
|
use Wambo\Catalog\Exception\RepositoryException; |
|
7
|
|
|
use Wambo\Catalog\Model\Product; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Class CachedProductRepository fetches Product models from the Storage and writes Products back to the Storage |
|
11
|
|
|
* while using a cache layer. |
|
12
|
|
|
* |
|
13
|
|
|
* @package Wambo\Catalog |
|
14
|
|
|
*/ |
|
15
|
|
|
class CachedProductRepository implements ProductRepositoryInteface |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* @var CacheInterface |
|
19
|
|
|
*/ |
|
20
|
|
|
private $cache; |
|
21
|
|
|
/** |
|
22
|
|
|
* @var ProductRepository |
|
23
|
|
|
*/ |
|
24
|
|
|
private $productRepository; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Creates a new CachedProductRepository instance. |
|
28
|
|
|
* |
|
29
|
|
|
* @param CacheInterface $cache |
|
30
|
|
|
* @param ProductRepository $productRepository |
|
31
|
|
|
* |
|
32
|
|
|
*/ |
|
33
|
|
|
public function __construct(CacheInterface $cache, ProductRepository $productRepository) |
|
34
|
|
|
{ |
|
35
|
|
|
$this->cache = $cache; |
|
36
|
|
|
$this->productRepository = $productRepository; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Get all products |
|
41
|
|
|
* |
|
42
|
|
|
* @return Product[] |
|
43
|
|
|
* |
|
44
|
|
|
* @throws RepositoryException If fetching the products failed |
|
45
|
|
|
*/ |
|
46
|
|
|
public function getProducts() |
|
47
|
|
|
{ |
|
48
|
|
|
$cacheKey = $this->cache->getKey(self::class, "products"); |
|
49
|
|
|
if ($this->cache->has($cacheKey)) { |
|
50
|
|
|
return $this->cache->get($cacheKey); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
$products = $this->productRepository->getProducts(); |
|
54
|
|
|
$this->cache->store($cacheKey, $products); |
|
55
|
|
|
|
|
56
|
|
|
return $products; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
public function getById(string $id) |
|
60
|
|
|
{ |
|
61
|
|
|
throw new RepositoryException("Not implemented yet"); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
public function add(Product $product) |
|
65
|
|
|
{ |
|
66
|
|
|
throw new RepositoryException("Not implemented yet"); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
public function remove(Product $product) |
|
70
|
|
|
{ |
|
71
|
|
|
throw new RepositoryException("Not implemented yet"); |
|
72
|
|
|
} |
|
73
|
|
|
} |