AclElement::castAs()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 2
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Ubiquity\security\acl\models;
4
5
use Ubiquity\attributes\items\Transient;
6
7
/**
8
 * Ubiquity\security\acl\models$AclElement
9
 * This class is part of Ubiquity
10
 *
11
 * @author jc
12
 * @version 1.0.2
13
 *
14
 */
15
16
class AclElement {
17
18
	/**
19
	 *
20
	 * @id
21
	 * @column("name"=>"id","nullable"=>false,"dbType"=>"int(11)")
22
	 */
23
	#[\Ubiquity\attributes\items\Id()]
24
	#[\Ubiquity\attributes\items\Column(name:'id',nullable:false,dbType:'int(11)')]
25
	protected $id;
26
27
	/**
28
	 *
29
	 * @var Role
30
	 * @manyToOne
31
	 * @joinColumn("className"=>"Ubiquity\\security\\acl\\models\\Role","name"=>"roleName","nullable"=>false)
32
	 */
33
	#[\Ubiquity\attributes\items\ManyToOne()]
34
	#[\Ubiquity\attributes\items\JoinColumn(className:"Ubiquity\\security\\acl\\models\\Role",name: "roleName", nullable: false)]
35
	protected $role;
36
37
	/**
38
	 *
39
	 * @var Permission
40
	 * @manyToOne
41
	 * @joinColumn("className"=>"Ubiquity\\security\\acl\\models\\Permission","name"=>"permissionName","nullable"=>false)
42
	 */
43
	#[\Ubiquity\attributes\items\ManyToOne()]
44
	#[\Ubiquity\attributes\items\JoinColumn(className:"Ubiquity\\security\\acl\\models\\Permission",name: "permissionName", nullable: false)]
45
	protected $permission;
46
47
	/**
48
	 *
49
	 * @var \Ubiquity\security\acl\models\Resource
50
	 * @manyToOne
51
	 * @joinColumn("className"=>"Ubiquity\\security\\acl\\models\\Resource","name"=>"resourceName","nullable"=>false)
52
	 */
53
	#[\Ubiquity\attributes\items\ManyToOne()]
54
	#[\Ubiquity\attributes\items\JoinColumn(className:"Ubiquity\\security\\acl\\models\\Resource",name: "resourceName", nullable: false)]
55
	protected $resource;
56
57
	/**
58
	 * @var string
59
	 * @transient
60
	 */
61
	#[Transient]
62
	protected $type='';
63
	
64
	/**
65
	 *
66
	 * @return Role
67
	 *
68
	 */
69 16
	public function getRole() {
70 16
		return $this->role;
71
	}
72
73
	/**
74
	 *
75
	 * @return Permission
76
	 */
77 15
	public function getPermission() {
78 15
		return $this->permission;
79
	}
80
81
	/**
82
	 *
83
	 * @return \Ubiquity\security\acl\models\Resource
84
	 */
85 16
	public function getResource() {
86 16
		return $this->resource;
87
	}
88
89 6
	public function fromArray($aclArray) {
90 6
		$role = new Role();
91 6
		$role->fromArray($aclArray['role']);
92 6
		$resource = new Resource();
93 6
		$resource->fromArray($aclArray['resource']);
94 6
		$permission = new Permission();
95 6
		$permission->fromArray($aclArray['permission']);
96 6
		$this->role = $role;
97 6
		$this->permission = $permission;
98 6
		$this->resource = $resource;
99
	}
100
101 4
	public function toArray(): array {
102 4
		return [
103 4
			'resource' => $this->resource->toArray(),
104 4
			'role' => $this->role->toArray(),
105 4
			'permission' => $this->permission->toArray()
106 4
		];
107
	}
108
109 13
	public function allow(Role $role, Resource $resource, Permission $permission) {
110 13
		$this->role = $role;
111 13
		$this->resource = $resource;
112 13
		$this->permission = $permission;
113
	}
114
115
	/**
116
	 *
117
	 * @return mixed
118
	 */
119 1
	public function getId() {
120 1
		return $this->id;
121
	}
122
123
	/**
124
	 *
125
	 * @param mixed $id
126
	 */
127 16
	public function setId($id) {
128 16
		$this->id = $id;
129
	}
130
131
	/**
132
	 *
133
	 * @param \Ubiquity\security\acl\models\Role $role
134
	 */
135 4
	public function setRole($role) {
136 4
		$this->role = $role;
137
	}
138
139
	/**
140
	 *
141
	 * @param \Ubiquity\security\acl\models\Permission $permission
142
	 */
143 4
	public function setPermission($permission) {
144 4
		$this->permission = $permission;
145
	}
146
147
	/**
148
	 *
149
	 * @param \Ubiquity\security\acl\models\Resource $resource
150
	 */
151 4
	public function setResource($resource) {
152 4
		$this->resource = $resource;
153
	}
154
155
	/**
156
	 * @return string
157
	 */
158
	public function getType(): string {
159
		return $this->type;
160
	}
161
162
	/**
163
	 * @param string $type
164
	 */
165 4
	public function setType(?string $type): void {
166 4
		$this->type = $type;
167
	}
168
169 6
	public function getId_() {
170 6
		$id = '';
171 6
		if (isset($this->role)) {
172 6
			$id = $this->role->getName();
173
		}
174 6
		if (isset($this->resource)) {
175 6
			$id .= $this->resource->getName();
176
		}
177 6
		if (isset($this->permission)) {
178 6
			$id .= $this->permission->getName();
179
		}
180 6
		return \crc32($id) . '.';
0 ignored issues
show
Bug introduced by
It seems like $id can also be of type null; however, parameter $string of crc32() does only seem to accept string, 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

180
		return \crc32(/** @scrutinizer ignore-type */ $id) . '.';
Loading history...
181
	}
182
183 1
	public function castAs(string $class) {
184 1
		return unserialize(sprintf('O:%d:"%s"%s', \strlen($class), $class, \strstr(\strstr(\serialize($this), '"'), ':')));
185
	}
186
}
187
188