Completed
Branch develop (d8e67e)
by Timothy
04:10
created

SQLDriver   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 4
Bugs 0 Features 3
Metric Value
wmc 6
c 4
b 0
f 3
lcom 1
cbo 1
dl 0
loc 82
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A get() 0 15 2
A set() 0 11 1
A invalidate() 0 7 1
A invalidateAll() 0 4 1
1
<?php
2
/**
3
 * Ion
4
 *
5
 * Building blocks for web development
6
 *
7
 * @package	 Ion
8
 * @author	  Timothy J. Warren
9
 * @copyright   Copyright (c) 2015 - 2016
10
 * @license	 MIT
11
 */
12
13
namespace Aviat\Ion\Cache\Driver;
14
15
use Aviat\Ion\Di\ContainerInterface;
16
use Aviat\Ion\Json;
17
use Aviat\AnimeClient\Model\DB;
18
19
/**
20
 * Driver for caching via a traditional SQL database
21
 */
22
class SQLDriver extends DB implements \Aviat\Ion\Cache\CacheDriverInterface  {
23
24
	/**
25
	 * The query builder object
26
	 * @var object $db
27
	 */
28
	protected $db;
29
30
	/**
31
	 * Create the driver object
32
	 */
33
	public function __construct(ContainerInterface $container)
34
	{
35
		parent::__construct($container);
36
		$this->db = \Query($this->db_config['collection']);
37
	}
38
39
	/**
40
	 * Retreive a value from the cache backend
41
	 *
42
	 * @param string $key
43
	 * @return mixed
44
	 */
45
	public function get($key)
46
	{
47
		$query = $this->db->select('value')
48
			->from('cache')
49
			->where('key', $key)
50
			->get();
51
			
52
		$row = $query->fetch(\PDO::FETCH_ASSOC);
53
		if ( ! empty($row))
54
		{
55
			return unserialize($row['value']);
56
		}
57
		
58
		return NULL;
59
	}
60
61
	/**
62
	 * Set a cached value
63
	 *
64
	 * @param string $key
65
	 * @param mixed $value
66
	 * @return CacheDriverInterface
0 ignored issues
show
Documentation introduced by
Should the return type not be SQLDriver?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
67
	 */
68
	public function set($key, $value)
69
	{
70
		$this->db->set([
71
			'key' => $key,
72
			'value' => serialize($value),
73
		]);
74
		
75
		$this->db->insert('cache');
76
77
		return $this;
78
	}
79
80
	/**
81
	 * Invalidate a cached value
82
	 *
83
	 * @param string $key
84
	 * @return CacheDriverInterface
0 ignored issues
show
Documentation introduced by
Should the return type not be SQLDriver?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
85
	 */
86
	public function invalidate($key)
87
	{
88
		$this->db->where('key', $key)
89
			->delete('cache');
90
			
91
		return $this;
92
	}
93
94
	/**
95
	 * Clear the contents of the cache
96
	 *
97
	 * @return void
98
	 */
99
	public function invalidateAll()
100
	{
101
		$this->db->truncate('cache');
102
	}
103
}
104
// End of SQLDriver.php