AnnotationService::getModelByType()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 12
nc 3
nop 1
dl 0
loc 16
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace App\Services;
4
5
use Nette\Utils\ArrayHash;
6
use App\Models\BlockModel;
7
use App\Models\ProgramModel;
8
use Nette\Database\Table\ActiveRow;
9
10
class AnnotationService
11
{
12
13
	/**
14
	 * @var BlockModel
15
	 */
16
	protected $blockModel;
17
18
	/**
19
	 * @var ProgramModel
20
	 */
21
	protected $programModel;
22
23
	/**
24
	 * @param BlockModel $block
25
	 * @param ProgramModel $program
26
	 */
27
	public function __construct(BlockModel $block, ProgramModel $program)
28
	{
29
		$this->setBlockModel($block);
30
		$this->setProgramModel($program);
31
	}
32
33
	/**
34
	 * @param  string $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...
35
	 * @param  string $type
36
	 * @return Row
37
	 */
38
	public function findByType(string $guid, string $type)
39
	{
40
		return $this->getModelByType($type)->findBy('guid', $guid);
41
	}
42
43
	/**
44
	 * @param  ActiveRow $annotation
45
	 * @return ActiveRow
46
	 */
47
	public function findParentProgram(ActiveRow $annotation): ActiveRow
48
	{
49
		if(array_key_exists('block', $annotation->toArray())) {
50
			$parentProgram = $this->getBlockModel()->find($annotation->block);
51
		} else {
52
			$parentProgram = $annotation;
53
		}
54
55
		return $parentProgram;
56
	}
57
58
	/**
59
	 * @param  string    $type
60
	 * @param  ArrayHash $annotation
61
	 * @return Row
62
	 */
63
	public function updateByType(string $type, ArrayHash $annotation)
64
	{
65
		return $this->getModelByType($type)->updateBy('guid', $annotation->guid, (array) $annotation);
66
	}
67
68
	/**
69
	 * @param  string $type
70
	 * @return ProgramModel | BLockModel
71
	 */
72
	protected function getModelByType(string $type)
73
	{
74
		switch ($type) {
75
			case 'block':
76
				$model = $this->getBlockModel();
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->getBlockModel(); of type App\Models\BlockModel adds the type App\Models\BlockModel to the return on line 86 which is incompatible with the return type documented by App\Services\AnnotationService::getModelByType of type App\Models\ProgramModel.
Loading history...
77
				break;
78
			case 'program':
79
				$model = $this->getProgramModel();
80
				break;
81
			default:
82
				throw new Exception('Annotation model not found!');
83
				break;
0 ignored issues
show
Unused Code introduced by
break; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
84
		}
85
86
		return $model;
87
	}
88
89
	/**
90
	 * @return ProgramModel
91
	 */
92
	protected function getProgramModel()
93
	{
94
		return $this->programModel;
95
	}
96
97
	/**
98
	 * @param  ProgramModel $model
99
	 * @return self
100
	 */
101
	protected function setProgramModel(ProgramModel $model): self
102
	{
103
		$this->programModel = $model;
104
105
		return $this;
106
	}
107
108
	/**
109
	 * @return BlockModel
110
	 */
111
	protected function getBlockModel(): BlockModel
112
	{
113
		return $this->blockModel;
114
	}
115
116
	/**
117
	 * @param  BlockModel $model
118
	 * @return self
119
	 */
120
	protected function setBlockModel(BlockModel $model): self
121
	{
122
		$this->blockModel = $model;
123
124
		return $this;
125
	}
126
127
}
128