Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
13.33% covered (danger)
13.33%
2 / 15
50.00% covered (danger)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
UploadedFileDisplayProvider
13.33% covered (danger)
13.33%
2 / 15
50.00% covered (danger)
50.00%
1 / 2
49.66
0.00% covered (danger)
0.00%
0 / 1
 supports
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
3
 createComponentFor
0.00% covered (danger)
0.00%
0 / 13
0.00% covered (danger)
0.00%
0 / 1
30
1<?php
2namespace Apie\HtmlBuilders\FieldDisplayProviders;
3
4use Apie\Core\ContextConstants;
5use Apie\Core\Entities\EntityInterface;
6use Apie\Core\FileStorage\StoredFile;
7use Apie\HtmlBuilders\Components\Resource\FieldDisplay\LinkDisplay;
8use Apie\HtmlBuilders\FieldDisplayBuildContext;
9use Apie\HtmlBuilders\Interfaces\ComponentInterface;
10use Apie\HtmlBuilders\Interfaces\FieldDisplayComponentProviderInterface;
11use Psr\Http\Message\UploadedFileInterface;
12use ReflectionClass;
13
14final class UploadedFileDisplayProvider implements FieldDisplayComponentProviderInterface
15{
16    public function supports(mixed $object, FieldDisplayBuildContext $context): bool
17    {
18        return $context->getResource() instanceof EntityInterface
19            && ($object instanceof UploadedFileInterface || is_resource($object));
20    }
21    public function createComponentFor(mixed $object, FieldDisplayBuildContext $context): ComponentInterface
22    {
23        $text = null;
24        if ($object instanceof UploadedFileInterface) {
25            $text = $object->getClientFilename();
26            if (null === $text && $object instanceof StoredFile) {
27                $text = $object->getServerPath();
28            }
29            $size = $object->getSize();
30            if ($size !== null) {
31                $text .= ' (' . $size . ' bytes)';
32            }
33        }
34        $text ??= 'download';
35        $resource = $context->getResource();
36        assert($resource instanceof EntityInterface);
37        $resourceName = (new ReflectionClass($context->getApieContext()->getContext(ContextConstants::RESOURCE_NAME)))->getShortName();
38        return new LinkDisplay($text, '../action/' . $resourceName . '/' . $resource->getId()->toNative() . '/download/' . implode('/', $context->getVisitedNodes()));
39    }
40}