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

Action   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 60
Duplicated Lines 6.67 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
dl 4
loc 60
rs 10
c 0
b 0
f 0
wmc 9
lcom 1
cbo 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getContents() 0 16 4
B handle() 4 24 5

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
/**
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