Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions css/app.scss
Original file line number Diff line number Diff line change
Expand Up @@ -1863,3 +1863,23 @@ body {
background-color: #ed9e2e;
}
}

.audit-log-table {
table-layout: fixed;

.audit-log-datetime {
width: 200px;

@media (max-width: 767px) {
width: 130px;
}
}

.audit-log-type {
width: 180px;

@media (max-width: 767px) {
width: 120px;
}
}
}
42 changes: 42 additions & 0 deletions src/Audit/Display/AbstractAuditLogDisplay.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php declare(strict_types=1);

/*
* This file is part of Packagist.
*
* (c) Jordi Boggiano <[email protected]>
* Nils Adermann <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace App\Audit\Display;

use App\Audit\AuditRecordType;

abstract readonly class AbstractAuditLogDisplay implements AuditLogDisplayInterface
{
public function __construct(
public \DateTimeImmutable $datetime,
public ActorDisplay $actor,
) {
}

abstract public function getType(): AuditRecordType;

public function getDateTime(): \DateTimeImmutable
{
return $this->datetime;
}

public function getActor(): ActorDisplay
{
return $this->actor;
}

public function getTypeTranslationKey(): string
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

instead of returning a translation key, I suggest returning a TranslatableInterface (probably using the TranslatableMessage implementation available in core in each case).

The benefits are:

  1. it will make it possible to use placeholders if needed
  2. when not using dynamic keys (unlike what it does here), keys can be extracted by the tooling (as it is aware that the constructor of TranslatableMessage takes a translation key)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think placeholders aren't really needed here as the types are just fixed strings, and translation key extraction is also kinda useless for this app where we don't really use translations much so we don't have hundreds of keys that are easy to lose track of.. Is there really a point then in changing this?

{
return 'audit_log.type.' . $this->getType()->value;
}

}
22 changes: 22 additions & 0 deletions src/Audit/Display/ActorDisplay.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php declare(strict_types=1);

/*
* This file is part of Packagist.
*
* (c) Jordi Boggiano <[email protected]>
* Nils Adermann <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace App\Audit\Display;

readonly class ActorDisplay
{
public function __construct(
public ?int $id,
public string $username,
) {
}
}
91 changes: 91 additions & 0 deletions src/Audit/Display/AuditLogDisplayFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?php declare(strict_types=1);

/*
* This file is part of Packagist.
*
* (c) Jordi Boggiano <[email protected]>
* Nils Adermann <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace App\Audit\Display;

use App\Audit\AuditRecordType;
use App\Entity\AuditRecord;

class AuditLogDisplayFactory
{
/**
* @param iterable<AuditRecord> $auditRecords
* @return array<AuditLogDisplayInterface>
*/
public function build(iterable $auditRecords): array
{
$displays = [];
foreach ($auditRecords as $record) {
$displays[] = $this->buildSingle($record);
}

return $displays;
}

public function buildSingle(AuditRecord $record): AuditLogDisplayInterface
{
return match ($record->type) {
AuditRecordType::PackageCreated => new PackageCreatedDisplay(
$record->datetime,
$record->attributes['name'],
$record->attributes['repository'],
$this->buildActor($record->attributes['actor']),
),
AuditRecordType::PackageDeleted => new PackageDeletedDisplay(
$record->datetime,
$record->attributes['name'],
$record->attributes['repository'],
$this->buildActor($record->attributes['actor']),
),
AuditRecordType::CanonicalUrlChanged => new CanonicalUrlChangedDisplay(
$record->datetime,
$record->attributes['name'],
$record->attributes['repository_from'],
$record->attributes['repository_to'],
$this->buildActor($record->attributes['actor']),
),
AuditRecordType::VersionDeleted => new VersionDeletedDisplay(
$record->datetime,
$record->attributes['name'],
$record->attributes['version'],
$this->buildActor($record->attributes['actor']),
),
AuditRecordType::VersionReferenceChanged => new VersionReferenceChangedDisplay(
$record->datetime,
$record->attributes['name'],
$record->attributes['version'],
$record->attributes['source_from'] ?? null,
$record->attributes['source_to'] ?? null,
$record->attributes['dist_from'] ?? null,
$record->attributes['dist_to'] ?? null,
$this->buildActor($record->attributes['actor'] ?? null),
),
default => throw new \LogicException(sprintf('Unsupported audit record type: %s', $record->type->value)),
};
}

/**
* @param array{id: int, username: string}|string|null $actor
*/
private function buildActor(array|string|null $actor): ActorDisplay
{
if ($actor === null) {
return new ActorDisplay(null, 'unknown');
}

if (is_string($actor)) {
return new ActorDisplay(null, $actor);
}

return new ActorDisplay($actor['id'], $actor['username']);
}
}
28 changes: 28 additions & 0 deletions src/Audit/Display/AuditLogDisplayInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php declare(strict_types=1);

/*
* This file is part of Packagist.
*
* (c) Jordi Boggiano <[email protected]>
* Nils Adermann <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace App\Audit\Display;

use App\Audit\AuditRecordType;

interface AuditLogDisplayInterface
{
public function getType(): AuditRecordType;

public function getDateTime(): \DateTimeImmutable;

public function getActor(): ActorDisplay;

public function getTypeTranslationKey(): string;

public function getTemplateName(): string;
}
38 changes: 38 additions & 0 deletions src/Audit/Display/CanonicalUrlChangedDisplay.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php declare(strict_types=1);

/*
* This file is part of Packagist.
*
* (c) Jordi Boggiano <[email protected]>
* Nils Adermann <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace App\Audit\Display;

use App\Audit\AuditRecordType;

readonly class CanonicalUrlChangedDisplay extends AbstractAuditLogDisplay
{
public function __construct(
\DateTimeImmutable $datetime,
public string $packageName,
public string $repositoryFrom,
public string $repositoryTo,
ActorDisplay $actor,
) {
parent::__construct($datetime, $actor);
}

public function getType(): AuditRecordType
{
return AuditRecordType::CanonicalUrlChanged;
}

public function getTemplateName(): string
{
return 'audit_log/display/canonical_url_changed.html.twig';
}
}
37 changes: 37 additions & 0 deletions src/Audit/Display/PackageCreatedDisplay.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php declare(strict_types=1);

/*
* This file is part of Packagist.
*
* (c) Jordi Boggiano <[email protected]>
* Nils Adermann <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace App\Audit\Display;

use App\Audit\AuditRecordType;

readonly class PackageCreatedDisplay extends AbstractAuditLogDisplay
{
public function __construct(
\DateTimeImmutable $datetime,
public string $packageName,
public string $repository,
ActorDisplay $actor,
) {
parent::__construct($datetime, $actor);
}

public function getType(): AuditRecordType
{
return AuditRecordType::PackageCreated;
}

public function getTemplateName(): string
{
return 'audit_log/display/package_created.html.twig';
}
}
37 changes: 37 additions & 0 deletions src/Audit/Display/PackageDeletedDisplay.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php declare(strict_types=1);

/*
* This file is part of Packagist.
*
* (c) Jordi Boggiano <[email protected]>
* Nils Adermann <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace App\Audit\Display;

use App\Audit\AuditRecordType;

readonly class PackageDeletedDisplay extends AbstractAuditLogDisplay
{
public function __construct(
\DateTimeImmutable $datetime,
public string $packageName,
public string $repository,
ActorDisplay $actor,
) {
parent::__construct($datetime, $actor);
}

public function getType(): AuditRecordType
{
return AuditRecordType::PackageDeleted;
}

public function getTemplateName(): string
{
return 'audit_log/display/package_deleted.html.twig';
}
}
37 changes: 37 additions & 0 deletions src/Audit/Display/VersionDeletedDisplay.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php declare(strict_types=1);

/*
* This file is part of Packagist.
*
* (c) Jordi Boggiano <[email protected]>
* Nils Adermann <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace App\Audit\Display;

use App\Audit\AuditRecordType;

readonly class VersionDeletedDisplay extends AbstractAuditLogDisplay
{
public function __construct(
\DateTimeImmutable $datetime,
public string $packageName,
public string $version,
ActorDisplay $actor,
) {
parent::__construct($datetime, $actor);
}

public function getType(): AuditRecordType
{
return AuditRecordType::VersionDeleted;
}

public function getTemplateName(): string
{
return 'audit_log/display/version_deleted.html.twig';
}
}
Loading