|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Audit\ConcreteFormatters\PresentationFormatters; |
| 4 | + |
| 5 | +/** |
| 6 | + * Copyright 2025 OpenStack Foundation |
| 7 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | + * you may not use this file except in compliance with the License. |
| 9 | + * You may obtain a copy of the License at |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + **/ |
| 17 | + |
| 18 | +use App\Audit\AbstractAuditLogFormatter; |
| 19 | +use App\Audit\Interfaces\IAuditStrategy; |
| 20 | +use models\summit\Presentation; |
| 21 | +use Illuminate\Support\Facades\Log; |
| 22 | + |
| 23 | +abstract class BasePresentationAuditLogFormatter extends AbstractAuditLogFormatter |
| 24 | +{ |
| 25 | + protected string $event_type; |
| 26 | + |
| 27 | + public function __construct(string $event_type) |
| 28 | + { |
| 29 | + $this->event_type = $event_type; |
| 30 | + } |
| 31 | + |
| 32 | + protected function extractChangedFields(array $change_set): array |
| 33 | + { |
| 34 | + $changed_fields = []; |
| 35 | + $old_status = null; |
| 36 | + $new_status = null; |
| 37 | + |
| 38 | + if (isset($change_set['Title'])) { |
| 39 | + $changed_fields[] = "title"; |
| 40 | + } |
| 41 | + if (isset($change_set['Abstract'])) { |
| 42 | + $changed_fields[] = "abstract"; |
| 43 | + } |
| 44 | + if (isset($change_set['ProblemAddressed'])) { |
| 45 | + $changed_fields[] = "problem_addressed"; |
| 46 | + } |
| 47 | + if (isset($change_set['AttendeesExpectedLearnt'])) { |
| 48 | + $changed_fields[] = "attendees_expected_learnt"; |
| 49 | + } |
| 50 | + |
| 51 | + if (isset($change_set['Status'])) { |
| 52 | + $changed_fields[] = "status"; |
| 53 | + $old_status = $change_set['Status'][0] ?? null; |
| 54 | + $new_status = $change_set['Status'][1] ?? null; |
| 55 | + } |
| 56 | + if (isset($change_set['CategoryID']) || isset($change_set['category'])) { |
| 57 | + $changed_fields[] = "track"; |
| 58 | + } |
| 59 | + if (isset($change_set['Published'])) { |
| 60 | + $changed_fields[] = "published"; |
| 61 | + } |
| 62 | + if (isset($change_set['SelectionPlanID'])) { |
| 63 | + $changed_fields[] = "selection_plan"; |
| 64 | + } |
| 65 | + |
| 66 | + return [ |
| 67 | + 'fields' => !empty($changed_fields) ? implode(', ', $changed_fields) : 'properties', |
| 68 | + 'old_status' => $old_status, |
| 69 | + 'new_status' => $new_status, |
| 70 | + ]; |
| 71 | + } |
| 72 | + |
| 73 | + protected function getPresentationData(Presentation $subject): array |
| 74 | + { |
| 75 | + $creator = $subject->getCreator(); |
| 76 | + $creator_name = $creator ? sprintf("%s %s", $creator->getFirstName() ?? '', $creator->getLastName() ?? '') : 'Unknown'; |
| 77 | + $creator_name = trim($creator_name) ?: 'Unknown'; |
| 78 | + |
| 79 | + $category = $subject->getCategory(); |
| 80 | + $category_name = $category ? $category->getTitle() : 'Unassigned Track'; |
| 81 | + |
| 82 | + $selection_plan = $subject->getSelectionPlan(); |
| 83 | + $plan_name = $selection_plan ? $selection_plan->getName() : 'Unknown Plan'; |
| 84 | + |
| 85 | + return [ |
| 86 | + 'title' => $subject->getTitle() ?? 'Unknown Presentation', |
| 87 | + 'id' => $subject->getId() ?? 'unknown', |
| 88 | + 'creator_name' => $creator_name, |
| 89 | + 'category_name' => $category_name, |
| 90 | + 'plan_name' => $plan_name, |
| 91 | + ]; |
| 92 | + } |
| 93 | + |
| 94 | + public function format($subject, array $change_set): ?string |
| 95 | + { |
| 96 | + if (!$subject instanceof Presentation) { |
| 97 | + return null; |
| 98 | + } |
| 99 | + |
| 100 | + try { |
| 101 | + $data = $this->getPresentationData($subject); |
| 102 | + |
| 103 | + switch ($this->event_type) { |
| 104 | + case IAuditStrategy::EVENT_ENTITY_CREATION: |
| 105 | + return $this->formatCreation($data); |
| 106 | + |
| 107 | + case IAuditStrategy::EVENT_ENTITY_UPDATE: |
| 108 | + $extracted = $this->extractChangedFields($change_set); |
| 109 | + return $this->formatUpdate($data, $extracted); |
| 110 | + |
| 111 | + case IAuditStrategy::EVENT_ENTITY_DELETION: |
| 112 | + return $this->formatDeletion($data); |
| 113 | + } |
| 114 | + } catch (\Exception $ex) { |
| 115 | + Log::warning(static::class . " error: " . $ex->getMessage()); |
| 116 | + } |
| 117 | + |
| 118 | + return null; |
| 119 | + } |
| 120 | + |
| 121 | + abstract protected function formatCreation(array $data): string; |
| 122 | + |
| 123 | + abstract protected function formatUpdate(array $data, array $extracted): string; |
| 124 | + |
| 125 | + abstract protected function formatDeletion(array $data): string; |
| 126 | +} |
0 commit comments