Passed
Push — master ( 23d0a5...26f6d6 )
by Aimeos
05:18
created

src/MShop/Common/Manager/Property/Base.php (1 issue)

Labels
Severity
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2014-2018
6
 * @package MShop
7
 * @subpackage Common
8
 */
9
10
11
namespace Aimeos\MShop\Common\Manager\Property;
12
13
14
/**
15
 * Abstract property manager implementation.
16
 *
17
 * @package MShop
18
 * @subpackage Common
19
 */
20
abstract class Base
21
	extends \Aimeos\MShop\Common\Manager\Base
22
{
23
	private $languageId;
24
	private $searchConfig;
25
	private $prefix;
26
27
28
	/**
29
	 * Initializes the object.
30
	 *
31
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object
32
	 */
33
	public function __construct( \Aimeos\MShop\Context\Item\Iface $context )
34
	{
35
		parent::__construct( $context );
36
37
		$this->languageId = $context->getLocale()->getLanguageId();
38
		$this->searchConfig = $this->getSearchConfig();
39
40
		if( ( $entry = reset( $this->searchConfig ) ) === false ) {
41
			throw new \Aimeos\MShop\Exception( sprintf( 'Search configuration not available' ) );
42
		}
43
44
		if( ( $pos = strrpos( $entry['code'], '.' ) ) === false ) {
45
			throw new \Aimeos\MShop\Exception( sprintf( 'Search configuration for "%1$s" not available', $entry['code'] ) );
46
		}
47
48
		if( ( $this->prefix = substr( $entry['code'], 0, $pos + 1 ) ) === false ) {
49
			throw new \Aimeos\MShop\Exception( sprintf( 'Search configuration for "%1$s" not available', $entry['code'] ) );
50
		}
51
	}
52
53
54
	/**
55
	 * Creates a new empty item instance
56
	 *
57
	 * @param array $values Values the item should be initialized with
58
	 * @return \Aimeos\MShop\Common\Item\Property\Iface New property item object
59
	 */
60
	public function createItem( array $values = [] )
61
	{
62
		$values[$this->prefix . 'siteid'] = $this->getContext()->getLocale()->getSiteId();
63
		return $this->createItemBase( $values );
64
	}
65
66
67
	/**
68
	 * Creates a search object and optionally sets base criteria.
69
	 *
70
	 * @param boolean $default Add default criteria
71
	 * @return \Aimeos\MW\Criteria\Iface Criteria object
72
	 */
73
	public function createSearch( $default = false )
74
	{
75
		$object = parent::createSearch();
76
77
		if( $default === true )
78
		{
79
			$langid = $this->getContext()->getLocale()->getLanguageId();
80
81
			$expr = array(
82
				$object->compare( '==', $this->prefix . 'languageid', null ),
83
				$object->compare( '==', $this->prefix . 'languageid', $langid ),
84
			);
85
86
			$object->setConditions( $object->combine( '||', $expr ) );
87
		}
88
89
		return $object;
90
	}
91
92
93
	/**
94
	 * Inserts the new property items for product item
95
	 *
96
	 * @param \Aimeos\MShop\Common\Item\Property\Iface $item Property item which should be saved
97
	 * @param boolean $fetch True if the new ID should be returned in the item
98
	 * @return \Aimeos\MShop\Common\Item\Iface $item Updated item including the generated ID
99
	 */
100
	public function saveItem( \Aimeos\MShop\Common\Item\Iface $item, $fetch = true )
101
	{
102
		self::checkClass( \Aimeos\MShop\Common\Item\Property\Iface::class, $item );
103
104
		if( !$item->isModified() ) {
105
			return $item;
106
		}
107
108
		$context = $this->getContext();
109
		$dbm = $context->getDatabaseManager();
110
		$dbname = $this->getResourceName();
111
		$conn = $dbm->acquire( $dbname );
112
113
		try
114
		{
115
			$id = $item->getId();
116
			$date = date( 'Y-m-d H:i:s' );
117
			$path = $this->getConfigPath();
118
			$columns = $this->getObject()->getSaveAttributes();
1 ignored issue
show
The method getSaveAttributes() does not exist on Aimeos\MShop\Common\Manager\Iface. Did you maybe mean getSearchAttributes()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

118
			$columns = $this->getObject()->/** @scrutinizer ignore-call */ getSaveAttributes();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
119
120
			if( $id === null ) {
121
				$sql = $this->addSqlColumns( array_keys( $columns ), $this->getSqlConfig( $path .= 'insert' ) );
122
			} else {
123
				$sql = $this->addSqlColumns( array_keys( $columns ), $this->getSqlConfig( $path .= 'update' ), false );
124
			}
125
126
			$idx = 1;
127
			$stmt = $this->getCachedStatement( $conn, $path, $sql );
128
129
			foreach( $columns as $name => $entry ) {
130
				$stmt->bind( $idx++, $item->get( $name ), $entry->getInternalType() );
131
			}
132
133
			$stmt->bind( $idx++, $item->getParentId(), \Aimeos\MW\DB\Statement\Base::PARAM_INT );
134
			$stmt->bind( $idx++, $item->getKey() );
135
			$stmt->bind( $idx++, $item->getType() );
136
			$stmt->bind( $idx++, $item->getLanguageId() );
137
			$stmt->bind( $idx++, $item->getValue() );
138
			$stmt->bind( $idx++, $date ); //mtime
139
			$stmt->bind( $idx++, $context->getEditor() );
140
			$stmt->bind( $idx++, $context->getLocale()->getSiteId(), \Aimeos\MW\DB\Statement\Base::PARAM_INT );
141
142
			if( $id !== null ) {
143
				$stmt->bind( $idx++, $id, \Aimeos\MW\DB\Statement\Base::PARAM_INT );
144
				$item->setId( $id ); //is not modified anymore
145
			} else {
146
				$stmt->bind( $idx++, $date ); //ctime
147
			}
148
149
			$stmt->execute()->finish();
150
151
			if( $fetch === true )
152
			{
153
				if( $id === null ) {
154
					$item->setId( $this->newId( $conn, $this->getConfigPath() . 'newid' ) );
155
				} else {
156
					$item->setId( $id ); // modified false
157
				}
158
			}
159
160
			$dbm->release( $conn, $dbname );
161
		}
162
		catch( \Exception $e )
163
		{
164
			$dbm->release( $conn, $dbname );
165
			throw $e;
166
		}
167
168
		return $item;
169
	}
170
171
172
	/**
173
	 * Removes multiple items specified by ids in the array.
174
	 *
175
	 * @param string[] $ids List of IDs
176
	 * @return \Aimeos\MShop\Common\Manager\Property\Iface Manager object for chaining method calls
177
	 */
178
	public function deleteItems( array $ids )
179
	{
180
		$this->deleteItemsBase( $ids, $this->getConfigPath() . 'delete' );
181
	}
182
183
184
	/**
185
	 * Returns product property item with given Id.
186
	 *
187
	 * @param string $id Id of the product property item
188
	 * @param string[] $ref List of domains to fetch list items and referenced items for
189
	 * @param boolean $default Add default criteria
190
	 * @return \Aimeos\MShop\Common\Item\Property\Iface Returns the product property item of the given id
191
	 * @throws \Aimeos\MShop\Exception If item couldn't be found
192
	 */
193
	public function getItem( $id, array $ref = [], $default = false )
194
	{
195
		return $this->getItemBase( $this->prefix . 'id', $id, $ref, $default );
196
	}
197
198
199
	/**
200
	 * Search for all property items based on the given critera.
201
	 *
202
	 * @param \Aimeos\MW\Criteria\Iface $search Search criteria object
203
	 * @param string[] $ref List of domains to fetch list items and referenced items for
204
	 * @param integer|null &$total Number of items that are available in total
205
	 * @return array List of property items implementing \Aimeos\MShop\Common\Item\Property\Iface
206
	 */
207
	public function searchItems( \Aimeos\MW\Criteria\Iface $search, array $ref = [], &$total = null )
208
	{
209
		$items = [];
210
		$context = $this->getContext();
211
212
		$dbm = $context->getDatabaseManager();
213
		$dbname = $this->getResourceName();
214
		$conn = $dbm->acquire( $dbname );
215
216
		try
217
		{
218
			$level = \Aimeos\MShop\Locale\Manager\Base::SITE_ALL;
219
			$cfgPathSearch = $this->getConfigPath() . 'search';
220
			$cfgPathCount = $this->getConfigPath() . 'count';
221
			$required = array( trim( $this->prefix, '.' ) );
222
223
			$results = $this->searchItemsBase( $conn, $search, $cfgPathSearch, $cfgPathCount, $required, $total, $level );
224
225
			while( ( $row = $results->fetch() ) !== false ) {
226
				$items[(string) $row[$this->prefix . 'id']] = $this->createItemBase( $row );
227
			}
228
229
			$dbm->release( $conn, $dbname );
230
		}
231
		catch( \Exception $e )
232
		{
233
			$dbm->release( $conn, $dbname );
234
			throw $e;
235
		}
236
237
		return $items;
238
	}
239
240
241
	/**
242
	 * Returns a new manager for product extensions
243
	 *
244
	 * @param string $manager Name of the sub manager type in lower case
245
	 * @param string|null $name Name of the implementation, will be from
246
	 * configuration (or Default) if null
247
	 * @return \Aimeos\MShop\Common\Manager\Iface Manager for different extensions, e.g property types, property lists etc.
248
	 */
249
	public function getSubManager( $manager, $name = null )
250
	{
251
		return $this->getSubManagerBase( 'common', 'property/' . $manager, $name );
252
	}
253
254
255
	/**
256
	 * Returns the config path for retrieving the configuration values.
257
	 *
258
	 * @return string Configuration path
259
	 */
260
	abstract protected function getConfigPath();
261
262
263
	/**
264
	 * Returns the search configuration for searching items.
265
	 *
266
	 * @return array Associative list of search keys and search definitions
267
	 */
268
	abstract protected function getSearchConfig();
269
270
271
	/**
272
	 * Creates new property item object.
273
	 *
274
	 * @param array $values Associative list of key/value pairs
275
	 * @return \Aimeos\MShop\Common\Item\Property\Standard New property item object
276
	 */
277
	protected function createItemBase( array $values = [] )
278
	{
279
		$values['.languageid'] = $this->languageId;
280
		return new \Aimeos\MShop\Common\Item\Property\Standard( $this->prefix, $values );
281
	}
282
283
284
	/**
285
	 * Returns the prefix used for the item keys.
286
	 *
287
	 * @return string Item key prefix
288
	 */
289
	protected function getPrefix()
290
	{
291
		return $this->prefix;
292
	}
293
}
294