Completed
Push — master ( 118c60...254398 )
by Litera
03:47
created

VisitorRepository::assembleFormPrograms()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 8

Duplication

Lines 15
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 15
loc 15
c 0
b 0
f 0
cc 3
eloc 8
nc 3
nop 1
rs 9.4285
ccs 0
cts 7
cp 0
crap 12
1
<?php
2
3
namespace App\Repositories;
4
5
use App\Models\BlockModel;
6
use App\Models\MealModel;
7
use App\Models\VisitorModel;
8
use DateTime;
9
use Nette\Database\Table\ActiveRow;
10
use Nette\Utils\ArrayHash;
11
use Nette\Utils\Strings;
12
13 1
class VisitorRepository
14
{
15
16
	/**
17
	 * @var VisitorModel
18
	 */
19
	protected $visitorModel;
20
21
	/**
22
	 * @var MealModel
23
	 */
24
	protected $mealModel;
25
26
	/**
27
	 * @var BlockModel
28
	 */
29
	protected $blockModel;
30
31
	/**
32
	 * @var ProgramRepository
33
	 */
34
	protected $programRepository;
35
36
	/**
37
	 * @param VisitorModel      $visitor
38
	 * @param MealModel         $meal
39
	 * @param BlockModel        $block
40
	 * @param ProgramRepository $program
41
	 */
42
	public function __construct(
43
		VisitorModel $visitor,
44
		MealModel $meal,
45
		BlockModel $block,
46
		ProgramRepository $program
47
	) {
48 1
		$this->setVisitorModel($visitor);
49 1
		$this->setMealModel($meal);
50 1
		$this->setBlockModel($block);
51 1
		$this->setProgramRepository($program);
52 1
	}
53
54
	/**
55
	 * @param int  $meetingId
56
	 * @return self
57
	 */
58
	public function setMeeting(int $meetingId): self
59
	{
60
		$this->getVisitorModel()->setMeetingId($meetingId);
61
62
		return $this;
63
	}
64
65
	/**
66
	 * @param  int $id
67
	 * @return boolean
68
	 */
69
	public function setChecked(int $id): bool
70
	{
71
		return $this->getVisitorModel()->checked($id, '1');
72
	}
73
74
	/**
75
	 * @param  int $id
76
	 * @return boolean
77
	 */
78
	public function setUnchecked(int $id): bool
79
	{
80
		return $this->getVisitorModel()->checked($id, '0');
81
	}
82
83
	/**
84
	 * Return visitor by id
85
	 *
86
	 * @param  int    $id
87
	 * @return ActiveRow
88
	 */
89
	public function findById($id)
90
	{
91
		return $this->getVisitorModel()->find($id);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The expression $this->getVisitorModel()->find($id); of type Nette\Database\Table\IRow|false adds false to the return on line 91 which is incompatible with the return type documented by App\Repositories\VisitorRepository::findById of type Nette\Database\Table\ActiveRow. It seems like you forgot to handle an error condition.
Loading history...
92
	}
93
94
    /**
95
     * @param  int $id
96
     * @return ArrayHash
97
     */
98
    public function findExpandedById(int $id): ArrayHash
99
    {
100
        $visitor = $this->findById($id);
101
        $meals = $this->getMealModel()->findByVisitorId($id);
102
        $programs = $this->assembleFormPrograms($id);
103
104
        return ArrayHash::from(
105
            array_merge(
106
                $visitor->toArray(),
107
                $meals,
108
                $programs
109
            )
110
        );
111
    }
112
113
	/**
114
	 * Return visitor by guid
115
	 *
116
	 * @param  string  $guid
117
	 * @return Nette\Database\Table\ActiveRow|bool
118
	 */
119
	public function findByGuid($guid)
120
	{
121
		return $this->getVisitorModel()->findBy('guid', $guid);
122
	}
123
124
	/**
125
	 * @param  string $guid
126
	 * @return array
127
	 */
128
	public function findExpandedByGuid(string $guid): array
129
	{
130
		$visitor = $this->getVisitorModel()->findByGuid($guid);
131
		$meals = $this->getMealModel()->findByVisitorId($visitor->id);
132
		$programs = $this->assembleFormPrograms($visitor->id);
133
134
		return array_merge($visitor->toArray(), $meals, $programs);
135
	}
136
137
	/**
138
	 * @param  string $value
139
	 * @return array
140
	 */
141
	public function findBySearch(string $value = ''): array
142
	{
143
		return $this->getVisitorModel()->setSearch($value)->all();
144
	}
145
146
	/**
147
	 * @param  int  $id
148
	 * @return array
149
	 */
150
	public function findRecipients($id): array
151
	{
152
		return $this->getVisitorModel()->getRecipients($id);
153
	}
154
155
	/**
156
	 * @param  array   $data
157
	 * @return string
158
	 */
159
	public function create($data)
160
	{
161
		$visitor = $this->filterFields($data, $this->getVisitorModel()->getColumns());
162
		$visitor['code'] = $this->calculateCode4Bank(
163
			$visitor['name'],
164
			$visitor['surname'],
165
			$visitor['birthday']->format('d. m. Y')
166
		);
167
		$meals = $this->filterFields($data, $this->getMealModel()->getColumns());
168
		$programs = $this->filterProgramFields($data);
169
170
		$guid = $this->getVisitorModel()->assemble($visitor, $meals, $programs, true);
171
172
		return $guid;
173
	}
174
175
	/**
176
	 * @param  integer $id
177
	 * @param  array   $data
0 ignored issues
show
Bug introduced by
There is no parameter named $data. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
178
	 * @return integer
179
	 */
180 View Code Duplication
	public function update($id, $values)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
181
	{
182
		$visitor = $this->filterFields($values, $this->getVisitorModel()->getColumns());
183
184
		$visitor['birthday'] = $this->convertToDateTime($visitor['birthday']);
185
186
		$visitor['code'] = $this->calculateCode4Bank(
187
			$visitor['name'],
188
			$visitor['surname'],
189
			$visitor['birthday']->format('d. m. Y')
190
		);
191
		$meals = $this->filterFields($values, $this->getMealModel()->getColumns());
192
		$programs = $this->filterProgramFields($values);
193
194
		$id = $this->getVisitorModel()->modify($id, $visitor, $meals, $programs);
195
196
		return $id;
197
	}
198
199
    /**
200
     * @param  integer $id
0 ignored issues
show
Bug introduced by
There is no parameter named $id. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
201
     * @param  array   $data
0 ignored issues
show
Bug introduced by
There is no parameter named $data. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
202
     * @return integer
203
     */
204 View Code Duplication
    public function updateByGuid($guid, $values)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
205
    {
206
        $visitor = $this->filterFields($values, $this->getVisitorModel()->getColumns());
207
208
        $visitor['birthday'] = $this->convertToDateTime($visitor['birthday']);
209
210
        $visitor['code'] = $this->calculateCode4Bank(
211
            $visitor['name'],
212
            $visitor['surname'],
213
            $visitor['birthday']->format('d. m. Y')
214
        );
215
216
        $meals = $this->filterFields($values, $this->getMealModel()->getColumns());
217
        $programs = $this->filterProgramFields($values);
218
219
        $guid = $this->getVisitorModel()->modifyByGuid($guid, $visitor, $meals, $programs);
220
221
        return $guid;
222
    }
223
224
	/**
225
	 * @param  integer $id
226
	 * @return boolean
227
	 */
228
	public function delete($id)
229
	{
230
		return $this->getVisitorModel()->delete($id);
231
	}
232
233
	/**
234
	 * Counts visitors
235
	 *
236
	 * @return int
237
	 */
238
	public function count(): int
239
	{
240
		return $this->getVisitorModel()->getCount();
241
	}
242
243
    /**
244
     * @param  int $id
245
     * @return string
246
     * @throws \Exception
247
     */
248
	public function payCostCharge($id)
249
	{
250
		return $this->getVisitorModel()->payCharge($id, 'cost');
251
	}
252
253
    /**
254
     * @param  int $id
255
     * @return string
256
     * @throws \Exception
257
     */
258
	public function payAdvanceCharge($id)
259
	{
260
		return $this->getVisitorModel()->payCharge($id, 'advance');
261
	}
262
263
	/**
264
	 * @param  string $name
265
	 * @param  string $surname
266
	 * @param  string $birthday
267
	 * @return string
268
	 */
269
	public function calculateCode4Bank(string $name, string $surname, string $birthday): string
270
	{
271 1
		return Strings::substring($name, 0, 1)
272 1
			. Strings::substring($surname, 0, 1)
273 1
			. Strings::substring($birthday, -2);
274
	}
275
276
	/**
277
	 * @param  int    $visitorId
278
	 * @return array
279
	 */
280 View Code Duplication
	public function assembleFormPrograms(int $visitorId): array
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
281
	{
282
		$visitorPrograms = $this->getVisitorModel()->findVisitorPrograms($visitorId);
283
284
		$formPrograms = [];
285
286
		foreach ($visitorPrograms as $visitorProgram) {
287
			if($visitorProgram->program !== 0) {
0 ignored issues
show
Bug introduced by
Accessing program on the interface Nette\Database\IRow suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
288
				$program = $this->getProgramRepository()->find($visitorProgram->program);
0 ignored issues
show
Bug introduced by
Accessing program on the interface Nette\Database\IRow suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
289
				$formPrograms['blck_' . $program->block] = $visitorProgram->program;
0 ignored issues
show
Bug introduced by
Accessing program on the interface Nette\Database\IRow suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
290
			}
291
		}
292
293
		return $formPrograms;
294
	}
295
296
	/**
297
	 * @param  array  $data
298
	 * @param  array  $fields
299
	 * @return array
300
	 */
301
	protected function filterFields($data, array $fields)
302
	{
303 1
		return array_intersect_key((array) $data, array_flip($fields));
304
	}
305
306
	/**
307
	 * @param  array $data
308
	 * @return array
309
	 */
310
	protected function filterProgramFields($data)
311
	{
312 1
		$blocks = $this->getBlockModel()->idsFromCurrentMeeting($data['meeting']);
313
314 1
		$programs = array_map(function($block) use ($data) {
315 1
			if(!array_key_exists('blck_' . $block['id'], $data)) {
316 1
				return 0;
317
			}
318
319 1
			return $data['blck_' . $block['id']];
320 1
		}, $blocks);
321
322 1
		return $programs;
323
	}
324
325
	/**
326
	 * @param  string|DateTime $datetime
327
	 * @return DateTime
328
	 */
329
	protected function convertToDateTime($datetime): DateTime
330
	{
331 1
		if (is_string($datetime)) {
332 1
			$datetime = new DateTime($datetime);
333
		}
334
335 1
		return $datetime;
336
	}
337
338
	/**
339
	 * @return BlockModel
340
	 */
341
	protected function getBlockModel(): BlockModel
342
	{
343 1
		return $this->blockModel;
344
	}
345
346
	/**
347
	 * @param  BlockModel $model
348
	 * @return $this
349
	 */
350
	protected function setBlockModel(BlockModel $model): self
351
	{
352 1
		$this->blockModel = $model;
353
354 1
		return $this;
355
	}
356
357
	/**
358
	 * @return MealModel
359
	 */
360
	protected function getMealModel(): MealModel
361
	{
362
		return $this->mealModel;
363
	}
364
365
	/**
366
	 * @param  MealModel $model
367
	 * @return $this
368
	 */
369
	protected function setMealModel(MealModel $model): self
370
	{
371 1
		$this->mealModel = $model;
372
373 1
		return $this;
374
	}
375
376
	/**
377
	 * @return VisitorModel
378
	 */
379
	protected function getVisitorModel(): VisitorModel
380
	{
381
		return $this->visitorModel;
382
	}
383
384
	/**
385
	 * @param  VisitorModel $model
386
	 * @return self
387
	 */
388
	protected function setVisitorModel(VisitorModel $model): self
389
	{
390 1
		$this->visitorModel = $model;
391
392 1
		return $this;
393
	}
394
395
	/**
396
	 * @return ProgramRepository
397
	 */
398
	protected function getProgramRepository()
399
	{
400
		return $this->programRepository;
401
	}
402
403
	/**
404
	 * @param  ProgramRepository $repository
405
	 * @return self
406
	 */
407
	protected function setProgramRepository(ProgramRepository $repository): self
408
	{
409 1
		$this->programRepository = $repository;
410
411 1
		return $this;
412
	}
413
414
}
415