Passed
Push — master ( 3311dc...3c99d6 )
by Aimeos
05:26
created

src/MShop/Service/Provider/Decorator/Postal.php (2 issues)

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-2018
7
 * @package MShop
8
 * @subpackage Service
9
 */
10
11
12
namespace Aimeos\MShop\Service\Provider\Decorator;
13
14
15
/**
16
 * Postal-limiting decorator for service providers
17
 *
18
 * This decorator interacts with the ServiceUpdate and Autofill basket plugins!
19
 * If the delivery/payment option isn't available any more, the ServiceUpdate
20
 * plugin will remove it from the basket and the Autofill plugin will add one
21
 * of the available options again.
22
 *
23
 * @package MShop
24
 * @subpackage Service
25
 */
26
class Postal
27
	extends \Aimeos\MShop\Service\Provider\Decorator\Base
28
	implements \Aimeos\MShop\Service\Provider\Decorator\Iface
29
{
30
	private $beConfig = array(
31
		'postal.billing-include' => array(
32
			'code' => 'postal.billing-include',
33
			'internalcode' => 'postal.billing-include',
34
			'label' => 'List of postal codes allowed for the billing address',
35
			'type' => 'string',
36
			'internaltype' => 'string',
37
			'default' => '',
38
			'required' => false,
39
		),
40
		'postal.billing-exclude' => array(
41
			'code' => 'postal.billing-exclude',
42
			'internalcode' => 'postal.billing-exclude',
43
			'label' => 'List of postal codes not allowed for the billing address',
44
			'type' => 'string',
45
			'internaltype' => 'string',
46
			'default' => '',
47
			'required' => false,
48
		),
49
		'postal.delivery-include' => array(
50
			'code' => 'postal.delivery-include',
51
			'internalcode' => 'postal.delivery-include',
52
			'label' => 'List of postal codes allowed for the delivery address',
53
			'type' => 'string',
54
			'internaltype' => 'string',
55
			'default' => '',
56
			'required' => false,
57
		),
58
		'postal.delivery-exclude' => array(
59
			'code' => 'postal.delivery-exclude',
60
			'internalcode' => 'postal.delivery-exclude',
61
			'label' => 'List of postal codes not allowed for the delivery address',
62
			'type' => 'string',
63
			'internaltype' => 'string',
64
			'default' => '',
65
			'required' => false,
66
		),
67
	);
68
69
70
	/**
71
	 * Checks the backend configuration attributes for validity.
72
	 *
73
	 * @param array $attributes Attributes added by the shop owner in the administraton interface
74
	 * @return array An array with the attribute keys as key and an error message as values for all attributes that are
75
	 * 	known by the provider but aren't valid
76
	 */
77
	public function checkConfigBE( array $attributes )
0 ignored issues
show
This method is not in camel caps format.

This check looks for method names that are not written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection seeker becomes databaseConnectionSeeker.

Loading history...
78
	{
79
		$error = $this->getProvider()->checkConfigBE( $attributes );
80
		$error += $this->checkConfig( $this->beConfig, $attributes );
81
82
		return $error;
83
	}
84
85
86
	/**
87
	 * Returns the configuration attribute definitions of the provider to generate a list of available fields and
88
	 * rules for the value of each field in the administration interface.
89
	 *
90
	 * @return array List of attribute definitions implementing \Aimeos\MW\Common\Critera\Attribute\Iface
91
	 */
92
	public function getConfigBE()
0 ignored issues
show
This method is not in camel caps format.

This check looks for method names that are not written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection seeker becomes databaseConnectionSeeker.

Loading history...
93
	{
94
		return array_merge( $this->getProvider()->getConfigBE(), $this->getConfigItems( $this->beConfig ) );
95
	}
96
97
98
	/**
99
	 * Checks if the postal code is allowed for the service provider.
100
	 *
101
	 * @param \Aimeos\MShop\Order\Item\Base\Iface $basket Basket object
102
	 * @return boolean True if payment provider can be used, false if not
103
	 */
104
	public function isAvailable( \Aimeos\MShop\Order\Item\Base\Iface $basket )
105
	{
106
		$paymentType = \Aimeos\MShop\Order\Item\Base\Address\Base::TYPE_PAYMENT;
107
		$deliveryType = \Aimeos\MShop\Order\Item\Base\Address\Base::TYPE_DELIVERY;
108
109
110
		if( ( $addresses = $basket->getAddress( $deliveryType ) ) !== [] )
111
		{
112
			foreach( $addresses as $address )
113
			{
114
				$code = $address->getPostal();
115
116
				if( $this->checkPostalCode( $code, 'postal.delivery-include' ) === false
117
					|| $this->checkPostalCode( $code, 'postal.delivery-exclude' ) === true
118
				) {
119
					return false;
120
				}
121
			}
122
		}
123
		elseif( ( $addresses = $basket->getAddress( $paymentType ) ) !== [] ) // use billing address if no delivery address is available
124
		{
125
			foreach( $addresses as $address )
126
			{
127
				$code = $address->getPostal();
128
129
				if( $this->checkPostalCode( $code, 'postal.delivery-include' ) === false
130
					|| $this->checkPostalCode( $code, 'postal.delivery-exclude' ) === true
131
				) {
132
					return false;
133
				}
134
135
				if( $this->checkPostalCode( $code, 'postal.billing-include' ) === false
136
					|| $this->checkPostalCode( $code, 'postal.billing-exclude' ) === true
137
				) {
138
					return false;
139
				}
140
			}
141
		}
142
143
		return $this->getProvider()->isAvailable( $basket );
144
	}
145
146
147
	/**
148
	 * Checks if the postal code is in the list of codes specified by the given key
149
	 *
150
	 * @param string $code Postal code
151
	 * @param string $key Configuration key referring to the postal code configuration
152
	 */
153
	protected function checkPostalCode( $code, $key )
154
	{
155
		if( ( $str = $this->getConfigValue( $key ) ) == null ) {
156
			return null;
157
		}
158
159
		return in_array( $code, explode( ',', str_replace( ' ', '', $str ) ) );
160
	}
161
}
162