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

ProgramRepository   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 203
Duplicated Lines 7.39 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 15
loc 203
c 0
b 0
f 0
rs 10
wmc 22
lcom 1
cbo 2

18 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A setMeetingId() 0 7 1
A all() 0 4 1
A find() 0 4 1
A findByVisitorId() 0 4 1
A findByBlockId() 0 4 1
A findTutor() 0 4 1
A findVisitors() 0 4 1
A create() 0 6 1
A update() 0 6 1
A delete() 0 4 1
A countVisitors() 0 4 1
A assembleFormPrograms() 15 15 3
A transformDisplayInRegValue() 0 10 3
A getProgramModel() 0 4 1
A setProgramModel() 0 6 1
A getVisitorModel() 0 4 1
A setVisitorModel() 0 6 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
68
	{
69
		return $this->getProgramModel()->findByVisitorId($visitorId);
70
	}
71
72
    /**
73
     * @param int $blockId
74
     * @return array
75
     */
76
    public function findByBlockId(int $blockId): array
77
    {
78
        return $this->getProgramModel()->findByBlockId($blockId);
79
	}
80
81
	/**
82
	 * @param  int    $programId
83
	 * @return array
84
	 */
85
	public function findTutor(int $programId): ActiveRow
86
	{
87
		return $this->getProgramModel()->getTutor($programId);
88
	}
89
90
	/**
91
	 * @param  int   $programId
92
	 * @return array
93
	 */
94
	public function findVisitors(int $programId): array
95
	{
96
		return $this->getProgramModel()->findProgramVisitors($programId);
97
	}
98
99
	/**
100
	 * @param  Nette\Utils\ArrayHash $program
101
	 * @return boolean
102
	 */
103
	public function create(ArrayHash $program)
104
	{
105
		$program = $this->transformDisplayInRegValue($program);
106
107
		return $this->getProgramModel()->create((array) $program);
108
	}
109
110
	/**
111
	 * @param  Nette\Utils\ArrayHash $program
112
	 * @return boolean
113
	 */
114
	public function update(int $id, ArrayHash $program)
115
	{
116
		$program = $this->transformDisplayInRegValue($program);
117
118
		return $this->getProgramModel()->update($id, (array) $program);
119
	}
120
121
	/**
122
	 * @param  int    $id
123
	 * @return boolean
124
	 */
125
	public function delete(int $id)
126
	{
127
		return $this->getProgramModel()->delete($id);
128
	}
129
130
    /**
131
     * @param  int $programId
132
     * @return int
133
     */
134
    public function countVisitors(int $programId): int
135
    {
136
        return $this->getProgramModel()->countProgramVisitors($programId);
137
	}
138
139
	/**
140
	 * @param  int    $visitorId
141
	 * @return array
142
	 */
143 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...
144
	{
145
		$visitorPrograms = $this->getVisitorModel()->findVisitorPrograms($visitorId);
146
147
		$formPrograms = [];
148
149
		foreach ($visitorPrograms as $visitorProgram) {
150
			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...
151
				$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...
Documentation Bug introduced
The method findByProgramId does not exist on object<App\Models\ProgramModel>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
152
				$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...
153
			}
154
		}
155
156
		return $formPrograms;
157
	}
158
159
	/**
160
	 * @param  Nette\Utils\ArrayHash $program
161
	 * @return Nette\Utils\ArrayHash
162
	 */
163
	protected function transformDisplayInRegValue(ArrayHash $program): ArrayHash
164
	{
165
		if(array_key_exists('display_in_reg', $program) && empty($program['display_in_reg'])) {
166
			$program['display_in_reg'] = '1';
167
		} else {
168
			$program['display_in_reg'] = '0';
169
		}
170
171
		return $program;
172
	}
173
174
	/**
175
	 * @return ProgramModel
176
	 */
177
	protected function getProgramModel()
178
	{
179
		return $this->programModel;
180
	}
181
182
	/**
183
	 * @param  ProgramModel $model
184
	 * @return $this
185
	 */
186
	protected function setProgramModel(ProgramModel $model): self
187
	{
188
		$this->programModel = $model;
189
190
		return $this;
191
	}
192
193
	/**
194
	 * @return VisitorModel
195
	 */
196
	protected function getVisitorModel(): VisitorModel
197
	{
198
		return $this->visitorModel;
199
	}
200
201
	/**
202
	 * @param  VisitorModel $model
203
	 * @return self
204
	 */
205
	protected function setVisitorModel(VisitorModel $model): self
206
	{
207
		$this->visitorModel = $model;
208
209
		return $this;
210
	}
211
212
}
213