Completed
Push — dev ( 1ec345...ce9639 )
by
unknown
05:02 queued 01:09
created

CheckSlots::parseResponseData()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 41
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 41
rs 8.439
c 0
b 0
f 0
cc 5
eloc 25
nc 5
nop 1
1
<?php
2
3
namespace HelePartnerSyncApi\Method;
4
5
use DateTime;
6
use HelePartnerSyncApi\Validator;
7
use HelePartnerSyncApi\ValidatorException;
8
9
class CheckSlots extends Method
10
{
11
12
	/**
13
	 * @return string
14
	 */
15
	public function getName()
16
	{
17
		return 'checkSlots';
18
	}
19
20
	/**
21
	 * @param array $data
22
	 * @return array
23
	 */
24
	protected function parseRequestData($data)
25
	{
26
		try {
27
			Validator::checkStructure($data, array(
28
				'date' => Validator::TYPE_DATE_TIME_STRING,
29
				'parameters' => Validator::TYPE_ARRAY,
30
			));
31
		} catch (ValidatorException $e) {
32
			throw new MethodException('Bad method input: ' . $e->getMessage(), $e);
33
		}
34
35
		return array(
36
			new DateTime($data['date']),
37
			$data['parameters'],
38
		);
39
	}
40
41
	/**
42
	 * @param mixed $data
43
	 * @return array
44
	 */
45
	protected function parseResponseData($data)
46
	{
47
		$exceptionPrefix = 'Bad method output: ';
48
49
		try {
50
			Validator::checkStructure($data, array(
51
				array(
52
					'startDateTime' => Validator::TYPE_DATE_TIME,
53
					'endDateTime' => Validator::TYPE_DATE_TIME,
54
					'capacity' => Validator::TYPE_INT,
55
				),
56
			));
57
		} catch (ValidatorException $e) {
58
			throw new MethodException($exceptionPrefix . $e->getMessage(), $e);
59
		}
60
61
		$result = array();
62
63
		foreach ($data as $index => $slot) {
64
			$whichSlot = sprintf('(slot #%d)', $index + 1);
65
			$startDateTime = $slot['startDateTime']->format(DateTime::W3C);
66
			$endDateTime = $slot['endDateTime']->format(DateTime::W3C);
67
			$capacity = $slot['capacity'];
68
69
			if ($startDateTime >= $endDateTime) {
70
				throw new MethodException(sprintf('%sSlot startDateTime (%s) must be before endDateTime (%s) %s', $exceptionPrefix, $startDateTime, $endDateTime, $whichSlot));
71
			}
72
73
			if ($capacity < 0) {
74
				throw new MethodException(sprintf('%sSlot capacity (%s) must be non-negative %s', $exceptionPrefix, $capacity, $whichSlot));
75
			}
76
77
			$result[] = array(
78
				'startDateTime' => $startDateTime,
79
				'endDateTime' => $endDateTime,
80
				'capacity' => $capacity,
81
			);
82
		}
83
84
		return $result;
85
	}
86
87
}
88