Test Failed
Pull Request — master (#140)
by Litera
10:13
created

ProgramRepository::setVisitorModel()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
rs 9.4285
1
<?php
2
3
namespace App\Repositories;
4
5
use App\Models\ProgramModel;
6
use App\Models\VisitorModel;
7
use Nette\Database\Table\ActiveRow;
8
use Nette\Utils\ArrayHash;
9
10
class ProgramRepository
11
{
12
13
	/**
14
	 * @var VisitorModel
15
	 */
16
	protected $visitorModel;
17
18
	/**
19
	 * @var ProgramModel
20
	 */
21
	protected $programModel;
22
23
	/**
24
	 * @param VisitorModel $visitorModel
25
	 * @param ProgramModel $programModel
26
	 */
27
	public function __construct(
28
		VisitorModel $visitorModel,
29
		ProgramModel $programModel
30
	) {
31
		$this->setVisitorModel($visitorModel);
32
		$this->setProgramModel($programModel);
33
	}
34
35
	/**
36
	 * @param int $meetingId
37
	 */
38
	public function setMeetingId(int $meetingId): self
39
	{
40
		$this->getProgramModel()->setMeetingId($meetingId);
41
		$this->getVisitorModel()->setMeetingId($meetingId);
42
43
		return $this;
44
	}
45
46
	/**
47
	 * @return array
48
	 */
49
	public function all(): array
50
	{
51
		return $this->getProgramModel()->all();
52
	}
53
54
	/**
55
	 * @param  int  $id
56
	 * @return Nette\Database\Table\ActiveRow
57
	 */
58
	public function find(int $id): ActiveRow
59
	{
60
		return $this->getProgramModel()->find($id);
61
	}
62
63
	/**
64
	 * @param  int  $VisitorId
65
	 * @return array
66
	 */
67
	public function findByVisitorId(int $VisitorId): array
0 ignored issues
show
Unused Code introduced by
The parameter $VisitorId is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
68
	{
69
		return $this->getProgramModels()->findByVisitorId($visitorId);
0 ignored issues
show
Bug introduced by
The variable $visitorId does not exist. Did you mean $VisitorId?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
Bug introduced by
The method getProgramModels() does not exist on App\Repositories\ProgramRepository. Did you maybe mean getProgramModel()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
70
	}
71
72
	/**
73
	 * @param  int    $programId
74
	 * @return array
75
	 */
76
	public function findTutor(int $programId): ActiveRow
77
	{
78
		return $this->getProgramModel()->getTutor($programId);
79
	}
80
81
	/**
82
	 * @param  int   $programId
83
	 * @return array
84
	 */
85
	public function findVisitors(int $programId): array
86
	{
87
		return $this->getProgramModel()->findProgramVisitors($programId);
88
	}
89
90
	/**
91
	 * @param  Nette\Utils\ArrayHash $program
92
	 * @return boolean
93
	 */
94
	public function create(ArrayHash $program)
95
	{
96
		$program = $this->transformDisplayInRegValue($program);
97
98
		return $this->getProgramModel()->create((array) $program);
99
	}
100
101
	/**
102
	 * @param  Nette\Utils\ArrayHash $program
103
	 * @return boolean
104
	 */
105
	public function update(int $id, ArrayHash $program)
106
	{
107
		$program = $this->transformDisplayInRegValue($program);
108
109
		return $this->getProgramModel()->update($id, (array) $program);
110
	}
111
112
	/**
113
	 * @param  int    $id
114
	 * @return boolean
115
	 */
116
	public function delete(int $id)
117
	{
118
		return $this->getProgramModel()->delete($id);
119
	}
120
121
	/**
122
	 * @param  int    $visitorId
123
	 * @return array
124
	 */
125 View Code Duplication
	public function assembleFormPrograms(int $visitorId): array
0 ignored issues
show
Duplication introduced
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...
126
	{
127
		$visitorPrograms = $this->getVisitorModel()->findVisitorPrograms($visitorId);
128
129
		$formPrograms = [];
130
131
		foreach ($visitorPrograms as $visitorProgram) {
132
			if($visitorProgram->program !== 0) {
0 ignored issues
show
Bug introduced
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...
133
				$program = $this->getProgramModel()->findByProgramId($visitorProgram->program);
0 ignored issues
show
Bug introduced
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...
134
				$formPrograms['blck_' . $program->block] = $visitorProgram->program;
0 ignored issues
show
Bug introduced
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...
135
			}
136
		}
137
138
		return $formPrograms;
139
	}
140
141
	/**
142
	 * @param  Nette\Utils\ArrayHash $program
143
	 * @return Nette\Utils\ArrayHash
144
	 */
145
	protected function transformDisplayInRegValue(ArrayHash $program): ArrayHash
146
	{
147
		if(array_key_exists('display_in_reg', $program) && empty($program['display_in_reg'])) {
148
			$program['display_in_reg'] = '1';
149
		} else {
150
			$program['display_in_reg'] = '0';
151
		}
152
153
		return $program;
154
	}
155
156
	/**
157
	 * @return ProgramModel
158
	 */
159
	protected function getProgramModel()
160
	{
161
		return $this->programModel;
162
	}
163
164
	/**
165
	 * @param  ProgramModel $model
166
	 * @return $this
167
	 */
168
	protected function setProgramModel(ProgramModel $model): self
169
	{
170
		$this->programModel = $model;
171
172
		return $this;
173
	}
174
175
	/**
176
	 * @return VisitorModel
177
	 */
178
	protected function getVisitorModel(): VisitorModel
179
	{
180
		return $this->visitorModel;
181
	}
182
183
	/**
184
	 * @param  VisitorModel $model
185
	 * @return VisitorService
186
	 */
187
	protected function setVisitorModel(VisitorModel $model): self
188
	{
189
		$this->visitorModel = $model;
190
191
		return $this;
192
	}
193
194
}
195