Completed
Push — master ( 197710...89dabe )
by Aimeos
07:28
created

Mysql::setMultiple()   D

Complexity

Conditions 10
Paths 604

Size

Total Lines 56
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 10
eloc 32
nc 604
nop 3
dl 0
loc 56
rs 4.7169
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Metaways Infosystems GmbH, 2014
6
 * @copyright Aimeos (aimeos.org), 2015-2016
7
 * @package MW
8
 * @subpackage Cache
9
 */
10
11
12
namespace Aimeos\MW\Cache;
13
14
15
/**
16
 * MySQL database cache class.
17
 *
18
 * @package MW
19
 * @subpackage Cache
20
 */
21
class Mysql
22
	extends \Aimeos\MW\Cache\DB
0 ignored issues
show
Coding Style introduced by
Expected 0 spaces between "DB" and comma; 1 found
Loading history...
23
	implements \Aimeos\MW\Cache\Iface
24
{
25
	private $sql;
26
	private $dbm;
27
	private $dbname;
28
	private $siteid;
29
30
31
	/**
32
	 * Initializes the object instance.
33
	 *
34
	 * The config['search] array must contain these key/array pairs suitable for \Aimeos\MW\Criteria\Attribute\Standard:
35
	 *	[cache.id] => Array containing the codes/types/labels for the unique ID
36
	 *	[cache.siteid] => Array containing the codes/types/labels for the site ID
37
	 *	[cache.value] => Array containing the codes/types/labels for the cached value
38
	 *	[cache.expire] => Array containing the codes/types/labels for the expiration date
39
	 *	[cache.tag.name] => Array containing the codes/types/labels for the tag name
40
	 *
41
	 * The config['sql] array must contain these statement:
42
	 *	[delete] =>
43
	 *		DELETE FROM cachetable WHERE siteid = ? AND :cond
44
	 *	[deletebytag] =>
45
	 *		DELETE FROM cachetable WHERE siteid = ? AND id IN (
46
	 *			SELECT tid FROM cachetagtable WHERE tsiteid = ? AND :cond
47
	 *		)
48
	 *	[get] =>
49
	 *		SELECT id, value, expire FROM cachetable WHERE siteid = ? AND :cond
50
	 *	[getbytag] =>
51
	 *		SELECT id, value, expire FROM cachetable
52
	 *		JOIN cachetagtable ON tid = id
53
	 *		WHERE siteid = ? AND tsiteid = ? AND :cond
54
	 *	[set] =>
55
	 *		INSERT INTO cachetable ( id, siteid, expire, value ) VALUES ( ?, ?, ?, ? ) ON DUPLICATE KEY UPDATE
56
	 *	[settag] =>
57
	 *		INSERT INTO cachetagtable ( tid, tsiteid, tname ) VALUES :tuples ON DUPLICATE KEY UPDATE
58
	 *
59
	 * For using a different database connection, the name of the database connection
60
	 * can be also given in the "config" parameter. In this case, use e.g.
61
	 *  config['dbname'] = 'db-cache'
62
	 *
63
	 * If a site ID is given, the cache is partitioned for different
64
	 * sites. This also includes access control so cached values can be only
65
	 * retrieved from the same site. Specify a site ID with
66
	 *  config['siteid'] = 123
67
	 *
68
	 * @param array $config Associative list with SQL statements, search attribute definitions and database name
69
	 * @param \Aimeos\MW\DB\Manager\Iface $dbm Database manager
70
	 */
71
	public function __construct( array $config, \Aimeos\MW\DB\Manager\Iface $dbm )
72
	{
73
		parent::__construct( $config, $dbm );
74
75
		$this->dbname = ( isset( $config['dbname'] ) ? $config['dbname'] : 'db' );
76
		$this->siteid = ( isset( $config['siteid'] ) ? $config['siteid'] : null );
77
		$this->sql = $config['sql'];
78
		$this->dbm = $dbm;
79
	}
80
81
82
	/**
83
	 * Adds or overwrites the given key/value pairs in the cache, which is much
84
	 * more efficient than setting them one by one using the set() method.
85
	 *
86
	 * @param \Traversable $pairs Associative list of key/value pairs. Both must be
87
	 * 	a string
88
	 * @param array|int|string|null $expires Associative list of keys and datetime
89
	 *  string or integer TTL pairs.
90
	 * @param array $tags Associative list of key/tag or key/tags pairs that
91
	 *  should be associated to the values identified by their key. The value
92
	 *  associated to the key can either be a tag string or an array of tag strings
93
	 * @return null
94
	 * @throws \Aimeos\MW\Cache\Exception If the cache server doesn't respond
95
	 */
96
	public function setMultiple( $pairs, $expires = null, array $tags = array() )
97
	{
98
		$type = ( count( $pairs ) > 1 ? \Aimeos\MW\DB\Connection\Base::TYPE_PREP : \Aimeos\MW\DB\Connection\Base::TYPE_SIMPLE );
99
		$conn = $this->dbm->acquire( $this->dbname );
100
101
		try
102
		{
103
			$conn->begin();
104
			$stmt = $conn->create( $this->sql['set'], $type );
105
106
			foreach( $pairs as $key => $value )
107
			{
108
				$date = ( is_array( $expires ) && isset( $expires[$key] ) ? $expires[$key] : $expires );
109
110
				if( is_int( $date ) ) {
111
					$date = date( 'Y-m-d H:i:s', time() + $date );
112
				}
113
114
				$stmt->bind( 1, $key );
115
				$stmt->bind( 2, $this->siteid, \Aimeos\MW\DB\Statement\Base::PARAM_INT );
116
				$stmt->bind( 3, $date );
117
				$stmt->bind( 4, $value );
118
				$stmt->execute()->finish();
119
120
				if( isset( $tags[$key] ) )
121
				{
122
					$parts = array();
123
					$stmtTagPart = $conn->create( '( ?, ?, ? )' );
124
125
					foreach( (array) $tags[$key] as $name )
126
					{
127
						$stmtTagPart->bind( 1, $key );
128
						$stmtTagPart->bind( 2, $this->siteid, \Aimeos\MW\DB\Statement\Base::PARAM_INT );
129
						$stmtTagPart->bind( 3, $name );
130
131
						$parts[] = (string) $stmtTagPart;
132
					}
133
134
					if( !empty ( $parts ) )
135
					{
136
						$stmtTag = $conn->create( str_replace( ':tuples', join( ',', $parts ), $this->sql['settag'] ) );
137
						$stmtTag->execute()->finish();
138
					}
139
				}
140
			}
141
142
			$conn->commit();
143
			$this->dbm->release( $conn, $this->dbname );
144
		}
145
		catch( \Exception $e )
146
		{
147
			$conn->rollback();
148
			$this->dbm->release( $conn, $this->dbname );
149
			throw $e;
150
		}
151
	}
152
}
153