Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
89.29% |
50 / 56 |
|
70.00% |
7 / 10 |
CRAP | |
0.00% |
0 / 1 |
StreamItemMethodAction | |
89.29% |
50 / 56 |
|
70.00% |
7 / 10 |
18.40 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
isAuthorized | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
2 | |||
__invoke | |
76.47% |
13 / 17 |
|
0.00% |
0 / 1 |
3.12 | |||
toDownload | |
93.75% |
15 / 16 |
|
0.00% |
0 / 1 |
4.00 | |||
getInputType | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getOutputType | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getPossibleActionResponseStatuses | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 | |||
getDescription | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getTags | |
80.00% |
4 / 5 |
|
0.00% |
0 / 1 |
3.07 | |||
getRouteAttributes | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
1 |
1 | <?php |
2 | namespace Apie\Common\Actions; |
3 | |
4 | use Apie\Common\IntegrationTestLogger; |
5 | use Apie\Common\Other\DownloadFile; |
6 | use Apie\Core\Actions\ActionResponse; |
7 | use Apie\Core\Actions\ActionResponseStatus; |
8 | use Apie\Core\Actions\ActionResponseStatusList; |
9 | use Apie\Core\Actions\ApieFacadeInterface; |
10 | use Apie\Core\Actions\MethodActionInterface; |
11 | use Apie\Core\BoundedContext\BoundedContextId; |
12 | use Apie\Core\Context\ApieContext; |
13 | use Apie\Core\ContextConstants; |
14 | use Apie\Core\Entities\EntityInterface; |
15 | use Apie\Core\Exceptions\EntityNotFoundException; |
16 | use Apie\Core\Exceptions\InvalidTypeException; |
17 | use Apie\Core\FileStorage\StoredFile; |
18 | use Apie\Core\IdentifierUtils; |
19 | use Apie\Core\Lists\StringList; |
20 | use Apie\Core\PropertyAccess; |
21 | use Apie\Core\ValueObjects\Exceptions\InvalidStringForValueObjectException; |
22 | use Apie\Serializer\Exceptions\ValidationException; |
23 | use LogicException; |
24 | use Nyholm\Psr7\Factory\Psr17Factory; |
25 | use Nyholm\Psr7\Stream; |
26 | use Psr\Http\Message\ResponseInterface; |
27 | use Psr\Http\Message\UploadedFileInterface; |
28 | use ReflectionClass; |
29 | use ReflectionMethod; |
30 | |
31 | /** |
32 | * Runs a method from a resource and will stream the result. |
33 | */ |
34 | final class StreamItemMethodAction implements MethodActionInterface |
35 | { |
36 | public function __construct(private readonly ApieFacadeInterface $apieFacade) |
37 | { |
38 | } |
39 | |
40 | public static function isAuthorized(ApieContext $context, bool $runtimeChecks, bool $throwError = false): bool |
41 | { |
42 | $refl = new ReflectionClass($context->getContext(ContextConstants::RESOURCE_NAME, $throwError)); |
43 | return $context->appliesToContext($refl, $runtimeChecks, $throwError ? new LogicException('Class access is not allowed!') : null); |
44 | } |
45 | |
46 | /** |
47 | * @param array<string|int, mixed> $rawContents |
48 | */ |
49 | public function __invoke(ApieContext $context, array $rawContents): ActionResponse |
50 | { |
51 | $context->withContext(ContextConstants::APIE_ACTION, __CLASS__)->checkAuthorization(); |
52 | $resourceClass = new ReflectionClass($context->getContext(ContextConstants::RESOURCE_NAME)); |
53 | if (!$resourceClass->implementsInterface(EntityInterface::class)) { |
54 | throw new InvalidTypeException($resourceClass->name, 'EntityInterface'); |
55 | } |
56 | $properties = explode('/', $context->getContext('properties')); |
57 | $id = $context->getContext(ContextConstants::RESOURCE_ID); |
58 | try { |
59 | $resource = $this->apieFacade->find( |
60 | IdentifierUtils::idStringToIdentifier($id, $context), |
61 | new BoundedContextId($context->getContext(ContextConstants::BOUNDED_CONTEXT_ID)) |
62 | ); |
63 | } catch (InvalidStringForValueObjectException|EntityNotFoundException $error) { |
64 | IntegrationTestLogger::logException($error); |
65 | return ActionResponse::createClientError($this->apieFacade, $context, $error); |
66 | } |
67 | $context = $context->withContext(ContextConstants::RESOURCE, $resource); |
68 | $result = PropertyAccess::getPropertyValue($resource, $properties, $context, false); |
69 | $result = $this->toDownload($result); |
70 | |
71 | return ActionResponse::createRunSuccess($this->apieFacade, $context, $result, $resource); |
72 | } |
73 | |
74 | private function toDownload(mixed $result): ResponseInterface |
75 | { |
76 | $factory = new Psr17Factory(); |
77 | $response = $factory->createResponse(200); |
78 | if (is_resource($result)) { |
79 | $stream = Stream::create($result); |
80 | $response = $response->withBody($stream); |
81 | $response = $response->withHeader('Content-Type', 'application/octet-stream'); |
82 | return $response; |
83 | } |
84 | if ($result instanceof UploadedFileInterface) { |
85 | $stream = $result->getStream(); |
86 | $response = $response->withBody($stream); |
87 | $filename = $result->getClientFilename(); |
88 | $mimeType = $result instanceof StoredFile ? $result->getServerMimeType() : $result->getClientMediaType(); |
89 | $response = $response->withHeader('Content-Disposition', 'attachment; filename="' . $filename . '"'); |
90 | $response = $response->withHeader('Content-Type', $mimeType); |
91 | return $response; |
92 | } |
93 | throw ValidationException::createFromArray(['' => new LogicException('There is nothing to stream')]); |
94 | } |
95 | |
96 | /** @param ReflectionClass<object> $class */ |
97 | public static function getInputType(ReflectionClass $class, ?ReflectionMethod $method = null): ReflectionMethod |
98 | { |
99 | return $class->getConstructor() ?? new ReflectionMethod(DownloadFile::class, '__construct'); |
100 | } |
101 | |
102 | /** @param ReflectionClass<object> $class */ |
103 | public static function getOutputType(ReflectionClass $class, ?ReflectionMethod $method = null): ReflectionMethod |
104 | { |
105 | return new ReflectionMethod(DownloadFile::class, 'download'); |
106 | } |
107 | |
108 | public static function getPossibleActionResponseStatuses(?ReflectionMethod $method = null): ActionResponseStatusList |
109 | { |
110 | return new ActionResponseStatusList([ |
111 | ActionResponseStatus::SUCCESS, |
112 | ActionResponseStatus::CLIENT_ERROR, |
113 | ActionResponseStatus::NOT_FOUND, |
114 | ]); |
115 | } |
116 | |
117 | /** |
118 | * @param ReflectionClass<object> $class |
119 | */ |
120 | public static function getDescription(ReflectionClass $class, ?ReflectionMethod $method = null): string |
121 | { |
122 | return 'Streams a file on a ' . $class->getShortName() . ' with a specific id'; |
123 | } |
124 | |
125 | /** |
126 | * @param ReflectionClass<object> $class |
127 | */ |
128 | public static function getTags(ReflectionClass $class, ?ReflectionMethod $method = null): StringList |
129 | { |
130 | $className = $class->getShortName(); |
131 | $declared = $method ? $method->getDeclaringClass()->getShortName() : $className; |
132 | if ($className !== $declared) { |
133 | return new StringList([$className, $declared, 'action', 'download']); |
134 | } |
135 | return new StringList([$className, 'action', 'download']); |
136 | } |
137 | |
138 | /** |
139 | * @param ReflectionClass<object> $class |
140 | */ |
141 | public static function getRouteAttributes(ReflectionClass $class, ?ReflectionMethod $method = null): array |
142 | { |
143 | return |
144 | [ |
145 | ContextConstants::GET_OBJECT => true, |
146 | ContextConstants::RESOURCE_METHOD => true, |
147 | ContextConstants::RESOURCE_NAME => $class->name, |
148 | ContextConstants::DISPLAY_FORM => true, |
149 | ]; |
150 | } |
151 | } |