Completed
Push — master ( 207311...06e599 )
by Aimeos
09:29
created

Time::getConfigFE()   A

Complexity

Conditions 3
Paths 7

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 11
c 0
b 0
f 0
nc 7
nop 1
dl 0
loc 19
rs 9.4285
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
 * 'Time decorator for service providers
16
 *
17
 * @package MShop
18
 * @subpackage Service
19
 */
20
class Time
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 $beConfig = array(
25
		'time.start' => array(
26
			'code' => 'time.start',
27
			'internalcode'=> 'time.start',
28
			'label'=> 'Earliest delivery time in 24h "HH:MM" format',
29
			'type'=> 'time',
30
			'internaltype'=> 'string',
31
			'default'=> '00:00',
32
			'required'=> false,
33
		),
34
		'time.end' => array(
35
			'code' => 'time.end',
36
			'internalcode'=> 'time.end',
37
			'label'=> 'Latest delivery time in 24h "HH:MM" format',
38
			'type'=> 'time',
39
			'internaltype'=> 'string',
40
			'default'=> '23:59',
41
			'required'=> false,
42
		),
43
		'time.weekdays' => array(
44
			'code' => 'time.weekdays',
45
			'internalcode'=> 'time.weekdays',
46
			'label'=> 'Comma separated week days the start and end time is valid for, i.e. number from 1 (Monday) to 7 (Sunday)',
47
			'type'=> 'string',
48
			'internaltype'=> 'string',
49
			'default'=> '1,2,3,4,5,6,7',
50
			'required'=> false,
51
		),
52
	);
53
54
	private $feConfig = array(
55
		'time.hourminute' => array(
56
			'code' => 'time.hourminute',
57
			'internalcode'=> 'hourminute',
58
			'label'=> 'Delivery time',
59
			'type'=> 'time',
60
			'internaltype'=> 'time',
61
			'default'=> '',
62
			'required'=> true
63
		),
64
	);
65
66
67
	/**
68
	 * Checks the backend configuration attributes for validity.
69
	 *
70
	 * @param array $attributes Attributes added by the shop owner in the administraton interface
71
	 * @return array An array with the attribute keys as key and an error message as values for all attributes that are
72
	 * 	known by the provider but aren't valid
73
	 */
74
	public function checkConfigBE( array $attributes )
75
	{
76
		$error = $this->getProvider()->checkConfigBE( $attributes );
77
		$error += $this->checkConfig( $this->beConfig, $attributes );
78
79
		return $error;
80
	}
81
82
83
	/**
84
	 * Returns the configuration attribute definitions of the provider to generate a list of available fields and
85
	 * rules for the value of each field in the administration interface.
86
	 *
87
	 * @return array List of attribute definitions implementing \Aimeos\MW\Common\Critera\Attribute\Iface
88
	 */
89
	public function getConfigBE()
90
	{
91
		return array_merge( $this->getProvider()->getConfigBE(), $this->getConfigItems( $this->beConfig ) );
92
	}
93
94
95
	/**
96
	 * Returns the configuration attribute definitions of the provider to generate a list of available fields and
97
	 * rules for the value of each field in the frontend.
98
	 *
99
	 * @param \Aimeos\MShop\Order\Item\Base\Iface $basket Basket object
100
	 * @return array List of attribute definitions implementing \Aimeos\MW\Common\Critera\Attribute\Iface
101
	 */
102
	public function getConfigFE( \Aimeos\MShop\Order\Item\Base\Iface $basket )
103
	{
104
		$feconfig = $this->feConfig;
105
106
		try
107
		{
108
			$minute = date( 'i' );
109
			$service = $basket->getService( \Aimeos\MShop\Order\Item\Base\Service\Base::TYPE_DELIVERY );
110
111
			if( ( $value = $service->getAttribute( 'time.hourminute', 'delivery' ) ) == '' ) {
112
				$feconfig['time.hourminute']['default'] = date( 'H:i', time() + ($minute + 15 - ($minute % 15)) * 60 );
113
			} else {
114
				$feconfig['time.hourminute']['default'] = $value;
115
			}
116
		}
117
		catch( \Aimeos\MShop\Order\Exception $e ) { echo $e->getMessage(); } // If service isn't available
118
119
		return array_merge( $this->getProvider()->getConfigFE( $basket ), $this->getConfigItems( $feconfig ) );
120
	}
121
122
123
	/**
124
	 * Checks the frontend configuration attributes for validity.
125
	 *
126
	 * @param array $attributes Attributes entered by the customer during the checkout process
127
	 * @return array An array with the attribute keys as key and an error message as values for all attributes that are
128
	 * 	known by the provider but aren't valid resp. null for attributes whose values are OK
129
	 */
130
	public function checkConfigFE( array $attributes )
131
	{
132
		$result = $this->getProvider()->checkConfigFE( $attributes );
133
		$result = array_merge( $result, $this->checkConfig( $this->feConfig, $attributes ) );
134
135
		if( $result['time.hourminute'] !== null ) {
136
			return $result;
137
		}
138
139
		$time = \DateTime::createFromFormat( 'H:i', $attributes['time.hourminute'] );
140
		$days = explode( ',', $this->getConfigValue( 'time.weekdays', '1,2,3,4,5,6,7' ) );
141
142
		if( in_array( date( 'N' ), $days, true ) )
143
		{
144
			$start = $this->getConfigValue( 'time.start', '00:00' );
145
			$end = $this->getConfigValue( 'time.end', '23:59' );
146
147
			if( $time->getTimeStamp() < \DateTime::createFromFormat( 'H:i', $start )->getTimeStamp() ) {
148
				$result['time.hourminute'] = sprintf( 'Time value before "%1$s"', $start );
149
			}
150
151
			if( $time->getTimeStamp() > \DateTime::createFromFormat( 'H:i', $end )->getTimeStamp() ) {
152
				$result['time.hourminute'] = sprintf( 'Time value after "%1$s"', $end );
153
			}
154
		}
155
156
		return $result;
157
	}
158
}
159