Standard::data()   B
last analyzed

Complexity

Conditions 11
Paths 8

Size

Total Lines 59
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 11
eloc 25
nc 8
nop 3
dl 0
loc 59
rs 7.3166
c 0
b 0
f 0

How to fix   Long Method    Complexity   

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, 2013
6
 * @copyright Aimeos (aimeos.org), 2015-2025
7
 * @package Client
8
 * @subpackage Html
9
 */
10
11
12
namespace Aimeos\Client\Html\Checkout\Standard\Delivery;
13
14
15
// Strings for translation
16
sprintf( 'delivery' );
17
18
19
/**
20
 * Default implementation of checkout delivery HTML client.
21
 *
22
 * @package Client
23
 * @subpackage Html
24
 */
25
class Standard
26
	extends \Aimeos\Client\Html\Common\Client\Factory\Base
27
	implements \Aimeos\Client\Html\Common\Client\Factory\Iface
28
{
29
	/**
30
	 * Returns the HTML code for insertion into the body.
31
	 *
32
	 * @param string $uid Unique identifier for the output if the content is placed more than once on the same page
33
	 * @return string HTML code
34
	 */
35
	public function body( string $uid = '' ) : string
36
	{
37
		$view = $this->view();
38
		$step = $view->get( 'standardStepActive' );
39
		$onepage = $view->config( 'client/html/checkout/standard/onepage', [] );
40
41
		if( $step != 'delivery' && !( in_array( 'delivery', $onepage ) && in_array( $step, $onepage ) ) ) {
42
			return '';
43
		}
44
45
		return parent::body( $uid );
46
	}
47
48
49
	/**
50
	 * Processes the input, e.g. store given values.
51
	 *
52
	 * A view must be available and this method doesn't generate any output
53
	 * besides setting view variables.
54
	 */
55
	public function init()
56
	{
57
		$view = $this->view();
58
		$context = $this->context();
59
60
		try
61
		{
62
			$basketCtrl = \Aimeos\Controller\Frontend::create( $context, 'basket' );
63
			$servCtrl = \Aimeos\Controller\Frontend::create( $context, 'service' )->uses( ['media', 'price', 'text'] );
64
65
			// only start if there's something to do
66
			if( ( $serviceIds = $view->param( 'c_deliveryoption', null ) ) !== null )
67
			{
68
				$basketCtrl->deleteService( 'delivery' );
69
70
				foreach( (array) $serviceIds as $idx => $id )
71
				{
72
					try
73
					{
74
						$basketCtrl->addService( $servCtrl->get( $id ), $view->param( 'c_delivery/' . $id, [] ), $idx );
75
					}
76
					catch( \Aimeos\Controller\Frontend\Basket\Exception $e )
77
					{
78
						$view->deliveryError = $e->getErrors();
79
						$view->errors = array_merge( $view->get( 'errors', [] ), $e->getErrors() );
80
81
						throw $e;
82
					}
83
				}
84
			}
85
86
87
			parent::init();
88
89
90
			if( !isset( $view->standardStepActive ) && !$this->call( 'isAvailable', $basketCtrl->get() ) ) {
91
				$view->standardStepActive = 'delivery';
92
			}
93
		}
94
		catch( \Exception $e )
95
		{
96
			$view->standardStepActive = 'delivery';
97
			throw $e;
98
		}
99
	}
100
101
102
	/**
103
	 * Sets the necessary parameter values in the view.
104
	 *
105
	 * @param \Aimeos\Base\View\Iface $view The view object which generates the HTML output
106
	 * @param array &$tags Result array for the list of tags that are associated to the output
107
	 * @param string|null &$expire Result variable for the expiration date of the output (null for no expiry)
108
	 * @return \Aimeos\Base\View\Iface Modified view object
109
	 */
110
	public function data( \Aimeos\Base\View\Iface $view, array &$tags = [], ?string &$expire = null ) : \Aimeos\Base\View\Iface
111
	{
112
		$context = $this->context();
113
		$domains = ['media', 'price', 'text'];
114
115
		/** client/html/checkout/standard/delivery/domains
116
		 * List of domain names whose items should be available in the checkout payment templates
117
		 *
118
		 * The templates rendering checkout delivery related data usually add the
119
		 * images, prices and texts associated to each item. If you want to display
120
		 * additional content like the attributes, you can configure your own list
121
		 * of domains (attribute, media, price, text, etc. are domains) whose items
122
		 * are fetched from the storage.
123
		 *
124
		 * @param array List of domain names
125
		 * @since 2019.04
126
		 * @see client/html/checkout/standard/payment/domains
127
		 */
128
		$domains = $context->config()->get( 'client/html/checkout/standard/delivery/domains', $domains );
129
130
		$basketCntl = \Aimeos\Controller\Frontend::create( $context, 'basket' );
131
		$serviceCntl = \Aimeos\Controller\Frontend::create( $context, 'service' );
132
133
		$services = [];
134
		$basket = $basketCntl->get();
135
		$providers = $serviceCntl->uses( $domains )->type( 'delivery' )->getProviders();
136
		$orderServices = map( $basket->getService( 'delivery' ) )->col( null, 'order.service.serviceid' );
137
138
		foreach( $providers as $id => $provider )
139
		{
140
			if( $provider->isAvailable( $basket ) === true )
141
			{
142
				$attr = $provider->getConfigFE( $basket );
143
144
				if( $oservice = $orderServices->get( $id ) )
145
				{
146
					foreach( $attr as $key => $item )
147
					{
148
						$value = is_array( $item->getDefault() ) ? key( $item->getDefault() ) : $item->getDefault();
149
						$value = $oservice->getAttribute( $key, 'delivery' ) ?: $value;
150
						$item->value = $oservice->getAttribute( $key . '/hidden', 'delivery' ) ?: $value;
151
					}
152
				}
153
				else
154
				{
155
					foreach( $attr as $key => $item ) {
156
						$item->value = is_array( $item->getDefault() ) ? key( $item->getDefault() ) : $item->getDefault();
157
					}
158
				}
159
160
				$services[$id] = $provider->getServiceItem()->set( 'attributes', $attr )
161
					->set( 'price', $provider->calcPrice( $basket ) );
162
			}
163
		}
164
165
		$view->deliveryServices = $services;
166
		$view->deliveryOption = $view->param( 'c_deliveryoption', $orderServices->firstKey() ?: key( $services ) );
167
168
		return parent::data( $view, $tags, $expire );
169
	}
170
171
172
	/**
173
	 * Tests if an item is available and the step can be skipped
174
	 *
175
	 * @param \Aimeos\MShop\Order\Item\Iface $basket Basket object
176
	 * @return bool TRUE if step can be skipped, FALSE if not
177
	 */
178
	protected function isAvailable( \Aimeos\MShop\Order\Item\Iface $basket ) : bool
179
	{
180
		return !empty( $basket->getService( 'delivery' ) );
181
	}
182
183
184
	/** client/html/checkout/standard/delivery/template-body
185
	 * Relative path to the HTML body template of the checkout standard delivery client.
186
	 *
187
	 * The template file contains the HTML code and processing instructions
188
	 * to generate the result shown in the body of the frontend. The
189
	 * configuration string is the path to the template file relative
190
	 * to the templates directory (usually in templates/client/html).
191
	 *
192
	 * You can overwrite the template file configuration in extensions and
193
	 * provide alternative templates. These alternative templates should be
194
	 * named like the default one but suffixed by
195
	 * an unique name. You may use the name of your project for this. If
196
	 * you've implemented an alternative client class as well, it
197
	 * should be suffixed by the name of the new class.
198
	 *
199
	 * @param string Relative path to the template creating code for the HTML page body
200
	 * @since 2014.03
201
	 * @see client/html/checkout/standard/delivery/template-header
202
	 */
203
}
204