Completed
Push — master ( 7569d8...f1534a )
by Aimeos
10:06
created

Supplier::__construct()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 52
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 26
nc 3
nop 3
dl 0
loc 52
rs 9.4929
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 Aimeos (aimeos.org), 2017
6
 * @package MShop
7
 * @subpackage Service
8
 */
9
10
11
namespace Aimeos\MShop\Service\Provider\Decorator;
12
13
14
/**
15
 * Supplier address decorator for service providers
16
 *
17
 * @package MShop
18
 * @subpackage Service
19
 */
20
class Supplier
21
	extends \Aimeos\MShop\Service\Provider\Decorator\Base
0 ignored issues
show
Coding Style introduced by
Expected 0 spaces between "Base" and comma; 1 found
Loading history...
22
	implements \Aimeos\MShop\Service\Provider\Decorator\Iface
23
{
24
	private $feConfig = array(
25
		'supplier.code' => array(
26
			'code' => 'supplier.code',
27
			'internalcode'=> 'supplier.code',
28
			'label'=> 'Pick-up address',
29
			'type'=> 'list',
30
			'internaltype'=> 'array',
31
			'default'=> [],
32
			'required'=> true
33
		),
34
	);
35
36
37
	/**
38
	 * Initializes a new service provider object using the given context object.
39
	 *
40
	 * @param \Aimeos\MShop\Service\Provider\Iface $provider Service provider or decorator
41
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object with required objects
42
	 * @param \Aimeos\MShop\Service\Item\Iface $serviceItem Service item with configuration for the provider
43
	 */
44
	public function __construct( \Aimeos\MShop\Service\Provider\Iface $provider,
45
		\Aimeos\MShop\Context\Item\Iface $context, \Aimeos\MShop\Service\Item\Iface $serviceItem )
46
	{
47
		parent::__construct( $provider, $context, $serviceItem );
48
49
		$manager = \Aimeos\MShop\Factory::createManager( $context, 'supplier' );
50
		$addrManager = \Aimeos\MShop\Factory::createManager( $context, 'supplier/address' );
51
52
		$search = $manager->createSearch( true );
53
		$search->setSortations( [$search->sort( '+', 'supplier.label' )] );
54
55
		foreach( $manager->searchItems( $search, ['supplier/address'] ) as $item )
56
		{
57
			$addresses = $item->getAddressItems();
58
59
			if( ( $addr = reset( $addresses ) ) === false ) {
60
				$addr = $addrManager->createItem();
61
			}
62
63
			$this->feConfig['supplier.code']['default'][$item->getCode()] = preg_replace( "/\n+/m", "\n", sprintf(
64
65
			/// Supplier address format with label (%1$s), company (%2$s),
66
			/// address part one (%3$s, e.g street), address part two (%4$s, e.g house number), address part three (%5$s, e.g additional information),
67
			/// postal/zip code (%6$s), city (%7$s), state (%8$s), country ID (%9$s),
68
			/// e-mail (%10$s), phone (%11$s), facsimile/telefax (%12$s), web site (%13$s)
69
				$context->getI18n()->dt( 'mshop', '%1$s
70
%2$s
71
%3$s %4$s
72
%5$s
73
%6$s %7$s
74
%8$s %9$s
75
%10$s
76
%11$s
77
%12$s
78
%13$s
79
' 				),
80
				$item->getLabel(),
81
				$addr->getCompany(),
82
				$addr->getAddress1(),
83
				$addr->getAddress2(),
84
				$addr->getAddress3(),
85
				$addr->getPostal(),
86
				$addr->getCity(),
87
				$addr->getState(),
88
				$addr->getCountryId(),
89
				$addr->getEmail(),
90
				$addr->getTelephone(),
91
				$addr->getTelefax(),
92
				$addr->getWebsite()
93
			) );
94
		}
95
	}
96
97
98
	/**
99
	 * Checks the frontend configuration attributes for validity.
100
	 *
101
	 * @param array $attributes Attributes entered by the customer during the checkout process
102
	 * @return array An array with the attribute keys as key and an error message as values for all attributes that are
103
	 * 	known by the provider but aren't valid resp. null for attributes whose values are OK
104
	 */
105
	public function checkConfigFE( array $attributes )
106
	{
107
		$result = $this->getProvider()->checkConfigFE( $attributes );
108
109
		return array_merge( $result, $this->checkConfig( $this->feConfig, $attributes ) );
110
	}
111
112
113
	/**
114
	 * Returns the configuration attribute definitions of the provider to generate a list of available fields and
115
	 * rules for the value of each field in the frontend.
116
	 *
117
	 * @param \Aimeos\MShop\Order\Item\Base\Iface $basket Basket object
118
	 * @return array List of attribute definitions implementing \Aimeos\MW\Common\Critera\Attribute\Iface
119
	 */
120
	public function getConfigFE( \Aimeos\MShop\Order\Item\Base\Iface $basket )
121
	{
122
		$list = [];
123
		$feconfig = $this->feConfig;
124
125
		try
126
		{
127
			$service = $basket->getService( \Aimeos\MShop\Order\Item\Base\Service\Base::TYPE_DELIVERY );
128
129
			if( ( $value = $service->getAttribute( 'supplier.code', 'delivery' ) ) != ''
130
				&& isset( $feconfig['supplier.code']['default'][$value] )
131
			) {
132
				// move to first position so it's selected
133
				$address = $feconfig['supplier.code']['default'][$value];
134
				unset( $feconfig['supplier.code']['default'][$value] );
135
				$feconfig['supplier.code']['default'] = array( $value => $address ) + $feconfig['supplier.code']['default'];
136
			}
137
		}
138
		catch( \Aimeos\MShop\Order\Exception $e ) {} // If service isn't available
139
140
		foreach( $feconfig as $key => $config ) {
141
			$list[$key] = new \Aimeos\MW\Criteria\Attribute\Standard( $config );
142
		}
143
144
		return $list;
145
	}
146
}
147