Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
17 / 17
100.00% covered (success)
100.00%
4 / 4
CRAP
100.00% covered (success)
100.00%
1 / 1
CachePool
100.00% covered (success)
100.00%
17 / 17
100.00% covered (success)
100.00%
4 / 4
11
100.00% covered (success)
100.00%
1 / 1
 add
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 remove
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 get
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
5
 create
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2/**
3 * Jingga
4 *
5 * PHP Version 8.1
6 *
7 * @package   phpOMS\DataStorage\Cache
8 * @copyright Dennis Eichhorn
9 * @license   OMS License 2.0
10 * @version   1.0.0
11 * @link      https://jingga.app
12 */
13declare(strict_types=1);
14
15namespace phpOMS\DataStorage\Cache;
16
17use phpOMS\DataStorage\Cache\Connection\ConnectionFactory;
18use phpOMS\DataStorage\Cache\Connection\NullCache;
19use phpOMS\DataStorage\DataStorageConnectionInterface;
20use phpOMS\DataStorage\DataStoragePoolInterface;
21
22/**
23 * Cache class.
24 *
25 * Responsible for storing cache implementation.
26 *
27 * @package phpOMS\DataStorage\Cache
28 * @license OMS License 2.0
29 * @link    https://jingga.app
30 * @since   1.0.0
31 */
32final class CachePool implements DataStoragePoolInterface
33{
34    /**
35     * MemCache instance.
36     *
37     * @var DataStorageConnectionInterface[]
38     * @since 1.0.0
39     */
40    private array $pool = [];
41
42    /**
43     * Add database.
44     *
45     * @param string                         $key   Database key
46     * @param DataStorageConnectionInterface $cache Cache
47     *
48     * @return bool
49     *
50     * @since 1.0.0
51     */
52    public function add(string $key, DataStorageConnectionInterface $cache) : bool
53    {
54        if (isset($this->pool[$key])) {
55            return false;
56        }
57
58        $this->pool[$key] = $cache;
59
60        return true;
61    }
62
63    /**
64     * Remove database.
65     *
66     * @param string $key Database key
67     *
68     * @return bool
69     *
70     * @since 1.0.0
71     */
72    public function remove(string $key) : bool
73    {
74        if (!isset($this->pool[$key])) {
75            return false;
76        }
77
78        unset($this->pool[$key]);
79
80        return true;
81    }
82
83    /**
84     * Requesting caching instance.
85     *
86     * @param string $key Cache to request
87     *
88     * @return DataStorageConnectionInterface
89     *
90     * @since 1.0.0
91     */
92    public function get(string $key = '') : DataStorageConnectionInterface
93    {
94        if ((!empty($key) && !isset($this->pool[$key])) || empty($this->pool)) {
95            return new NullCache();
96        }
97
98        if (empty($key)) {
99            return \reset($this->pool);
100        }
101
102        return $this->pool[$key];
103    }
104
105    /**
106     * Create Cache.
107     *
108     * @param string $key    Database key
109     * @param array  $config Database config data
110     *
111     * @return bool
112     *
113     * @since 1.0.0
114     */
115    public function create(string $key, array $config) : bool
116    {
117        if (isset($this->pool[$key])) {
118            return false;
119        }
120
121        $this->pool[$key] = ConnectionFactory::create($config);
122
123        return true;
124    }
125}