PluginProbe
User Access Manager / 2.2.21
User Access Manager v2.2.21
2.3.20 2.3.19 2.3.18 2.3.17 2.3.16 2.3.15 2.3.14 2.3.13 trunk 0.6 0.6.1 0.6.2 0.7 0.7 Beta 0.7.0.1 0.8 0.8.0.1 0.8.0.2 0.9 0.9.1 0.9.1.1 0.9.1.2 0.9.1.3 0.9.1.4 1.0 All 136 releases
← All changes | src/ObjectMembership/ObjectMembershipHandler.php +98 -12 trunk2.2.21 View file →
@@ -1,5 +1,18 @@
1 1 <?php
2 +/**
3 + * MembershipHandler.php
4 + *
5 + * The MembershipHandler class file.
6 + *
7 + * PHP versions 5
8 + *
9 + * @author Alexander Schneider <alexanderschneider85@gmail.com>
10 + * @copyright 2008-2017 Alexander Schneider
11 + * @license http://www.gnu.org/licenses/gpl-2.0.html GNU General Public License, version 2
12 + * @version SVN: $id$
13 + * @link http://wordpress.org/extend/plugins/user-access-manager/
14 + */
2 15
3 16 declare(strict_types=1);
4 17
5 18 namespace UserAccessManager\ObjectMembership;
@@ -4,49 +17,90 @@
4 17
5 18 namespace UserAccessManager\ObjectMembership;
6 19
7 20 use Exception;
8 -use UserAccessManager\ObjectMembership\Exception\MissingObjectTypeException;
9 21 use UserAccessManager\UserGroup\AbstractUserGroup;
10 22 use UserAccessManager\UserGroup\AssignmentInformation;
11 23 use UserAccessManager\UserGroup\AssignmentInformationFactory;
12 24
25 +/**
26 + * Class MembershipHandler
27 + *
28 + * @package UserAccessManager\UserGroup
29 + */
13 30 abstract class ObjectMembershipHandler
14 31 {
15 - protected ?string $generalObjectType = null;
32 + /**
33 + * @var AssignmentInformationFactory
34 + */
35 + protected $assignmentInformationFactory;
16 36
17 37 /**
18 - * @throws Exception
38 + * @var string
19 39 */
40 + protected $generalObjectType;
41 +
42 + /**
43 + * MembershipHandler constructor.
44 + * @param AssignmentInformationFactory $assignmentInformationFactory
45 + * @throws Exception
46 + */
20 47 public function __construct(
21 - protected AssignmentInformationFactory $assignmentInformationFactory
48 + AssignmentInformationFactory $assignmentInformationFactory
22 49 ) {
23 50 if ($this->generalObjectType === null) {
24 51 throw new MissingObjectTypeException('Missing general object type, Object type must be set.');
25 52 }
53 +
54 + $this->assignmentInformationFactory = $assignmentInformationFactory;
26 55 }
27 56
28 - abstract public function getObjectName(int|string|null $objectId, string &$typeName = ''): int|string;
57 + /**
58 + * Returns the object name.
59 + * @param int|string $objectId
60 + * @param string $typeName
61 + * @return int|string
62 + */
63 + abstract public function getObjectName($objectId, string &$typeName = '');
29 64
65 + /**
66 + * Returns the general object type.
67 + * @return string
68 + */
30 69 public function getGeneralObjectType(): string
31 70 {
32 71 return $this->generalObjectType;
33 72 }
34 73
74 + /**
75 + * Returns the handled objects.
76 + * @return array
77 + */
35 78 public function getHandledObjects(): array
36 79 {
37 80 return [$this->generalObjectType => $this->generalObjectType];
38 81 }
39 82
40 - public function handlesObject(mixed $objectType): bool
83 + /**
84 + * Returns if the object is handled by this handler.
85 + * @param $objectType
86 + * @return bool
87 + */
88 + public function handlesObject($objectType): bool
41 89 {
42 - return isset($this->getHandledObjects()[$objectType]);
90 + $objectTypes = $this->getHandledObjects();
91 + return isset($objectTypes[$objectType]);
43 92 }
44 93
94 + /**
95 + * Assigns recursive membership to the assignment information object.
96 + * @param null|AssignmentInformation $assignmentInformation
97 + * @param array $recursiveMembership
98 + */
45 99 protected function assignRecursiveMembership(
46 100 ?AssignmentInformation &$assignmentInformation,
47 101 array $recursiveMembership
48 - ): void {
102 + ) {
49 103 if ($assignmentInformation === null) {
50 104 $assignmentInformation = $this->assignmentInformationFactory->createAssignmentInformation();
51 105 }
52 106
@@ -52,8 +106,15 @@
52 106
53 107 $assignmentInformation->setRecursiveMembership($recursiveMembership);
54 108 }
55 109
110 + /**
111 + * Checks for the access and assigns the recursive membership array if access.
112 + * @param bool $isMember
113 + * @param array $recursiveMembership
114 + * @param null|AssignmentInformation $assignmentInformation
115 + * @return bool
116 + */
56 117 protected function checkAccessWithRecursiveMembership(
57 118 bool $isMember,
58 119 array $recursiveMembership,
59 120 ?AssignmentInformation &$assignmentInformation
@@ -65,25 +126,50 @@
65 126
66 127 return false;
67 128 }
68 129
130 + /**
131 + * Returns the assigned objects as array map.
132 + * @param AbstractUserGroup $userGroup
133 + * @param string $objectType
134 + * @return array
135 + */
69 136 protected function getSimpleAssignedObjects(AbstractUserGroup $userGroup, string $objectType): array
70 137 {
138 + $objects = $userGroup->getAssignedObjects($objectType);
139 +
71 140 return array_map(
72 - fn(AssignmentInformation $assignmentInformation) => $assignmentInformation->getType(),
73 - $userGroup->getAssignedObjects($objectType)
141 + function (AssignmentInformation $element) {
142 + return $element->getType();
143 + },
144 + $objects
74 145 );
75 146 }
76 147
148 + /**
149 + * Checks if the object is a member of the user group.
150 + * @param AbstractUserGroup $userGroup
151 + * @param bool $lockRecursive
152 + * @param int|string $objectId
153 + * @param null|AssignmentInformation $assignmentInformation
154 + * @return bool
155 + */
77 156 abstract public function isMember(
78 157 AbstractUserGroup $userGroup,
79 158 bool $lockRecursive,
80 - int|string|null $objectId,
159 + $objectId,
81 160 ?AssignmentInformation &$assignmentInformation = null
82 161 ): bool;
83 162
163 + /**
164 + * Returns the full objects.
165 + * @param AbstractUserGroup $userGroup
166 + * @param bool $lockRecursive
167 + * @param null $objectType
168 + * @return array
169 + */
84 170 abstract public function getFullObjects(
85 171 AbstractUserGroup $userGroup,
86 172 bool $lockRecursive,
87 - ?string $objectType = null
173 + $objectType = null
88 174 ): array;
89 175 }