Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
7.69% covered (danger)
7.69%
1 / 13
33.33% covered (danger)
33.33%
1 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
EntitySnapshotFile
7.69% covered (danger)
7.69%
1 / 13
33.33% covered (danger)
33.33%
1 / 3
24.66
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 applies
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 normalize
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
12
1<?php
2namespace Apie\Common\Other;
3
4use Apie\Common\Enums\AccessDenied;
5use Apie\Core\Attributes\ApieContextAttribute;
6use Apie\Core\FileStorage\FileStorageInterface;
7use Apie\Core\FileStorage\StoredFile;
8use Apie\Serializer\Context\ApieSerializerContext;
9use Apie\Serializer\Lists\SerializedHashmap;
10
11/**
12 * Represents an uploaded file in the audit log.
13 */
14class EntitySnapshotFile implements EntitySnapshotInstance
15{
16    public function __construct(
17        private readonly ?string $storagePath,
18        private readonly string $originalFilename,
19        public readonly ApieContextAttribute $context
20    ) {
21    }
22
23    public function applies(ApieSerializerContext $apieSerializerContext): bool
24    {
25        return $this->context->applies($apieSerializerContext->getContext());
26    }
27
28    public function normalize(ApieSerializerContext $apieSerializerContext): SerializedHashmap|AccessDenied
29    {
30        if (!$this->applies($apieSerializerContext)) {
31            return AccessDenied::Denied;
32        }
33        $storage = $apieSerializerContext->getContext()->getContext(FileStorageInterface::class, false);
34        $file = ($storage instanceof FileStorageInterface)
35            ? StoredFile::createFromStorage($storage, $this->storagePath)
36            : null;
37        return new SerializedHashmap([
38            'storagePath' => $this->storagePath,
39            'originalFilename' => $this->originalFilename,
40            'file' => $file,
41        ]);
42    }
43}