Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
25 / 25
100.00% covered (success)
100.00%
8 / 8
CRAP
100.00% covered (success)
100.00%
1 / 1
ConnectionAbstract
100.00% covered (success)
100.00%
25 / 25
100.00% covered (success)
100.00%
8 / 8
16
100.00% covered (success)
100.00%
1 / 1
 getType
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getStatus
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getCache
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getHost
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getPort
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 __destruct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 close
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 dataType
100.00% covered (success)
100.00%
17 / 17
100.00% covered (success)
100.00%
1 / 1
9
1<?php
2/**
3 * Jingga
4 *
5 * PHP Version 8.1
6 *
7 * @package   phpOMS\DataStorage\Cache\Connection
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\Connection;
16
17use phpOMS\Contract\SerializableInterface;
18use phpOMS\DataStorage\Cache\CacheStatus;
19use phpOMS\DataStorage\Cache\CacheType;
20
21/**
22 * Cache handler.
23 *
24 * Handles the cache connection.
25 * Implementing wrapper functions for multiple caches is planned (far away).
26 *
27 * @package phpOMS\DataStorage\Cache\Connection
28 * @license OMS License 2.0
29 * @link    https://jingga.app
30 * @since   1.0.0
31 */
32abstract class ConnectionAbstract implements ConnectionInterface
33{
34    /**
35     * Connection object.
36     *
37     * This can be used externally to define queries and execute them.
38     *
39     * @var mixed
40     * @since 1.0.0
41     */
42    protected $con = null;
43
44    /**
45     * Database data.
46     *
47     * @var null|string[]
48     * @since 1.0.0
49     */
50    protected ?array $dbdata = null;
51
52    /**
53     * Database type.
54     *
55     * @var string
56     * @since 1.0.0
57     */
58    protected string $type = CacheType::UNDEFINED;
59
60    /**
61     * Database status.
62     *
63     * @var int
64     * @since 1.0.0
65     */
66    protected int $status = CacheStatus::CLOSED;
67
68    /**
69     * {@inheritdoc}
70     */
71    public function getType() : string
72    {
73        return $this->type;
74    }
75
76    /**
77     * {@inheritdoc}
78     */
79    public function getStatus() : int
80    {
81        return $this->status;
82    }
83
84    /**
85     * Get database name.
86     *
87     * @return string
88     *
89     * @since 1.0.0
90     */
91    public function getCache() : string
92    {
93        return (string) ($this->dbdata['db'] ?? '');
94    }
95
96    /**
97     * Get database host.
98     *
99     * @return string
100     *
101     * @since 1.0.0
102     */
103    public function getHost() : string
104    {
105        return $this->dbdata['host'] ?? '';
106    }
107
108    /**
109     * Get database port.
110     *
111     * @return int
112     *
113     * @since 1.0.0
114     */
115    public function getPort() : int
116    {
117        return (int) ($this->dbdata['port'] ?? 0);
118    }
119
120    /**
121     * Object destructor.
122     *
123     * Sets the database connection to null
124     *
125     * @since 1.0.0
126     */
127    public function __destruct()
128    {
129        $this->close();
130    }
131
132    /**
133     * Closes the chache.
134     *
135     * @since 1.0.0
136     */
137    public function close() : void
138    {
139        $this->con    = null;
140        $this->status = CacheStatus::CLOSED;
141    }
142
143    /**
144     * Analyze caching data type.
145     *
146     * @param mixed $value Data to cache
147     *
148     * @return int Returns the cache type for a value
149     *
150     * @throws \InvalidArgumentException This exception is thrown if an unsupported datatype is used
151     *
152     * @since 1.0.0
153     */
154    protected function dataType(mixed $value) : int
155    {
156        if (\is_int($value)) {
157            return CacheValueType::_INT;
158        } elseif (\is_float($value)) {
159            return CacheValueType::_FLOAT;
160        } elseif (\is_string($value)) {
161            return CacheValueType::_STRING;
162        } elseif (\is_bool($value)) {
163            return CacheValueType::_BOOL;
164        } elseif (\is_array($value)) {
165            return CacheValueType::_ARRAY;
166        } elseif ($value === null) {
167            return CacheValueType::_NULL;
168        } elseif ($value instanceof SerializableInterface) {
169            return CacheValueType::_SERIALIZABLE;
170        } elseif ($value instanceof \JsonSerializable) {
171            return CacheValueType::_JSONSERIALIZABLE;
172        }
173
174        throw new \InvalidArgumentException('Invalid value type.');
175    }
176}