CachedProductStorage   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 1
dl 0
loc 39
ccs 10
cts 10
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getFromId() 0 11 2
1
<?php
2
declare(strict_types=1);
3
4
namespace codenixsv\Patterns\Structural\Proxy;
5
6
/**
7
 * Class CachedProductStorage
8
 * @package codenixsv\Patterns\Structural\Proxy
9
 */
10
class CachedProductStorage implements ProductStorageInterface
11
{
12
    /**
13
     * @var ProductStorageInterface
14
     */
15
    private $storage;
16
17
    /**
18
     * @var array
19
     */
20
    private $cache;
21
22
    /**
23
     * CachedProductStorage constructor.
24
     * @param ProductStorageInterface $storage
25
     */
26 1
    public function __construct(ProductStorageInterface $storage)
27
    {
28 1
        $this->storage = $storage;
29 1
        $this->cache = [];
30 1
    }
31
32
33
    /**
34
     * @param int $id
35
     * @return string
36
     */
37 1
    public function getFromId(int $id): string
38
    {
39 1
        if (key_exists($id, $this->cache)) {
40 1
            return $this->cache[$id] . '. From cache';
41
        }
42
43 1
        $data = $this->storage->getFromId($id);
44 1
        $this->cache[$id] = $data;
45
46 1
        return $data;
47
    }
48
}
49