Completed
Pull Request — devel (#144)
by Litera
03:43
created

BlockPresenter::setMeetingModel()   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\Presenters;
4
5
use App\Services\Emailer;
6
use App\Repositories\BlockRepository;
7
use App\Components\Forms\Factories\IBlockFormFactory;
8
use App\Components\Forms\BlockForm;
9
use \Exception;
10
use Nette\Utils\ArrayHash;
11
12
/**
13
 * Block controller
14
 *
15
 * This file handles the retrieval and serving of blocks
16
 *
17
 * @created 2013-06-03
18
 * @author Tomas Litera <[email protected]>
19
 */
20
class BlockPresenter extends BasePresenter
21
{
22
23
	const REDIRECT_DEFAULT = 'Block:listing';
24
25
	/**
26
	 * @var integer
27
	 */
28
	private $blockId = NULL;
29
30
	/**
31
	 * @var Emailer
32
	 */
33
	private $emailer;
34
35
	/**
36
	 * @var BlockRepository
37
	 */
38
	private $blockRepository;
39
40
	/**
41
	 * @var IBlockFormFactory
42
	 */
43
	private $blockFormFactory;
44
45
	/**
46
	 * @param BlockRepository $blockRepository
47
	 * @param Emailer         $emailer
48
	 */
49
	public function __construct(BlockRepository $blockRepository, Emailer $emailer)
50
	{
51
		$this->setBlockRepository($blockRepository);
52
		$this->setEmailer($emailer);
53
	}
54
55
	/**
56
	 * @param  IBlockFormFactory $factory
57
	 */
58
	public function injectBlockFormFactory(IBlockFormFactory $factory)
59
	{
60
		$this->blockFormFactory = $factory;
61
	}
62
63
	/**
64
	 * @return void
65
	 */
66
	public function startup()
67
	{
68
		parent::startup();
69
70
		$meetingId = $this->getMeetingId();
71
		$this->getBlockRepository()->setMeetingId($meetingId);
72
	}
73
74
	/**
75
	 * @return void
76
	 */
77
	public function actionCreate(ArrayHash $block)
78
	{
79
		try {
80
			$this->logInfo('Storing new block.');
81
82
			$result = $this->getBlockRepository()->create($block);
83
84
			$this->logInfo('Creation of block successfull, result: ' . json_encode($result));
85
			$this->flashSuccess('Položka byla úspěšně vytvořena');
86
		} catch(Exception $e) {
87
			$this->logError('Creation of block with data ' . json_encode($block) . ' failed, result: ' . $e->getMessage());
88
			$this->flashFailure('Creation of block failed, result: ' . $e->getMessage());
89
			$result = false;
90
		}
91
92
		return $result;
93
	}
94
95
	/**
96
	 * @param  integer   $id
97
	 * @param  ArrayHash $block
98
	 * @return boolean
99
	 */
100
	public function actionUpdate($id, ArrayHash $block)
101
	{
102
		try {
103
			$this->logInfo('Updating block.');
104
105
			$result = $this->getBlockRepository()->update($id, $block);
106
107
			$this->logInfo('Modification of block id %s with data =s successfull, result: %s', [
108
				$id,
109
				json_encode($block),
110
				json_encode($result),
111
			]);
112
			$this->flashSuccess('Položka byla úspěšně uložena.');
113
		} catch(Exception $e) {
114
			$this->logError('Záznam se nepodařilo uložit.');
115
			$this->flashFailure('Modification of block id ' . $id . ' failed, result: ' . $e->getMessage());
116
			$result = false;
117
		}
118
119
		return $result;
120
	}
121
122
	/**
123
	 * @param  int $id
124
	 * @return void
125
	 * @throws \Nette\Application\AbortException
126
	 */
127
	public function actionDelete($id)
128
	{
129
		try {
130
			$result = $this->getBlockRepository()->delete($id);
131
			$this->logInfo('Destroying of block successfull, result: ' . json_encode($result));
132
			$this->flashSuccess('Položka byla úspěšně smazána.');
133
		} catch(Exception $e) {
134
			$this->logError('Destroying of block failed, result: ' .  $e->getMessage());
135
			$this->flashFailure('Destroying of block failed, result: ' . $e->getMessage());
136
		}
137
138
		$this->redirect(self::REDIRECT_DEFAULT);
139
	}
140
141
	/**
142
	 * Send mail to tutor
143
	 *
144
	 * @param $id
145
	 * @return void
146
	 * @throws \Nette\Application\AbortException
147
	 */
148
	public function actionMail($id)
149
	{
150
		try {
151
			$tutors = $this->getBlockRepository()->findTutor($id);
152
			$recipients = $this->parseTutorEmail($tutors);
153
154
			$this->getEmailer()->tutor($recipients, $tutors->guid, 'block');
155
156
			$this->logInfo('Sending email to block tutor successfull, result: ' . json_encode($recipients) . ', ' . $tutors->guid);
157
			$this->flashSuccess('Email lektorovi byl odeslán..');
158
		} catch(Exception $e) {
159
			$this->logError('Sending email to block tutor failed, result: ' .  $e->getMessage());
160
			$this->flashFailure('Email lektorovi nebyl odeslán, result: ' . $e->getMessage());
161
		}
162
163
		$this->redirect('Block:edit', $id);
164
	}
165
166
	/**
167
	 * @return void
168
	 */
169
	public function renderNew()
170
	{
171
		$template = $this->getTemplate();
172
		$template->heading = 'nový blok';
0 ignored issues
show
Bug introduced by
Accessing heading on the interface Nette\Application\UI\ITemplate 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...
173
	}
174
175
	/**
176
	 * Prepare data for editing
177
	 *
178
	 * @param  int $id of Block
179
	 * @return void
180
	 */
181
	public function renderEdit($id)
182
	{
183
		$template = $this->getTemplate();
184
185
		$template->heading = 'úprava bloku';
0 ignored issues
show
Bug introduced by
Accessing heading on the interface Nette\Application\UI\ITemplate 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...
186
187
		$this->blockId = $id;
188
		$block = $this->getBlockRepository()->find($id);
189
		$template->block = $block;
0 ignored issues
show
Bug introduced by
Accessing block on the interface Nette\Application\UI\ITemplate 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...
190
		$template->id = $id;
0 ignored issues
show
Bug introduced by
Accessing id on the interface Nette\Application\UI\ITemplate 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...
191
192
		$block->start_hour = (int) $block->from->format('%H');
0 ignored issues
show
Bug introduced by
The property start_hour does not seem to exist in App\Entities\BlockEntity.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
193
		$block->start_minute = (int) $block->from->format('%I');
0 ignored issues
show
Bug introduced by
The property start_minute does not seem to exist in App\Entities\BlockEntity.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
194
		$block->end_hour = (int) $block->to->format('%H');
0 ignored issues
show
Bug introduced by
The property end_hour does not seem to exist in App\Entities\BlockEntity.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
195
		$block->end_minute = (int) $block->to->format('%I');
0 ignored issues
show
Bug introduced by
The property end_minute does not seem to exist in App\Entities\BlockEntity.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
196
197
		$this['blockForm']->setDefaults($block);
198
	}
199
200
	/**
201
	 * @return void
202
	 */
203
	public function renderListing()
204
	{
205
		$template = $this->getTemplate();
206
		$template->blocks = $this->getBlockRepository()->all();
0 ignored issues
show
Bug introduced by
Accessing blocks on the interface Nette\Application\UI\ITemplate 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...
207
		$template->mid = $this->meetingId;
0 ignored issues
show
Bug introduced by
Accessing mid on the interface Nette\Application\UI\ITemplate 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...
208
		$template->heading = $this->heading;
0 ignored issues
show
Bug introduced by
Accessing heading on the interface Nette\Application\UI\ITemplate 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...
209
	}
210
211
	/**
212
	 * @return BlockForm
213
	 */
214 View Code Duplication
	protected function createComponentBlockForm(): BlockForm
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...
215
	{
216
		$control = $this->blockFormFactory->create();
217
		$control->setMeetingId($this->getMeetingId());
218
		$control->onBlockSave[] = function(BlockForm $control, $block) {
219
			//$guid = $this->getParameter('guid');
220
			$id = $this->getParameter('id');
221
222
			$this->setBacklinkFromArray($block);
223
224
			if($id) {
225
				$this->actionUpdate($id, $block);
226
			} else {
227
				$this->actionCreate($block);
228
			}
229
230
			$this->redirect($this->getBacklink() ?: self::REDIRECT_DEFAULT);
231
		};
232
233
		$control->onBlockReset[] = function(BlockForm $control, $block) {
234
			$this->setBacklinkFromArray($block);
235
236
			$this->redirect($this->getBacklink() ?: self::REDIRECT_DEFAULT);
237
		};
238
239
		return $control;
240
	}
241
242
	/**
243
	 * @return Emailer
244
	 */
245
	protected function getEmailer(): Emailer
246
	{
247
		return $this->emailer;
248
	}
249
250
	/**
251
	 * @param  Emailer $emailer
252
	 * @return self
253
	 */
254
	protected function setEmailer(Emailer $emailer): self
255
	{
256
		$this->emailer = $emailer;
257
		return $this;
258
	}
259
260
	/**
261
	 * @return BlockRepository
262
	 */
263
	private function getBlockRepository(): BlockRepository
264
	{
265
		return $this->blockRepository;
266
	}
267
268
	/**
269
	 * @param  BlockRepository $blockRepository
270
	 * @return self
271
	 */
272
	private function setBlockRepository(BlockRepository $blockRepository): self
273
	{
274
		$this->blockRepository = $blockRepository;
275
276
		return $this;
277
	}
278
279
280
281
}
282