Completed
Push — master ( 815faa...197710 )
by Aimeos
07:55
created

Mysql::setList()   C

Complexity

Conditions 8
Paths 156

Size

Total Lines 52
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 30
nc 156
nop 3
dl 0
loc 52
rs 6.0761
c 0
b 0
f 0

How to fix   Long Method   

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 iterable $pairs Associative list of key/value pairs. Both must be
87
	 * 	a string
88
	 * @param int|string|array $expires Associative list of keys and datetime
0 ignored issues
show
Documentation introduced by
Should the type for parameter $expires not be integer|string|array|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
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 = ( isset( $expires[$key] ) ? $expires[$key] : null );
109
110
				$stmt->bind( 1, $key );
111
				$stmt->bind( 2, $this->siteid, \Aimeos\MW\DB\Statement\Base::PARAM_INT );
112
				$stmt->bind( 3, $date );
113
				$stmt->bind( 4, $value );
114
				$stmt->execute()->finish();
115
116
				if( isset( $tags[$key] ) )
117
				{
118
					$parts = array();
119
					$stmtTagPart = $conn->create( '( ?, ?, ? )' );
120
121
					foreach( (array) $tags[$key] as $name )
122
					{
123
						$stmtTagPart->bind( 1, $key );
124
						$stmtTagPart->bind( 2, $this->siteid, \Aimeos\MW\DB\Statement\Base::PARAM_INT );
125
						$stmtTagPart->bind( 3, $name );
126
127
						$parts[] = (string) $stmtTagPart;
128
					}
129
130
					if( !empty ( $parts ) )
131
					{
132
						$stmtTag = $conn->create( str_replace( ':tuples', join( ',', $parts ), $this->sql['settag'] ) );
133
						$stmtTag->execute()->finish();
134
					}
135
				}
136
			}
137
138
			$conn->commit();
139
			$this->dbm->release( $conn, $this->dbname );
140
		}
141
		catch( \Exception $e )
142
		{
143
			$conn->rollback();
144
			$this->dbm->release( $conn, $this->dbname );
145
			throw $e;
146
		}
147
	}
148
}
149