Process::hasEvent()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
/**
3
 * Process main class.
4
 *
5
 * @package App
6
 *
7
 * @copyright YetiForce S.A.
8
 * @license   YetiForce Public License 6.5 (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
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() can also be of type string. However, the property $events is declared as type array. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
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);
0 ignored issues
show
Bug introduced by
App\SORT_DESC cannot be passed to array_multisort() as the parameter $rest expects a reference. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

152
		array_multisort($priority, /** @scrutinizer ignore-type */ SORT_DESC, $events);
Loading history...
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