Passed
Pull Request — develop (#2)
by Andreas
03:58 queued 01:12
created

CachedProductRepository::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 5
c 1
b 0
f 0
ccs 0
cts 4
cp 0
rs 9.4285
cc 1
eloc 3
nc 1
nop 2
crap 2
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
}