Passed
Push — master ( 032384...6ce998 )
by Fabio
05:25
created

TApplicationSignals::detachEventHandlers()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 5
nc 3
nop 1
dl 0
loc 9
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * TApplicationSignals class file.
4
 *
5
 * @author Brad Anderson <[email protected]>
6
 * @link https://github.com/pradosoft/prado
7
 * @license https://github.com/pradosoft/prado/blob/master/LICENSE
8
 */
9
10
namespace Prado\Util\Behaviors;
11
12
use Prado\Exceptions\TInvalidDataValueException;
13
use Prado\Exceptions\TInvalidOperationException;
14
use Prado\TComponent;
15
use Prado\TPropertyValue;
16
use Prado\Util\TBehavior;
17
use Prado\Util\TSignalsDispatcher;
0 ignored issues
show
Bug introduced by
The type Prado\Util\TSignalsDispatcher was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
18
19
/**
20
 * TApplicationSignals class.
21
 *
22
 * This behavior installs the {@see \Prado\Util\TSignalsDispatcher} (or subclass) for
23
 * the application when PHP pcntl_* is available.  The signals dispatcher class can
24
 * be specified with {@see self::setSignalsClass()} and is installed when the TApplicationSignals
25
 * behavior is attached to the TApplication owner.
26
 *
27
 * There is a TSignalsDispatcher getter {@see self::getSignalsDispatcher} added to
28
 * the owner (TApplication) for retrieving the dispatcher.
29
 *
30
 * There are two properties of TApplicationSignals for TSignalsDispatcher. {@see
31
 * self::setAsyncSignals} changes how signals are handled.  When synchronous,
32
 * {@see \Prado\Util\TSignalsDispatcher::syncDispatch()} must be called for signals
33
 * to be processed.  When asynchronous, the signals will be handled by atomic interrupt.
34
 *
35
 * ```xml
36
 *		<behavior name="appSignals" AttachToClass="Prado\TApplication" class="Prado\Util\Behaviors\TApplicationSignals" PriorHandlerPriority="5" />
37
 * ```
38
 *
39
 * @author Brad Anderson <[email protected]>
40
 * @since 4.2.3
41
 */
42
class TApplicationSignals extends TBehavior
43
{
44
	/** @var string the signals class. */
45
	protected ?string $_signalsClass = null;
46
47
	/**
48
	 * Attaches the TSignalsDispatcher to handle the process signals.
49
	 * @param TComponent $component The owner.
50
	 * @return bool Should the behavior's event handlers be attached.
51
	 */
52
	protected function attachEventHandlers(TComponent $component): bool
53
	{
54
		if ($return = parent::attachEventHandlers($component)) {
55
			($this->getSignalsClass())::singleton();
56
		}
57
		return $return;
58
	}
59
60
	/**
61
	 * Detaches the TSignalsDispatcher from handling the process signals.
62
	 * @param TComponent $component The owner.
63
	 * @return bool Should the behavior's event handlers be detached.
64
	 */
65
	protected function detachEventHandlers(TComponent $component): bool
66
	{
67
		if ($return = parent::detachEventHandlers($component)) {
68
			if ($dispatcher = ($this->getSignalsClass())::singleton(false)) {
69
				$dispatcher->detach();
70
				unset($dispatcher);
71
			}
72
		}
73
		return $return;
74
	}
75
76
	/**
77
	 * @return ?object The Signal Dispatcher.
78
	 */
79
	public function getSignalsDispatcher(): ?object
80
	{
81
		return ($this->getSignalsClass())::singleton(false);
82
	}
83
84
	/**
85
	 * @return ?string The class of the Signals Dispatcher.
86
	 */
87
	public function getSignalsClass(): ?string
88
	{
89
		if ($this->_signalsClass === null) {
90
			$this->_signalsClass = TSignalsDispatcher::class;
91
		}
92
93
		return $this->_signalsClass;
94
	}
95
96
	/**
97
	 * @param ?string $value The class of the Signals Dispatcher.
98
	 * @throws TInvalidOperationException When already attached, this cannot be changed.
99
	 * @throws TInvalidDataValueException When the class is not a TSignalsDispatcher.
100
	 * @return static The current object.
101
	 */
102
	public function setSignalsClass($value): static
103
	{
104
		if ($this->getOwner()) {
105
			throw new TInvalidOperationException('appsignals_no_change', 'SignalsClass');
106
		}
107
		if ($value === '') {
108
			$value = null;
109
		}
110
111
		if ($value !== null) {
112
			$value = TPropertyValue::ensureString($value);
113
			if (!is_a($value, TSignalsDispatcher::class, true)) {
114
				throw new TInvalidDataValueException('appsignals_not_a_dispatcher', $value);
115
			}
116
		}
117
118
		$this->_signalsClass = $value;
119
120
		return $this;
121
	}
122
123
	/**
124
	 * @return bool Is the system executing signal handlers asynchronously.
125
	 */
126
	public function getAsyncSignals(): bool
127
	{
128
		return ($this->getSignalsClass())::getAsyncSignals();
129
	}
130
131
	/**
132
	 * @param mixed $value Set the system to execute signal handlers asynchronously (or synchronously on false).
133
	 * @return bool Was the system executing signal handlers asynchronously.
134
	 */
135
	public function setAsyncSignals($value): bool
136
	{
137
		return ($this->getSignalsClass())::setAsyncSignals(TPropertyValue::ensureBoolean($value));
138
	}
139
140
	/**
141
	 * When the original signal handlers are placed into the Signals Events this is the
142
	 * priority of original signal handlers.
143
	 * @return ?float The priority of the signal handlers that were installed before
144
	 *   the TSignalsDispatcher attaches.
145
	 */
146
	public function getPriorHandlerPriority(): ?float
147
	{
148
		return ($this->getSignalsClass())::getPriorHandlerPriority();
149
	}
150
151
	/**
152
	 * @param null|float|string $value The priority of the signal handlers that were installed before
153
	 *   the TSignalsDispatcher attaches.
154
	 * @return bool Is the Prior Handler Priority changed.
155
	 */
156
	public function setPriorHandlerPriority($value): bool
157
	{
158
		if ($value === '') {
159
			$value = null;
160
		}
161
		if ($value !== null) {
162
			$value = TPropertyValue::ensureFloat($value);
163
		}
164
		return ($this->getSignalsClass())::setPriorHandlerPriority($value);
165
	}
166
}
167