Passed
Push — developer ( 9791ed...d95459 )
by Mariusz
169:15 queued 134:18
created

Process   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 148
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 2
dl 0
loc 148
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 4 1
A addEvent() 0 14 5
A removeEvent() 0 7 2
A hasEvent() 0 4 1
A getEvents() 0 18 4
A writeSession() 0 4 1
1
<?php
2
/**
3
 * Process main class.
4
 *
5
 * @package App
6
 *
7
 * @copyright YetiForce Sp. z o.o
8
 * @license   YetiForce Public License 3.0 (licenses/LicenseEN.txt or yetiforce.com)
9
 * @author    Mariusz Krzaczkowski <[email protected]>
10
 * @author    Radosław Skrzypczak <[email protected]>
11
 */
12
13
namespace App;
14
15
/**
16
 * Process class.
17
 */
18
class Process
0 ignored issues
show
Coding Style introduced by
Process does not seem to conform to the naming convention (Utils?$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
19
{
20
	/**
21
	 * Request start time.
22
	 *
23
	 * @var int
24
	 */
25
	public static $startTime;
26
27
	/**
28
	 * Request mode.
29
	 *
30
	 * @var string
31
	 */
32
	public static $requestMode;
33
34
	/**
35
	 * CRM root directory.
36
	 *
37
	 * @var string
38
	 */
39
	public static $rootDirectory;
40
41
	/**
42
	 * Request process type.
43
	 *
44
	 * @var string
45
	 */
46
	public static $processType;
47
48
	/**
49
	 * Request process name.
50
	 *
51
	 * @var string
52
	 */
53
	public static $processName;
54
55
	/**
56
	 * List of events.
57
	 *
58
	 * @var array
59
	 */
60
	private static $events = [];
61
62
	/**
63
	 * Initialization of events.
64
	 *
65
	 * @return void
66
	 */
67
	public static function init(): void
68
	{
69
		self::$events = Session::get('processEvents', []);
0 ignored issues
show
Documentation Bug introduced by
It seems like \App\Session::get('processEvents', array()) of type * is incompatible with the declared type array of property $events.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
70
	}
71
72
	/**
73
	 * Add event.
74
	 *
75
	 * App\Process::addEvent([
76
	 *	'name' => 'notify test',
77
	 *	'execution' => 'once',
78
	 *	'type' => 'notify',
79
	 *	'notify' => [
80
	 *		'text' => 'test',
81
	 *		'type' => 'info' // alert, notice, info, success, error
82
	 *	]
83
	 * ]);
84
	 * alert, notice, info, success, error
85
	 *
86
	 * @param array $event
87
	 *
88
	 * @return void
89
	 */
90
	public static function addEvent(array $event): void
91
	{
92
		if (empty($event['name']) || empty($event['type'])) {
93
			throw new Exceptions\AppException('Incorrect data');
94
		}
95
		if (empty($event['priority'])) {
96
			$event['priority'] = 5;
97
		}
98
		if (empty($event['execution'])) {
99
			$event['execution'] = 'constant';
100
		}
101
		self::$events[$event['name']] = $event;
102
		self::writeSession();
103
	}
104
105
	/**
106
	 * Remove event.
107
	 *
108
	 * @param string $name
109
	 *
110
	 * @return void
111
	 */
112
	public static function removeEvent(string $name): void
113
	{
114
		if (isset(self::$events[$name])) {
115
			unset(self::$events[$name]);
116
			self::writeSession();
117
		}
118
	}
119
120
	/**
121
	 * Has event.
122
	 *
123
	 * @param string $name
124
	 *
125
	 * @return bool
126
	 */
127
	public static function hasEvent(string $name): bool
128
	{
129
		return isset(self::$events[$name]);
130
	}
131
132
	/**
133
	 * Get events.
134
	 *
135
	 * @return array
136
	 */
137
	public static function getEvents(): array
138
	{
139
		$events = [];
140
		$writeSession = false;
141
		foreach (self::$events as $name => $row) {
142
			$events[] = $row;
143
			if ('once' === $row['execution']) {
144
				unset(self::$events[$name]);
145
				$writeSession = true;
146
			}
147
		}
148
		if ($writeSession) {
149
			self::writeSession();
150
		}
151
		$priority = array_column($events, 'priority');
152
		array_multisort($priority, SORT_DESC, $events);
153
		return $events;
154
	}
155
156
	/**
157
	 * Write session data.
158
	 *
159
	 * @return void
160
	 */
161
	private static function writeSession(): void
162
	{
163
		Session::set('processEvents', self::$events);
164
	}
165
}
166