Passed
Push — master ( c46287...9f1c00 )
by Aimeos
05:19
created

Base::prefix()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
/**
4
 * @license LGPLv3, https://opensource.org/licenses/LGPL-3.0
5
 * @copyright Metaways Infosystems GmbH, 2011
6
 * @copyright Aimeos (aimeos.org), 2015-2024
7
 * @package MShop
8
 * @subpackage Common
9
 */
10
11
12
namespace Aimeos\MShop\Common\Manager\Address;
13
14
15
/**
16
 * Common abstract address manager implementation.
17
 *
18
 * @package MShop
19
 * @subpackage Common
20
 */
21
abstract class Base
22
	extends \Aimeos\MShop\Common\Manager\Base
23
{
24
	private string $prefix;
25
	private array $searchConfig;
26
27
28
	/**
29
	 * Initializes a new common address manager object using the given context object.
30
	 *
31
	 * @param \Aimeos\MShop\ContextIface $context Context object with required objects
32
	 *
33
	 * @throws \Aimeos\MShop\Exception if no configuration is available
34
	 */
35
	public function __construct( \Aimeos\MShop\ContextIface $context )
36
	{
37
		parent::__construct( $context );
38
39
		$this->searchConfig = $this->getSearchConfig();
40
41
		if( ( $entry = reset( $this->searchConfig ) ) === false )
42
		{
43
			$msg = $this->context()->translate( 'mshop', 'Search configuration not available' );
44
			throw new \Aimeos\MShop\Exception( $msg );
45
		}
46
47
		if( ( $pos = strrpos( $entry['code'], '.' ) ) === false )
48
		{
49
			$msg = $this->context()->translate( 'mshop', 'Search configuration for "%1$s" not available' );
50
			throw new \Aimeos\MShop\Exception( sprintf( $msg, $entry['code'] ) );
51
		}
52
53
		if( empty( $this->prefix = substr( $entry['code'], 0, $pos + 1 ) ) )
54
		{
55
			$msg = $this->context()->translate( 'mshop', 'Search configuration for "%1$s" not available' );
56
			throw new \Aimeos\MShop\Exception( sprintf( $msg, $entry['code'] ) );
57
		}
58
	}
59
60
61
	/**
62
	 * Creates a new empty item instance
63
	 *
64
	 * @param array $values Values the item should be initialized with
65
	 * @return \Aimeos\MShop\Common\Item\Address\Iface New address item object
66
	 */
67
	public function create( array $values = [] ) : \Aimeos\MShop\Common\Item\Iface
68
	{
69
		$values[$this->prefix . 'siteid'] = $values[$this->prefix . 'siteid'] ?? $this->context()->locale()->getSiteId();
70
		return $this->createItemBase( $values );
71
	}
72
73
74
	/**
75
	 * Removes multiple items.
76
	 *
77
	 * @param \Aimeos\MShop\Common\Item\Iface[]|string[] $itemIds List of item objects or IDs of the items
78
	 * @return \Aimeos\MShop\Common\Manager\Address\Iface Manager object for chaining method calls
79
	 */
80
	public function delete( $itemIds ) : \Aimeos\MShop\Common\Manager\Iface
81
	{
82
		return $this->deleteItemsBase( $itemIds, $this->getConfigPath() . 'delete' );
83
	}
84
85
86
	/**
87
	 * Returns the common address item object specificed by its ID.
88
	 *
89
	 * @param string $id Unique common address ID referencing an existing address
90
	 * @param string[] $ref List of domains to fetch list items and referenced items for
91
	 * @param bool|null $default Add default criteria or NULL for relaxed default criteria
92
	 * @return \Aimeos\MShop\Common\Item\Address\Iface Returns the address item of the given id
93
	 * @throws \Aimeos\MShop\Exception If address search configuration isn't available
94
	 */
95
	public function get( string $id, array $ref = [], ?bool $default = false ) : \Aimeos\MShop\Common\Item\Iface
96
	{
97
		if( ( $conf = reset( $this->searchConfig ) ) === false )
98
		{
99
			$msg = $this->context()->translate( 'mshop', 'Address search configuration not available' );
100
			throw new \Aimeos\MShop\Exception( $msg );
101
		}
102
103
		return $this->getItemBase( $conf['code'], $id, $ref, $default );
104
	}
105
106
107
	/**
108
	 * Saves a common address item object.
109
	 *
110
	 * @param \Aimeos\MShop\Common\Item\Address\Iface $item common address item object
111
	 * @param bool $fetch True if the new ID should be returned in the item
112
	 * @return \Aimeos\MShop\Common\Item\Address\Iface $item Updated item including the generated ID
113
	 */
114
	protected function saveItem( \Aimeos\MShop\Common\Item\Address\Iface $item, bool $fetch = true ) : \Aimeos\MShop\Common\Item\Address\Iface
115
	{
116
		if( !$item->isModified() ) {
117
			return $item;
118
		}
119
120
		$context = $this->context();
121
		$conn = $context->db( $this->getResourceName() );
122
123
		$id = $item->getId();
124
		$path = $this->getConfigPath();
125
		$columns = $this->object()->getSaveAttributes();
126
127
		if( $id === null ) {
128
			$sql = $this->addSqlColumns( array_keys( $columns ), $this->getSqlConfig( $path .= 'insert' ) );
0 ignored issues
show
Bug introduced by
It seems like $this->getSqlConfig(AssignConcatNode) can also be of type array; however, parameter $sql of Aimeos\MShop\Common\Manager\Base::addSqlColumns() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

128
			$sql = $this->addSqlColumns( array_keys( $columns ), /** @scrutinizer ignore-type */ $this->getSqlConfig( $path .= 'insert' ) );
Loading history...
129
		} else {
130
			$sql = $this->addSqlColumns( array_keys( $columns ), $this->getSqlConfig( $path .= 'update' ), false );
131
		}
132
133
		$idx = 1;
134
		$stmt = $this->getCachedStatement( $conn, $path, $sql );
135
136
		foreach( $columns as $name => $entry ) {
137
			$stmt->bind( $idx++, $item->get( $name ), \Aimeos\Base\Criteria\SQL::type( $entry->getType() ) );
138
		}
139
140
		$stmt->bind( $idx++, $item->getParentId(), \Aimeos\Base\DB\Statement\Base::PARAM_INT );
141
		$stmt->bind( $idx++, $item->getType() );
142
		$stmt->bind( $idx++, $item->getCompany() );
143
		$stmt->bind( $idx++, $item->getVatId() );
144
		$stmt->bind( $idx++, $item->getSalutation() );
145
		$stmt->bind( $idx++, $item->getTitle() );
146
		$stmt->bind( $idx++, $item->getFirstname() );
147
		$stmt->bind( $idx++, $item->getLastname() );
148
		$stmt->bind( $idx++, $item->getAddress1() );
149
		$stmt->bind( $idx++, $item->getAddress2() );
150
		$stmt->bind( $idx++, $item->getAddress3() );
151
		$stmt->bind( $idx++, $item->getPostal() );
152
		$stmt->bind( $idx++, $item->getCity() );
153
		$stmt->bind( $idx++, $item->getState() );
154
		$stmt->bind( $idx++, $item->getCountryId() );
155
		$stmt->bind( $idx++, $item->getLanguageId() );
156
		$stmt->bind( $idx++, $item->getTelephone() );
157
		$stmt->bind( $idx++, $item->getMobile() );
158
		$stmt->bind( $idx++, $item->getEmail() );
159
		$stmt->bind( $idx++, $item->getTelefax() );
160
		$stmt->bind( $idx++, $item->getWebsite() );
161
		$stmt->bind( $idx++, $item->getLongitude(), \Aimeos\Base\DB\Statement\Base::PARAM_FLOAT );
162
		$stmt->bind( $idx++, $item->getLatitude(), \Aimeos\Base\DB\Statement\Base::PARAM_FLOAT );
163
		$stmt->bind( $idx++, $item->getPosition(), \Aimeos\Base\DB\Statement\Base::PARAM_INT );
164
		$stmt->bind( $idx++, $item->getBirthday() );
165
		$stmt->bind( $idx++, $context->datetime() ); //mtime
166
		$stmt->bind( $idx++, $context->editor() );
167
168
		if( $id !== null ) {
169
			$stmt->bind( $idx++, $context->locale()->getSiteId() . '%' );
170
			$stmt->bind( $idx++, $id, \Aimeos\Base\DB\Statement\Base::PARAM_INT );
171
		} else {
172
			$stmt->bind( $idx++, $this->siteId( $item->getSiteId(), \Aimeos\MShop\Locale\Manager\Base::SITE_SUBTREE ) );
173
			$stmt->bind( $idx++, $context->datetime() ); // ctime
174
		}
175
176
		$stmt->execute()->finish();
177
178
		if( $id === null && $fetch === true ) {
179
			$id = $this->newId( $conn, $this->getConfigPath() . 'newid' );
180
		}
181
182
		return $item->setId( $id );
183
	}
184
185
186
	/**
187
	 * Returns the item objects matched by the given search criteria.
188
	 *
189
	 * @param \Aimeos\Base\Criteria\Iface $search Search criteria object
190
	 * @param string[] $ref List of domains to fetch list items and referenced items for
191
	 * @param int|null &$total Number of items that are available in total
192
	 * @return \Aimeos\Map List of items implementing \Aimeos\MShop\Common\Item\Address\Iface with ids as keys
193
	 */
194
	public function search( \Aimeos\Base\Criteria\Iface $search, array $ref = [], int &$total = null ) : \Aimeos\Map
195
	{
196
		$conn = $this->context()->db( $this->getResourceName() );
197
		$items = [];
198
199
		$required = [trim( $this->prefix, '.' )];
200
		$level = \Aimeos\MShop\Locale\Manager\Base::SITE_ALL;
201
		$cfgPathSearch = $this->getConfigPath() . 'search';
202
		$cfgPathCount = $this->getConfigPath() . 'count';
203
204
		$results = $this->searchItemsBase( $conn, $search, $cfgPathSearch, $cfgPathCount, $required, $total, $level );
205
206
		while( $row = $results->fetch() )
207
		{
208
			if( $item = $this->applyFilter( $this->createItemBase( $row ) ) ) {
209
				$items[$row[$this->prefix . 'id']] = $item;
210
			}
211
		}
212
213
		return map( $items );
214
	}
215
216
217
	/**
218
	 * Returns a new manager for address extensions.
219
	 *
220
	 * @param string $manager Name of the sub manager type in lower case
221
	 * @param string|null $name Name of the implementation, will be from configuration (or Default) if null
222
	 * @return \Aimeos\MShop\Common\Manager\Iface Manager for different extensions, e.g type, etc.
223
	 */
224
	public function getSubManager( string $manager, string $name = null ) : \Aimeos\MShop\Common\Manager\Iface
225
	{
226
		return $this->getSubManagerBase( 'common', 'address/' . $manager, $name );
227
	}
228
229
230
	/**
231
	 * Returns the config path for retrieving the configuration values.
232
	 *
233
	 * @return string Configuration path
234
	 */
235
	abstract protected function getConfigPath() : string;
236
237
238
	/**
239
	 * Returns the search configuration for searching items.
240
	 *
241
	 * @return array Associative list of search keys and search definitions
242
	 */
243
	abstract protected function getSearchConfig() : array;
244
245
246
	/**
247
	 * Returns the search key and item prefix
248
	 *
249
	 * @return string Search key / item prefix
250
	 */
251
	protected function prefix() : string
252
	{
253
		return $this->prefix;
254
	}
255
256
257
	/**
258
	 * Creates a new address item
259
	 *
260
	 * @param array $values List of attributes for address item
261
	 * @return \Aimeos\MShop\Common\Item\Address\Iface New address item
262
	 */
263
	protected function createItemBase( array $values = [] ) : \Aimeos\MShop\Common\Item\Address\Iface
264
	{
265
		return new \Aimeos\MShop\Common\Item\Address\Standard( $this->prefix, $values );
266
	}
267
}
268