Test Failed
Pull Request — master (#125)
by Litera
08:49
created

VisitorService::setVisitorModel()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace App\Services;
4
5
use DateTime;
6
use App\Models\VisitorModel;
7
use App\Models\MealModel;
8
use App\Models\BlockModel;
9
use App\Services\ProgramService;
10
use Nette\Utils\Strings;
11
12
class VisitorService
13
{
14
15
	/**
16
	 * @var VisitorModel
17
	 */
18
	protected $visitorModel;
19
20
	/**
21
	 * @var MealModel
22
	 */
23
	protected $mealModel;
24
25
	/**
26
	 * @var BlockModel
27
	 */
28
	protected $blockModel;
29
30
	/**
31
	 * @var ProgramService
32
	 */
33
	protected $programService;
34
35
	/**
36
	 * @param VisitorModel   $visitor
37
	 * @param MealModel      $meal
38
	 * @param BlockModel     $block
39
	 * @param ProgramService $program
40
	 */
41
	public function __construct(
42
		VisitorModel $visitor,
43
		MealModel $meal,
44
		BlockModel $block,
45
		ProgramService $program
46
	) {
47
		$this->setVisitorModel($visitor);
48
		$this->setMealModel($meal);
49
		$this->setBlockModel($block);
50
		$this->setProgramService($program);
51
	}
52
53
	/**
54
	 * @param  array   $data
55
	 * @return string
56
	 */
57
	public function create(array $data = [])
58
	{
59
		$visitor = $this->filterFields($data, $this->getVisitorModel()->getColumns());
60
		$visitor['code'] = $this->calculateCode4Bank(
61
			$visitor['name'],
62
			$visitor['surname'],
63
			$visitor['birthday']->format('d. m. Y')
64
		);
65
		$meals = $this->filterFields($data, $this->getMealModel()->getColumns());
66
		$programs = $this->filterProgramFields($data);
67
68
		$guid = $this->getVisitorModel()->assemble($visitor, $meals, $programs, true);
69
70
		return $guid;
71
	}
72
73
	/**
74
	 * @param  integer $id
75
	 * @param  array   $data
76
	 * @return integer
77
	 */
78
	public function update($id, array $data)
79
	{
80
		$visitor = $this->filterFields($data, $this->getVisitorModel()->getColumns());
81
82
		$visitor['birthday'] = $this->convertToDateTime($visitor['birthday']);
83
84
		$visitor['code'] = $this->calculateCode4Bank(
85
			$visitor['name'],
86
			$visitor['surname'],
87
			$visitor['birthday']->format('d. m. Y')
88
		);
89
		$meals = $this->filterFields($data, $this->getMealModel()->getColumns());
90
		$programs = $this->filterProgramFields($data);
91
92
		if(is_numeric($id)) {
93
			$id = $this->getVisitorModel()->modify($id, $visitor, $meals, $programs);
94
		} else {
95
			$id = $this->getVisitorModel()->modifyByGuid($id, $visitor, $meals, $programs);
96
		}
97
98
		return $id;
99
	}
100
101
	/**
102
	 * @param  integer $id
103
	 * @return boolean
104
	 */
105
	public function delete($id)
106
	{
107
		return $this->getVisitorModel()->delete($id);
108
	}
109
110
	/**
111
	 * @param  string $name
112
	 * @param  string $surname
113
	 * @param  string $birthday
114
	 * @return string
115
	 */
116
	public function calculateCode4Bank(string $name, string $surname, string $birthday): string
117
	{
118
		return Strings::substring($name, 0, 1)
119
			. Strings::substring($surname, 0, 1)
120
			. Strings::substring($birthday, -2);
121
	}
122
123
	/**
124
	 * @param  string $guid
125
	 * @return array
126
	 */
127
	public function findByGuid(string $guid): array
128
	{
129
		$visitor = $this->getVisitorModel()->findByGuid($guid);
130
		$meals = $this->getMealModel()->findByVisitorId($visitor->id);
131
		$programs = $this->getProgramService()->assembleFormPrograms($visitor->id);
132
133
		return array_merge($visitor->toArray(), $meals->toArray(), $programs);
134
	}
135
136
	/**
137
	 * @param  array  $data
138
	 * @param  array  $fields
139
	 * @return array
140
	 */
141
	protected function filterFields(array $data, array $fields)
142
	{
143
		return array_intersect_key($data, array_flip($fields));
144
	}
145
146
	/**
147
	 * @param  array $data
148
	 * @return array
149
	 */
150
	protected function filterProgramFields(array $data)
151
	{
152
		$blocks = $this->getBlockModel()->idsFromCurrentMeeting($data['meeting']);
153
154
		$programs = array_map(function($block) use ($data) {
155
			if(!array_key_exists('blck_' . $block['id'], $data)) {
156
				return 0;
157
			}
158
159
			return $data['blck_' . $block['id']];
160
		}, $blocks);
161
162
		return $programs;
163
	}
164
165
	/**
166
	 * @param  string|DateTime $datetime
167
	 * @return DateTime
168
	 */
169
	protected function convertToDateTime($datetime): DateTime
170
	{
171
		if (is_string($datetime)) {
172
			$datetime = new DateTime($datetime);
173
		}
174
175
		return $datetime;
176
	}
177
178
	/**
179
	 * @return BlockModel
180
	 */
181
	protected function getBlockModel()
182
	{
183
		return $this->blockModel;
184
	}
185
186
	/**
187
	 * @param  BlockModel $model
188
	 * @return $this
189
	 */
190
	protected function setBlockModel(BlockModel $model)
191
	{
192
		$this->blockModel = $model;
193
194
		return $this;
195
	}
196
197
	/**
198
	 * @return MealModel
199
	 */
200
	protected function getMealModel()
201
	{
202
		return $this->mealModel;
203
	}
204
205
	/**
206
	 * @param  MealModel $model
207
	 * @return $this
208
	 */
209
	protected function setMealModel(MealModel $model)
210
	{
211
		$this->mealModel = $model;
212
213
		return $this;
214
	}
215
216
	/**
217
	 * @return VisitorModel
218
	 */
219
	protected function getVisitorModel()
220
	{
221
		return $this->visitorModel;
222
	}
223
224
	/**
225
	 * @param  VisitorModel $model
226
	 * @return VisitorService
227
	 */
228
	protected function setVisitorModel(VisitorModel $model): VisitorService
229
	{
230
		$this->visitorModel = $model;
231
232
		return $this;
233
	}
234
235
	/**
236
	 * @return ProgramService
237
	 */
238
	protected function getProgramService()
239
	{
240
		return $this->programService;
241
	}
242
243
	/**
244
	 * @param  ProgramService $service
245
	 * @return ProgramService
246
	 */
247
	protected function setProgramService(ProgramService $service): VisitorService
248
	{
249
		$this->programService = $service;
250
251
		return $this;
252
	}
253
254
}
255