Passed
Pull Request — develop (#2)
by Andreas
05:17
created

CachedProductRepository::remove()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 4
c 1
b 0
f 0
ccs 0
cts 2
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php
2
3
namespace Wambo\Catalog;
4
5
use Psr\Cache\CacheItemPoolInterface;
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 ProductRepositoryInterface
16
{
17
    /**
18
     * @var CacheItemPoolInterface
19
     */
20
    private $cache;
21
    /**
22
     * @var ProductRepository
23
     */
24
    private $productRepository;
25
26
    /**
27
     * Creates a new CachedProductRepository instance.
28
     *
29
     * @param CacheItemPoolInterface $cache             A cache backend for storing the results
30
     * @param ProductRepository      $productRepository A product repository for reading and writing product data
31
     *
32
     */
33 2
    public function __construct(CacheItemPoolInterface $cache, ProductRepository $productRepository)
34
    {
35 2
        $this->cache = $cache;
36 2
        $this->productRepository = $productRepository;
37 2
    }
38
39
    /**
40
     * Get all products
41
     *
42
     * @return Product[]
43
     *
44
     * @throws RepositoryException If fetching the products failed
45
     */
46 2
    public function getProducts()
47
    {
48 2
        $cacheKey = "products";
49 2
        if ($this->cache->hasItem($cacheKey)) {
50
51
            // return from cache
52 1
            $cacheItem = $this->cache->getItem($cacheKey);
53 1
            $cachedProducts = $cacheItem->get();
54 1
            return $cachedProducts;
55
        }
56
57 1
        $products = $this->productRepository->getProducts();
58
59
        // save to cache
60 1
        $cacheItem = $this->cache->getItem($cacheKey);
61 1
        $cacheItem->set($products);
62
63 1
        return $products;
64
    }
65
66
    public function getById(string $id)
67
    {
68
        throw new RepositoryException("Not implemented yet");
69
    }
70
71
    public function add(Product $product)
72
    {
73
        throw new RepositoryException("Not implemented yet");
74
    }
75
76
    public function remove(Product $product)
77
    {
78
        throw new RepositoryException("Not implemented yet");
79
    }
80
}