Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
13.33% |
2 / 15 |
|
50.00% |
1 / 2 |
CRAP | |
0.00% |
0 / 1 |
| UploadedFileDisplayProvider | |
13.33% |
2 / 15 |
|
50.00% |
1 / 2 |
49.66 | |
0.00% |
0 / 1 |
| supports | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
3 | |||
| createComponentFor | |
0.00% |
0 / 13 |
|
0.00% |
0 / 1 |
30 | |||
| 1 | <?php |
| 2 | namespace Apie\HtmlBuilders\FieldDisplayProviders; |
| 3 | |
| 4 | use Apie\Core\ContextConstants; |
| 5 | use Apie\Core\Entities\EntityInterface; |
| 6 | use Apie\Core\FileStorage\StoredFile; |
| 7 | use Apie\HtmlBuilders\Components\Resource\FieldDisplay\LinkDisplay; |
| 8 | use Apie\HtmlBuilders\FieldDisplayBuildContext; |
| 9 | use Apie\HtmlBuilders\Interfaces\ComponentInterface; |
| 10 | use Apie\HtmlBuilders\Interfaces\FieldDisplayComponentProviderInterface; |
| 11 | use Psr\Http\Message\UploadedFileInterface; |
| 12 | use ReflectionClass; |
| 13 | |
| 14 | final 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 | } |