Total Complexity | 41 |
Total Lines | 247 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like UserGroupClass often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use UserGroupClass, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
33 | class UserGroupClass implements \JsonSerializable { |
||
34 | public const TYPE = 'generic'; |
||
35 | public const TYPE_PUBLIC = 'public'; |
||
36 | public const TYPE_EXTERNAL = 'external'; |
||
37 | public const TYPE_CIRCLE = Circle::TYPE; |
||
38 | public const TYPE_CONTACT = Contact::TYPE; |
||
39 | public const TYPE_CONTACTGROUP = ContactGroup::TYPE; |
||
40 | public const TYPE_EMAIL = Email::TYPE; |
||
41 | public const TYPE_GROUP = Group::TYPE; |
||
42 | public const TYPE_USER = User::TYPE; |
||
43 | |||
44 | /** @var IL10N */ |
||
45 | private $l10n; |
||
46 | |||
47 | /** @var string */ |
||
48 | protected $id; |
||
49 | |||
50 | /** @var string */ |
||
51 | protected $type; |
||
52 | |||
53 | /** @var string */ |
||
54 | protected $displayName = ''; |
||
55 | |||
56 | /** @var string */ |
||
57 | protected $description = ''; |
||
58 | |||
59 | /** @var string */ |
||
60 | protected $emailAddress = ''; |
||
61 | |||
62 | /** @var string */ |
||
63 | protected $language = ''; |
||
64 | |||
65 | /** @var string */ |
||
66 | protected $organisation = ''; |
||
67 | |||
68 | /** @var string */ |
||
69 | protected $icon = ''; |
||
70 | |||
71 | /** @var bool */ |
||
72 | protected $isNoUser = true; |
||
73 | |||
74 | /** @var string[] */ |
||
75 | protected $categories = []; |
||
76 | |||
77 | public function __construct( |
||
78 | string $id, |
||
79 | string $type, |
||
80 | string $displayName = '', |
||
81 | string $emailAddress = '', |
||
82 | string $language = '' |
||
83 | ) { |
||
84 | $this->id = $id; |
||
85 | $this->type = $type; |
||
86 | $this->displayName = $displayName; |
||
87 | $this->emailAddress = $emailAddress; |
||
88 | $this->language = $language; |
||
89 | $this->icon = 'icon-share'; |
||
90 | $this->l10n = \OC::$server->getL10N('polls'); |
||
91 | } |
||
92 | |||
93 | public function getId(): string { |
||
94 | return $this->id; |
||
95 | } |
||
96 | |||
97 | /** |
||
98 | * @return string |
||
99 | */ |
||
100 | public function getPublicId(): string { |
||
101 | return $this->id; |
||
102 | } |
||
103 | |||
104 | public function getUser(): string { |
||
105 | return $this->id; |
||
106 | } |
||
107 | |||
108 | public function getType(): string { |
||
109 | return $this->type; |
||
110 | } |
||
111 | |||
112 | public function getLanguage(): string { |
||
113 | return $this->language; |
||
114 | } |
||
115 | |||
116 | public function getDisplayName(): string { |
||
117 | return $this->displayName; |
||
118 | } |
||
119 | |||
120 | public function getDescription(): string { |
||
121 | return $this->description; |
||
122 | } |
||
123 | |||
124 | public function getIcon(): string { |
||
125 | return $this->icon; |
||
126 | } |
||
127 | |||
128 | public function getEmailAddress(): string { |
||
129 | return $this->emailAddress; |
||
130 | } |
||
131 | |||
132 | public function getOrganisation(): string { |
||
133 | return $this->organisation; |
||
134 | } |
||
135 | |||
136 | /** |
||
137 | * @return string[] |
||
138 | * |
||
139 | * @psalm-return array<array-key, string> |
||
140 | */ |
||
141 | public function getCategories(): array { |
||
142 | return $this->categories; |
||
143 | } |
||
144 | |||
145 | public function getIsNoUser(): bool { |
||
147 | } |
||
148 | |||
149 | public function setType(string $type): string { |
||
150 | $this->type = $type; |
||
151 | return $this->type; |
||
152 | } |
||
153 | |||
154 | public function setDisplayName(string $displayName): string { |
||
155 | $this->displayName = $displayName; |
||
156 | return $this->displayName; |
||
157 | } |
||
158 | |||
159 | public function setDescription(string $description): string { |
||
160 | $this->description = $description; |
||
161 | return $this->description; |
||
162 | } |
||
163 | |||
164 | public function setEmailAddress(string $emailAddress) : string { |
||
165 | $this->emailAddress = $emailAddress; |
||
166 | return $this->emailAddress; |
||
167 | } |
||
168 | |||
169 | public function setLanguage(string $language): string { |
||
170 | $this->language = $language; |
||
171 | return $this->language; |
||
172 | } |
||
173 | |||
174 | public function setOrganisation(string $organisation): string { |
||
175 | $this->organisation = $organisation; |
||
176 | return $this->organisation; |
||
177 | } |
||
178 | |||
179 | /** |
||
180 | * search all possible sharees - use ISearch to respect autocomplete restrictions |
||
181 | */ |
||
182 | public static function search(string $query = ''): array { |
||
183 | $items = []; |
||
184 | $types = [ |
||
185 | IShare::TYPE_USER, |
||
186 | IShare::TYPE_GROUP |
||
187 | ]; |
||
188 | if (Circle::isEnabled() && class_exists('\OCA\Circles\ShareByCircleProvider')) { |
||
189 | $types[] = IShare::TYPE_CIRCLE; |
||
190 | } |
||
191 | |||
192 | [$result, $more] = self::getContainer()->query(ISearch::class)->search($query, $types, false, 200, 0); |
||
193 | |||
194 | foreach ($result['users'] as $item) { |
||
195 | $items[] = new User($item['value']['shareWith']); |
||
196 | } |
||
197 | |||
198 | foreach ($result['exact']['users'] as $item) { |
||
199 | $items[] = new User($item['value']['shareWith']); |
||
200 | } |
||
201 | |||
202 | foreach ($result['groups'] as $item) { |
||
203 | $items[] = new Group($item['value']['shareWith']); |
||
204 | } |
||
205 | |||
206 | foreach ($result['exact']['groups'] as $item) { |
||
207 | $items[] = new Group($item['value']['shareWith']); |
||
208 | } |
||
209 | |||
210 | $items = array_merge($items, Contact::search($query)); |
||
211 | $items = array_merge($items, ContactGroup::search($query)); |
||
212 | |||
213 | if (Circle::isEnabled()) { |
||
214 | foreach ($result['circles'] as $item) { |
||
215 | $items[] = new Circle($item['value']['shareWith']); |
||
216 | } |
||
217 | foreach ($result['exact']['circles'] as $item) { |
||
218 | $items[] = new Circle($item['value']['shareWith']); |
||
219 | } |
||
220 | } |
||
221 | |||
222 | |||
223 | return $items; |
||
224 | } |
||
225 | |||
226 | /** |
||
227 | * @return array |
||
228 | */ |
||
229 | public function getMembers(): array { |
||
230 | return []; |
||
231 | } |
||
232 | |||
233 | protected static function getContainer() { |
||
234 | $app = \OC::$server->query(Application::class); |
||
235 | |||
236 | return $app->getContainer(); |
||
237 | } |
||
238 | |||
239 | /** |
||
240 | * @return Circle|Contact|ContactGroup|Email|GenericUser|Group|User |
||
241 | */ |
||
242 | public static function getUserGroupChild(string $type, string $id, string $displayName = '', string $emailAddress = '') { |
||
243 | switch ($type) { |
||
244 | case Group::TYPE: |
||
245 | return new Group($id); |
||
246 | case Circle::TYPE: |
||
247 | return new Circle($id); |
||
248 | case Contact::TYPE: |
||
249 | return new Contact($id); |
||
250 | case ContactGroup::TYPE: |
||
251 | return new ContactGroup($id); |
||
252 | case User::TYPE: |
||
253 | return new User($id); |
||
254 | case Email::TYPE: |
||
255 | return new Email($id); |
||
256 | case self::TYPE_PUBLIC: |
||
257 | return new GenericUser($id, self::TYPE_PUBLIC); |
||
258 | case self::TYPE_EXTERNAL: |
||
259 | return new GenericUser($id, self::TYPE_EXTERNAL, $displayName, $emailAddress); |
||
260 | default: |
||
261 | throw new InvalidShareTypeException('Invalid share type (' . $type . ')'); |
||
262 | } |
||
263 | } |
||
264 | |||
265 | public function jsonSerialize(): array { |
||
280 | ]; |
||
281 | } |
||
282 | } |
||
283 |