Passed
Pull Request — master (#16)
by Anton
03:08
created

Action::handle()   B

Complexity

Conditions 5
Paths 6

Size

Total Lines 24
Code Lines 7

Duplication

Lines 4
Ratio 16.67 %

Importance

Changes 0
Metric Value
cc 5
eloc 7
nc 6
nop 0
dl 4
loc 24
rs 8.5125
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * @package Cadmium\System\Modules\Auth
5
 * @author Anton Romanov
6
 * @copyright Copyright (c) 2015-2017, Anton Romanov
7
 * @link http://cadmium-cms.com
8
 */
9
10
namespace Modules\Auth\Utils {
11
12
	use Modules\Auth, Utils\Messages, Utils\View, Language, Request, Template;
13
14
	abstract class Action {
15
16
		protected $code = null, $user = null, $form = null;
17
18
		# Action configuration interface
19
20
		protected static $view = '', $form_class = '', $controller_class = '';
21
22
		protected static $redirect = '', $messages = [];
23
24
		/**
25
		 * Get the contents block
26
		 */
27
28
		private function getContents() : Template\Block {
29
30
			$contents = View::get((Auth::isAdmin() ? 'Blocks/Auth/' : 'Blocks/Profile/Auth/') . static::$view);
31
32
			# Set code
33
34
			if (null !== $this->code) $contents->code = $this->code;
35
36
			# Implement form
37
38
			if (null !== $this->form) $this->form->implement($contents);
39
40
			# ------------------------
41
42
			return $contents;
43
		}
44
45
		/**
46
		 * Handle the request
47
		 */
48
49
		public function handle() : Template\Block {
50
51
			# Create form
52
53
			$this->form = new static::$form_class;
54
55
			# Handle form
56
57 View Code Duplication
			if ($this->form->handle(new static::$controller_class($this->user))) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
58
59
				Request::redirect(INSTALL_PATH . (Auth::isAdmin() ? '/admin' : '/profile') . static::$redirect);
60
			}
61
62
			# Display success message
63
64
			foreach (static::$messages as $key => $message) if (Request::get('submitted') === $key) {
65
66
				Messages::set('success', Language::get($message['text']), Language::get($message['title']));
67
			}
68
69
			# ------------------------
70
71
			return $this->getContents();
72
		}
73
	}
74
}
75