| Total Complexity | 46 |
| Total Lines | 341 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like CGroup 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 CGroup, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 28 | #[ApiResource( |
||
| 29 | shortName: 'Group', |
||
| 30 | operations: [ |
||
| 31 | new GetCollection( |
||
| 32 | uriTemplate: '/groups', |
||
| 33 | openapi: new Operation( |
||
| 34 | parameters: [ |
||
| 35 | new Parameter( |
||
| 36 | name: 'resourceNode.parent', |
||
| 37 | in: 'query', |
||
| 38 | required: true, |
||
| 39 | description: 'Filter groups by the parent resource node (course)', |
||
| 40 | schema: ['type' => 'integer'], |
||
| 41 | ), |
||
| 42 | ], |
||
| 43 | ) |
||
| 44 | ), |
||
| 45 | new Get(security: "is_granted('VIEW', object.resourceNode)"), |
||
| 46 | ], |
||
| 47 | normalizationContext: ['groups' => ['group:read']], |
||
| 48 | denormalizationContext: ['groups' => ['group:write']], |
||
| 49 | paginationEnabled: true |
||
| 50 | )] |
||
| 51 | #[ApiFilter(SearchFilter::class, properties: ['resourceNode.parent' => 'exact'])] |
||
| 52 | #[ORM\Table(name: 'c_group_info')] |
||
| 53 | #[ORM\Entity(repositoryClass: CGroupRepository::class)] |
||
| 54 | class CGroup extends AbstractResource implements ResourceInterface, Stringable |
||
| 55 | { |
||
| 56 | public const TOOL_NOT_AVAILABLE = 0; |
||
| 57 | public const TOOL_PUBLIC = 1; |
||
| 58 | public const TOOL_PRIVATE = 2; |
||
| 59 | public const TOOL_PRIVATE_BETWEEN_USERS = 3; |
||
| 60 | #[ORM\Column(name: 'iid', type: 'integer')] |
||
| 61 | #[ORM\Id] |
||
| 62 | #[ORM\GeneratedValue] |
||
| 63 | #[Groups(['group:read', 'group:write'])] |
||
| 64 | protected ?int $iid = null; |
||
| 65 | #[Assert\NotBlank] |
||
| 66 | #[ORM\Column(name: 'title', type: 'string', length: 100, nullable: false)] |
||
| 67 | #[Groups(['group:read', 'group:write'])] |
||
| 68 | protected string $title; |
||
| 69 | #[Assert\NotNull] |
||
| 70 | #[ORM\Column(name: 'status', type: 'boolean', nullable: false)] |
||
| 71 | #[Groups(['group:read', 'group:write'])] |
||
| 72 | protected bool $status; |
||
| 73 | #[ORM\ManyToOne(targetEntity: CGroupCategory::class, cascade: ['persist'])] |
||
| 74 | #[ORM\JoinColumn(name: 'category_id', referencedColumnName: 'iid', onDelete: 'CASCADE')] |
||
| 75 | protected ?CGroupCategory $category = null; |
||
| 76 | #[ORM\Column(name: 'description', type: 'text', nullable: true)] |
||
| 77 | #[Groups(['group:read', 'group:write'])] |
||
| 78 | protected ?string $description = null; |
||
| 79 | #[Assert\NotBlank] |
||
| 80 | #[ORM\Column(name: 'max_student', type: 'integer')] |
||
| 81 | protected int $maxStudent; |
||
| 82 | #[ORM\Column(name: 'doc_state', type: 'integer')] |
||
| 83 | protected int $docState; |
||
| 84 | #[ORM\Column(name: 'calendar_state', type: 'integer')] |
||
| 85 | protected int $calendarState; |
||
| 86 | #[ORM\Column(name: 'work_state', type: 'integer')] |
||
| 87 | protected int $workState; |
||
| 88 | #[ORM\Column(name: 'announcements_state', type: 'integer')] |
||
| 89 | protected int $announcementsState; |
||
| 90 | #[ORM\Column(name: 'forum_state', type: 'integer')] |
||
| 91 | protected int $forumState; |
||
| 92 | #[ORM\Column(name: 'wiki_state', type: 'integer')] |
||
| 93 | protected int $wikiState; |
||
| 94 | #[ORM\Column(name: 'chat_state', type: 'integer')] |
||
| 95 | protected int $chatState; |
||
| 96 | #[ORM\Column(name: 'self_registration_allowed', type: 'boolean')] |
||
| 97 | protected bool $selfRegistrationAllowed; |
||
| 98 | #[ORM\Column(name: 'self_unregistration_allowed', type: 'boolean')] |
||
| 99 | protected bool $selfUnregistrationAllowed; |
||
| 100 | #[ORM\Column(name: 'document_access', type: 'integer', options: ['default' => 0])] |
||
| 101 | protected int $documentAccess; |
||
| 102 | |||
| 103 | /** |
||
| 104 | * @var Collection<int, CGroupRelUser> |
||
| 105 | */ |
||
| 106 | #[ORM\OneToMany(mappedBy: 'group', targetEntity: CGroupRelUser::class)] |
||
| 107 | protected Collection $members; |
||
| 108 | |||
| 109 | /** |
||
| 110 | * @var Collection<int, CGroupRelTutor> |
||
| 111 | */ |
||
| 112 | #[ORM\OneToMany(mappedBy: 'group', targetEntity: CGroupRelTutor::class)] |
||
| 113 | protected Collection $tutors; |
||
| 114 | public function __construct() |
||
| 115 | { |
||
| 116 | $this->status = true; |
||
| 117 | $this->members = new ArrayCollection(); |
||
| 118 | $this->tutors = new ArrayCollection(); |
||
| 119 | // Default values |
||
| 120 | $defaultVisibility = self::TOOL_PRIVATE; |
||
| 121 | $this->docState = $defaultVisibility; |
||
| 122 | $this->calendarState = $defaultVisibility; |
||
| 123 | $this->workState = $defaultVisibility; |
||
| 124 | $this->announcementsState = $defaultVisibility; |
||
| 125 | $this->forumState = $defaultVisibility; |
||
| 126 | $this->wikiState = $defaultVisibility; |
||
| 127 | $this->chatState = $defaultVisibility; |
||
| 128 | $this->documentAccess = $defaultVisibility; |
||
| 129 | $this->selfRegistrationAllowed = false; |
||
| 130 | $this->selfUnregistrationAllowed = false; |
||
| 131 | } |
||
| 132 | public function __toString(): string |
||
| 135 | } |
||
| 136 | |||
| 137 | public function getIid(): ?int |
||
| 138 | { |
||
| 139 | return $this->iid; |
||
| 140 | } |
||
| 141 | |||
| 142 | public function setTitle(string $title): self |
||
| 143 | { |
||
| 144 | $this->title = $title; |
||
| 145 | |||
| 146 | return $this; |
||
| 147 | } |
||
| 148 | |||
| 149 | public function getTitle(): string |
||
| 150 | { |
||
| 151 | return $this->title; |
||
| 152 | } |
||
| 153 | public function setStatus(bool $status): self |
||
| 154 | { |
||
| 155 | $this->status = $status; |
||
| 156 | |||
| 157 | return $this; |
||
| 158 | } |
||
| 159 | public function getStatus(): bool |
||
| 160 | { |
||
| 161 | return $this->status; |
||
| 162 | } |
||
| 163 | public function setDescription(string $description): self |
||
| 164 | { |
||
| 165 | $this->description = $description; |
||
| 166 | |||
| 167 | return $this; |
||
| 168 | } |
||
| 169 | public function getDescription(): ?string |
||
| 170 | { |
||
| 171 | return $this->description; |
||
| 172 | } |
||
| 173 | public function setMaxStudent(int $maxStudent): self |
||
| 174 | { |
||
| 175 | $this->maxStudent = $maxStudent; |
||
| 176 | |||
| 177 | return $this; |
||
| 178 | } |
||
| 179 | public function getMaxStudent(): int |
||
| 180 | { |
||
| 181 | return $this->maxStudent; |
||
| 182 | } |
||
| 183 | public function setDocState(int $docState): self |
||
| 184 | { |
||
| 185 | $this->docState = $docState; |
||
| 186 | |||
| 187 | return $this; |
||
| 188 | } |
||
| 189 | |||
| 190 | public function getDocState(): int |
||
| 191 | { |
||
| 192 | return $this->docState; |
||
| 193 | } |
||
| 194 | public function setCalendarState(int $calendarState): self |
||
| 195 | { |
||
| 196 | $this->calendarState = $calendarState; |
||
| 197 | |||
| 198 | return $this; |
||
| 199 | } |
||
| 200 | |||
| 201 | public function getCalendarState(): int |
||
| 202 | { |
||
| 203 | return $this->calendarState; |
||
| 204 | } |
||
| 205 | public function setWorkState(int $workState): self |
||
| 206 | { |
||
| 207 | $this->workState = $workState; |
||
| 208 | |||
| 209 | return $this; |
||
| 210 | } |
||
| 211 | |||
| 212 | public function getWorkState(): int |
||
| 213 | { |
||
| 214 | return $this->workState; |
||
| 215 | } |
||
| 216 | public function setAnnouncementsState(int $announcementsState): self |
||
| 217 | { |
||
| 218 | $this->announcementsState = $announcementsState; |
||
| 219 | |||
| 220 | return $this; |
||
| 221 | } |
||
| 222 | |||
| 223 | public function getAnnouncementsState(): int |
||
| 224 | { |
||
| 225 | return $this->announcementsState; |
||
| 226 | } |
||
| 227 | public function setForumState(int $forumState): self |
||
| 228 | { |
||
| 229 | $this->forumState = $forumState; |
||
| 230 | |||
| 231 | return $this; |
||
| 232 | } |
||
| 233 | public function getForumState(): int |
||
| 234 | { |
||
| 235 | return $this->forumState; |
||
| 236 | } |
||
| 237 | public function setWikiState(int $wikiState): self |
||
| 238 | { |
||
| 239 | $this->wikiState = $wikiState; |
||
| 240 | |||
| 241 | return $this; |
||
| 242 | } |
||
| 243 | |||
| 244 | public function getWikiState(): int |
||
| 245 | { |
||
| 246 | return $this->wikiState; |
||
| 247 | } |
||
| 248 | public function setChatState(int $chatState): self |
||
| 249 | { |
||
| 250 | $this->chatState = $chatState; |
||
| 251 | |||
| 252 | return $this; |
||
| 253 | } |
||
| 254 | |||
| 255 | public function getChatState(): int |
||
| 256 | { |
||
| 257 | return $this->chatState; |
||
| 258 | } |
||
| 259 | public function setSelfRegistrationAllowed(bool $selfRegistrationAllowed): self |
||
| 260 | { |
||
| 261 | $this->selfRegistrationAllowed = $selfRegistrationAllowed; |
||
| 262 | |||
| 263 | return $this; |
||
| 264 | } |
||
| 265 | |||
| 266 | public function getSelfRegistrationAllowed(): bool |
||
| 267 | { |
||
| 268 | return $this->selfRegistrationAllowed; |
||
| 269 | } |
||
| 270 | public function setSelfUnregistrationAllowed(bool $selfUnregistrationAllowed): self |
||
| 271 | { |
||
| 272 | $this->selfUnregistrationAllowed = $selfUnregistrationAllowed; |
||
| 273 | |||
| 274 | return $this; |
||
| 275 | } |
||
| 276 | |||
| 277 | public function getSelfUnregistrationAllowed(): bool |
||
| 278 | { |
||
| 279 | return $this->selfUnregistrationAllowed; |
||
| 280 | } |
||
| 281 | public function getDocumentAccess(): int |
||
| 282 | { |
||
| 283 | return $this->documentAccess; |
||
| 284 | } |
||
| 285 | public function setDocumentAccess(int $documentAccess): self |
||
| 286 | { |
||
| 287 | $this->documentAccess = $documentAccess; |
||
| 288 | |||
| 289 | return $this; |
||
| 290 | } |
||
| 291 | |||
| 292 | /** |
||
| 293 | * @return Collection<int, CGroupRelUser> |
||
| 294 | */ |
||
| 295 | public function getMembers(): Collection |
||
| 296 | { |
||
| 297 | return $this->members; |
||
| 298 | } |
||
| 299 | |||
| 300 | public function setMembers(Collection $members): self |
||
| 301 | { |
||
| 302 | $this->members = $members; |
||
| 303 | |||
| 304 | return $this; |
||
| 305 | } |
||
| 306 | public function hasMembers(): bool |
||
| 307 | { |
||
| 308 | return $this->members->count() > 0; |
||
| 309 | } |
||
| 310 | public function hasMember(User $user): bool |
||
| 311 | { |
||
| 312 | if (!$this->hasMembers()) { |
||
| 313 | return false; |
||
| 314 | } |
||
| 315 | $list = $this->members->filter(fn (CGroupRelUser $member) => $member->getUser()->getId() === $user->getId()); |
||
| 316 | |||
| 317 | return $list->count() > 0; |
||
| 318 | } |
||
| 319 | public function hasTutor(User $user): bool |
||
| 320 | { |
||
| 321 | if (!$this->hasTutors()) { |
||
| 322 | return false; |
||
| 323 | } |
||
| 324 | $list = $this->tutors->filter(fn (CGroupRelTutor $tutor) => $tutor->getUser()->getId() === $user->getId()); |
||
| 325 | |||
| 326 | return $list->count() > 0; |
||
| 327 | } |
||
| 328 | |||
| 329 | /** |
||
| 330 | * @return Collection<int, CGroupRelTutor> |
||
| 331 | */ |
||
| 332 | public function getTutors(): Collection |
||
| 333 | { |
||
| 334 | return $this->tutors; |
||
| 335 | } |
||
| 336 | |||
| 337 | public function setTutors(Collection $tutors): self |
||
| 338 | { |
||
| 339 | $this->tutors = $tutors; |
||
| 340 | |||
| 341 | return $this; |
||
| 342 | } |
||
| 343 | public function hasTutors(): bool |
||
| 344 | { |
||
| 345 | return $this->tutors->count() > 0; |
||
| 346 | } |
||
| 347 | public function getCategory(): ?CGroupCategory |
||
| 348 | { |
||
| 349 | return $this->category; |
||
| 350 | } |
||
| 351 | public function setCategory(?CGroupCategory $category = null): self |
||
| 352 | { |
||
| 353 | $this->category = $category; |
||
| 354 | |||
| 355 | return $this; |
||
| 356 | } |
||
| 357 | |||
| 358 | public function getResourceIdentifier(): int|Uuid |
||
| 359 | { |
||
| 360 | return $this->iid; |
||
|
|
|||
| 361 | } |
||
| 362 | public function getResourceName(): string |
||
| 363 | { |
||
| 364 | return $this->getTitle(); |
||
| 365 | } |
||
| 366 | public function setResourceName(string $name): self |
||
| 369 | } |
||
| 370 | } |
||
| 371 |