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

TForkable::attachEventHandlers()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 8
nc 5
nop 1
dl 0
loc 13
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * TForkable 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\TComponent;
13
use Prado\Util\Helpers\TProcessHelper;
14
15
/**
16
 * TForkable class.
17
 *
18
 * This attaches the Owner Component `fxPrepareForFork` and `fxRestoreAfterFork`
19
 * methods as the event handlers for the PRADO global event `fxPrepareForFork` and
20
 * `fxRestoreAfterFork`, respectively.
21
 *
22
 * This should only be used when the TComponent is not {@see \Prado\TComponent::listen()}ing
23
 * or the handlers will be double added.
24
 *
25
 * @author Brad Anderson <[email protected]>
26
 * @since 4.2.3
27
 */
28
class TForkable extends \Prado\Util\TBehavior
29
{
30
	private int $_methods = 0;
31
32
	/**
33
	 * Attaches the component event handlers:
34
	 * {@see \Prado\TComponent::fxAttachClassBehavior()} and {@see \Prado\TComponent::fxDetachClassBehavior()}
35
	 * @param TComponent $component
36
	 * @return bool Attaching handlers.
37
	 */
38
	protected function attachEventHandlers(TComponent $component): bool
39
	{
40
		if ($return = parent::attachEventHandlers($component)) {
41
			if ($component->hasMethod(TProcessHelper::FX_PREPARE_FOR_FORK)) {
42
				$this->_methods |= 1;
43
				$component->attachEventHandler(TProcessHelper::FX_PREPARE_FOR_FORK, [$component, TProcessHelper::FX_PREPARE_FOR_FORK], $this->getPriority());
0 ignored issues
show
Bug introduced by
It seems like $this->getPriority() can also be of type double; however, parameter $priority of Prado\TComponent::attachEventHandler() does only seem to accept Prado\numeric|null, maybe add an additional type check? ( Ignorable by Annotation )

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

43
				$component->attachEventHandler(TProcessHelper::FX_PREPARE_FOR_FORK, [$component, TProcessHelper::FX_PREPARE_FOR_FORK], /** @scrutinizer ignore-type */ $this->getPriority());
Loading history...
44
			}
45
			if ($component->hasMethod(TProcessHelper::FX_RESTORE_AFTER_FORK)) {
46
				$this->_methods |= 2;
47
				$component->attachEventHandler(TProcessHelper::FX_RESTORE_AFTER_FORK, [$component, TProcessHelper::FX_RESTORE_AFTER_FORK], $this->getPriority());
48
			}
49
		}
50
		return $return;
51
	}
52
53
	protected function detachEventHandlers(TComponent $component): bool
54
	{
55
		if ($return = parent::detachEventHandlers($component)) {
56
			if ($this->_methods & 1) {
57
				$component->detachEventHandler(TProcessHelper::FX_PREPARE_FOR_FORK, [$component, TProcessHelper::FX_PREPARE_FOR_FORK]);
58
			}
59
60
			if ($this->_methods & 2) {
61
				$component->detachEventHandler(TProcessHelper::FX_RESTORE_AFTER_FORK, [$component, TProcessHelper::FX_RESTORE_AFTER_FORK]);
62
			}
63
			$this->_methods = 0;
64
		}
65
		return $return;
66
	}
67
}
68