Passed
Push — master ( f5ec54...94ecb9 )
by Fabio
05:41
created

TPermissionsManagerPropertyTrait::__wakeup()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 4
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 6
rs 10
1
<?php
2
/**
3
 * TPermissionsManagerPropertyTrait 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\Security\Permissions;
11
12
use WeakReference;
13
14
/**
15
 * TPermissionsManagerPropertyTrait class.
16
 *
17
 * These are the methods for having the TPermissionsManager as a property in the
18
 * the behaviors {@link TPermissionsBehavior}, {@link TPermissionsConfigurationBehavior},
19
 * and {@link TUserPermissionsBehavior}.
20
 *
21
 * The Permissions Manager property is important to zap when sleeping.  On waking
22
 * up, the PermissionsManager is set to the current instance manager.
23
 *
24
 * This functionality is important to replicate if-when overriding.
25
 *
26
 * @author Brad Anderson <[email protected]>
27
 * @since 4.2.3
28
 */
29
trait TPermissionsManagerPropertyTrait
30
{
31
	/** @var TPermissionsManager manager object for the behavior */
32
	private $_permissionsManager;
33
34
	/**
35
	 * @param ?TPermissionsManager $manager
36
	 */
37
	public function __construct($manager = null)
38
	{
39
		if ($manager) {
40
			$this->setPermissionsManager($manager);
41
		}
42
		parent::__construct();
43
	}
44
45
	/**
46
	 * Sets the TPermissionsManager to the current singleton instance.
47
	 */
48
	public function __wakeup()
49
	{
50
		$this->setPermissionsManager(TPermissionsManager::getManager());
51
		parent::__wakeup();
52
		if(!$this->getPermissionsManager() && ($owner = $this->getOwner())) {
0 ignored issues
show
Bug introduced by
It seems like getOwner() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

52
		if(!$this->getPermissionsManager() && ($owner = $this->/** @scrutinizer ignore-call */ getOwner())) {
Loading history...
53
			$owner->detachBehavior($this->getName());
0 ignored issues
show
Bug introduced by
It seems like getName() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

53
			$owner->detachBehavior($this->/** @scrutinizer ignore-call */ getName());
Loading history...
54
		}
55
	}
56
57
	/**
58
	 * @return \Prado\Security\Permissions\TPermissionsManager application permissions manager
59
	 */
60
	public function getPermissionsManager()
61
	{
62
		return $this->_permissionsManager;
63
	}
64
65
	/**
66
	 * @param null|TPermissionsManager|WeakReference $manager manages application permissions
67
	 */
68
	public function setPermissionsManager($manager)
69
	{
70
		if ($manager instanceof WeakReference) {
71
			$manager = $manager->get();
72
		}
73
		$this->_permissionsManager = $manager;
74
	}
75
76
	/**
77
	 * Returns an array with the names of all variables of this object that should NOT be serialized
78
	 * because their value is the default one or useless to be cached for the next page loads.
79
	 * Reimplement in derived classes to add new variables, but remember to  also to call the parent
80
	 * implementation first.
81
	 * @param array $exprops by reference
82
	 */
83
	protected function _getZappableSleepProps(&$exprops)
84
	{
85
		parent::_getZappableSleepProps($exprops);
86
		$exprops[] = "\0" . __CLASS__ . "\0_permissionsManager";
87
	}
88
}
89