Skip to content

Commit eb3a048

Browse files
feat : Add PresentationTrackChairFormatter (#464)
* feat: add presentationtrackchairformatter * chore: replace the otlp interface with abstractFactory feat: add the user context to the formatters * chore: refactor getUserInfo to AbstractAuditLog
1 parent 7e817d7 commit eb3a048

File tree

6 files changed

+342
-23
lines changed

6 files changed

+342
-23
lines changed

app/Audit/AuditLogFormatterFactory.php

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,6 @@ public function __construct()
2929
$this->config = config('audit_log', []);
3030
}
3131

32-
public function getStrategyClass(object $subject, string $event_type): ?IAuditLogFormatter
33-
{
34-
$class = get_class($subject);
35-
$cls = $this->config['entities'][$class]['strategy'] ?? null;
36-
return !is_null($cls) ? new $cls($event_type):null;
37-
}
38-
3932
public function make(AuditContext $ctx, $subject, $eventType): ?IAuditLogFormatter
4033
{
4134
$formatter = null;
@@ -53,28 +46,19 @@ public function make(AuditContext $ctx, $subject, $eventType): ?IAuditLogFormatt
5346
break;
5447
case IAuditStrategy::EVENT_ENTITY_CREATION:
5548
$formatter = $this->getFormatterByContext($subject, $eventType, $ctx);
56-
if (is_null($formatter)) {
57-
$formatter = $this->getStrategyClass($subject, $eventType);
58-
}
5949
if(is_null($formatter)) {
6050
$formatter = new EntityCreationAuditLogFormatter();
6151
}
6252
break;
6353
case IAuditStrategy::EVENT_ENTITY_DELETION:
6454
$formatter = $this->getFormatterByContext($subject, $eventType, $ctx);
65-
if (is_null($formatter)) {
66-
$formatter = $this->getStrategyClass($subject, $eventType);
67-
}
6855
if(is_null($formatter)) {
6956
$child_entity_formatter = ChildEntityFormatterFactory::build($subject);
7057
$formatter = new EntityDeletionAuditLogFormatter($child_entity_formatter);
7158
}
7259
break;
7360
case IAuditStrategy::EVENT_ENTITY_UPDATE:
7461
$formatter = $this->getFormatterByContext($subject, $eventType, $ctx);
75-
if (is_null($formatter)) {
76-
$formatter = $this->getStrategyClass($subject, $eventType);
77-
}
7862
if(is_null($formatter)) {
7963
$child_entity_formatter = ChildEntityFormatterFactory::build($subject);
8064
$formatter = new EntityUpdateAuditLogFormatter($child_entity_formatter);
@@ -90,17 +74,24 @@ private function getFormatterByContext(object $subject, string $event_type, Audi
9074
$class = get_class($subject);
9175
$entity_config = $this->config['entities'][$class] ?? null;
9276

93-
if (!$entity_config || !isset($entity_config['strategies'])) {
77+
if (!$entity_config) {
9478
return null;
9579
}
9680

97-
foreach ($entity_config['strategies'] as $strategy) {
98-
if (!$this->matchesStrategy($strategy, $ctx)) {
99-
continue;
81+
if (isset($entity_config['strategies'])) {
82+
foreach ($entity_config['strategies'] as $strategy) {
83+
if (!$this->matchesStrategy($strategy, $ctx)) {
84+
continue;
85+
}
86+
87+
$formatter_class = $strategy['formatter'] ?? null;
88+
return $formatter_class ? new $formatter_class($event_type) : null;
10089
}
90+
}
10191

102-
$formatter_class = $strategy['formatter'] ?? null;
103-
return $formatter_class ? new $formatter_class($event_type) : null;
92+
if (isset($entity_config['strategy'])) {
93+
$strategy_class = $entity_config['strategy'];
94+
return new $strategy_class($event_type);
10495
}
10596

10697
return null;
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php namespace App\Audit\ConcreteFormatters\ChildEntityFormatters;
2+
3+
use App\Models\Foundation\Summit\Events\Presentations\TrackChairs\PresentationTrackChairScore;
4+
use Illuminate\Support\Facades\Log;
5+
6+
class PresentationTrackChairScoreAuditLogFormatter implements IChildEntityAuditLogFormatter
7+
{
8+
public function format($subject, string $child_entity_action_type, ?string $additional_info = ""): ?string
9+
{
10+
if (!$subject instanceof PresentationTrackChairScore) {
11+
return null;
12+
}
13+
14+
try {
15+
$score_type = $subject->getScoreType();
16+
$score_label = $score_type ? $score_type->getLabel() : 'Unknown Score';
17+
18+
$presentation = $subject->getPresentation();
19+
$presentation_title = $presentation ? $presentation->getTitle() : 'Unknown Presentation';
20+
21+
$created_by = $subject->getCreatedBy();
22+
$chair_name = $created_by
23+
? sprintf("%s %s", $created_by->getFirstName(), $created_by->getLastName())
24+
: 'Unknown Chair';
25+
26+
switch ($child_entity_action_type) {
27+
case self::CHILD_ENTITY_CREATION:
28+
return sprintf(
29+
"Track Chair '%s' scored '%s' on presentation '%s'",
30+
$chair_name,
31+
$score_label,
32+
$presentation_title
33+
);
34+
case self::CHILD_ENTITY_DELETION:
35+
return sprintf(
36+
"Score removed for Track Chair '%s' from presentation '%s'",
37+
$chair_name,
38+
$presentation_title
39+
);
40+
case self::CHILD_ENTITY_UPDATE:
41+
return sprintf(
42+
"Track Chair '%s' score updated to '%s' on presentation '%s'",
43+
$chair_name,
44+
$score_label,
45+
$presentation_title
46+
);
47+
}
48+
} catch (\Exception $ex) {
49+
Log::warning("PresentationTrackChairScoreAuditLogFormatter error: " . $ex->getMessage());
50+
}
51+
52+
return null;
53+
}
54+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
<?php
2+
3+
namespace App\Audit\ConcreteFormatters;
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 App\Models\Foundation\Summit\Events\Presentations\TrackChairs\PresentationTrackChairRatingType;
21+
use Illuminate\Support\Facades\Log;
22+
23+
class PresentationTrackChairRatingTypeAuditLogFormatter extends AbstractAuditLogFormatter
24+
{
25+
private string $event_type;
26+
27+
public function __construct(string $event_type)
28+
{
29+
$this->event_type = $event_type;
30+
}
31+
32+
public function format($subject, array $change_set): ?string
33+
{
34+
if (!$subject instanceof PresentationTrackChairRatingType) {
35+
return null;
36+
}
37+
38+
try {
39+
$name = $subject->getName() ?? 'Unknown';
40+
$selection_plan = $subject->getSelectionPlan();
41+
$plan_name = $selection_plan ? $selection_plan->getName() : 'Unknown';
42+
43+
switch ($this->event_type) {
44+
case IAuditStrategy::EVENT_ENTITY_CREATION:
45+
return sprintf(
46+
"Track Chair Rating Type '%s' created for Selection Plan '%s' by user %s",
47+
$name,
48+
$plan_name,
49+
$this->getUserInfo()
50+
);
51+
52+
case IAuditStrategy::EVENT_ENTITY_UPDATE:
53+
$changed_fields = [];
54+
if (isset($change_set['name'])) {
55+
$changed_fields[] = "name";
56+
}
57+
58+
$fields_str = !empty($changed_fields) ? implode(', ', $changed_fields) : 'properties';
59+
return sprintf(
60+
"Track Chair Rating Type '%s' updated (%s changed) by user %s",
61+
$name,
62+
$fields_str,
63+
$this->getUserInfo()
64+
);
65+
66+
case IAuditStrategy::EVENT_ENTITY_DELETION:
67+
return sprintf(
68+
"Track Chair Rating Type '%s' deleted from Selection Plan '%s' by user %s",
69+
$name,
70+
$plan_name,
71+
$this->getUserInfo()
72+
);
73+
}
74+
} catch (\Exception $ex) {
75+
Log::warning("PresentationTrackChairRatingTypeAuditLogFormatter error: " . $ex->getMessage());
76+
}
77+
78+
return null;
79+
}
80+
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
<?php
2+
3+
namespace App\Audit\ConcreteFormatters;
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 App\Models\Foundation\Summit\Events\Presentations\TrackChairs\PresentationTrackChairScoreType;
21+
use Illuminate\Support\Facades\Log;
22+
23+
class PresentationTrackChairScoreTypeAuditLogFormatter extends AbstractAuditLogFormatter
24+
{
25+
private string $event_type;
26+
27+
public function __construct(string $event_type)
28+
{
29+
$this->event_type = $event_type;
30+
}
31+
32+
public function format($subject, array $change_set): ?string
33+
{
34+
if (!$subject instanceof PresentationTrackChairScoreType) {
35+
return null;
36+
}
37+
38+
try {
39+
$label = $subject->getLabel() ?? 'Unknown';
40+
$score = $subject->getScore() ?? 'unknown';
41+
$rating_type = $subject->getRatingType();
42+
$rating_type_name = $rating_type ? $rating_type->getName() : 'Unknown';
43+
44+
switch ($this->event_type) {
45+
case IAuditStrategy::EVENT_ENTITY_CREATION:
46+
return sprintf(
47+
"Score Type '%s' (value: %s) added to Rating Type '%s' by user %s",
48+
$label,
49+
$score,
50+
$rating_type_name,
51+
$this->getUserInfo()
52+
);
53+
54+
case IAuditStrategy::EVENT_ENTITY_UPDATE:
55+
$changed_fields = [];
56+
if (isset($change_set['label'])) {
57+
$changed_fields[] = "label";
58+
}
59+
if (isset($change_set['score'])) {
60+
$changed_fields[] = "score";
61+
}
62+
63+
$fields_str = !empty($changed_fields) ? implode(', ', $changed_fields) : 'properties';
64+
return sprintf(
65+
"Score Type '%s' updated (%s changed) by user %s",
66+
$label,
67+
$fields_str,
68+
$this->getUserInfo()
69+
);
70+
71+
case IAuditStrategy::EVENT_ENTITY_DELETION:
72+
return sprintf(
73+
"Score Type '%s' removed from Rating Type '%s' by user %s",
74+
$label,
75+
$rating_type_name,
76+
$this->getUserInfo()
77+
);
78+
}
79+
} catch (\Exception $ex) {
80+
Log::warning("PresentationTrackChairScoreTypeAuditLogFormatter error: " . $ex->getMessage());
81+
}
82+
83+
return null;
84+
}
85+
}
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
<?php
2+
3+
namespace App\Audit\ConcreteFormatters;
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\SummitTrackChair;
21+
use Illuminate\Support\Facades\Log;
22+
23+
class SummitTrackChairAuditLogFormatter extends AbstractAuditLogFormatter
24+
{
25+
private string $event_type;
26+
27+
public function __construct(string $event_type)
28+
{
29+
$this->event_type = $event_type;
30+
}
31+
32+
public function format($subject, array $change_set): ?string
33+
{
34+
if (!$subject instanceof SummitTrackChair) {
35+
return null;
36+
}
37+
38+
try {
39+
$member = $subject->getMember();
40+
$member_name = $member ? sprintf("%s %s", $member->getFirstName(), $member->getLastName()) : 'Unknown';
41+
$member_id = $member ? $member->getId() : 'unknown';
42+
43+
switch ($this->event_type) {
44+
case IAuditStrategy::EVENT_ENTITY_CREATION:
45+
$categories = [];
46+
foreach ($subject->getCategories() as $category) {
47+
$categories[] = $category->getTitle();
48+
}
49+
$tracks_list = !empty($categories) ? implode(', ', $categories) : 'No tracks assigned';
50+
return sprintf(
51+
"Track Chair '%s' (%d) assigned with tracks: %s by user %s",
52+
$member_name,
53+
$member_id,
54+
$tracks_list,
55+
$this->getUserInfo()
56+
);
57+
58+
case IAuditStrategy::EVENT_ENTITY_UPDATE:
59+
if (isset($change_set['categories'])) {
60+
$old_cats = $change_set['categories'][0] ?? [];
61+
$new_cats = $change_set['categories'][1] ?? [];
62+
63+
$old_names = is_array($old_cats)
64+
? array_map(fn($c) => $c->getTitle() ?? 'Unknown', $old_cats)
65+
: [];
66+
$new_names = is_array($new_cats)
67+
? array_map(fn($c) => $c->getTitle() ?? 'Unknown', $new_cats)
68+
: [];
69+
70+
$old_str = !empty($old_names) ? implode(', ', $old_names) : 'None';
71+
$new_str = !empty($new_names) ? implode(', ', $new_names) : 'None';
72+
73+
return sprintf(
74+
"Track Chair '%s' tracks changed: [%s] → [%s] by user %s",
75+
$member_name,
76+
$old_str,
77+
$new_str,
78+
$this->getUserInfo()
79+
);
80+
}
81+
return sprintf("Track Chair '%s' updated by user %s", $member_name, $this->getUserInfo());
82+
83+
case IAuditStrategy::EVENT_ENTITY_DELETION:
84+
return sprintf(
85+
"Track Chair '%s' (%d) removed from summit by user %s",
86+
$member_name,
87+
$member_id,
88+
$this->getUserInfo()
89+
);
90+
}
91+
} catch (\Exception $ex) {
92+
Log::warning("SummitTrackChairAuditLogFormatter error: " . $ex->getMessage());
93+
}
94+
95+
return null;
96+
}
97+
}

0 commit comments

Comments
 (0)