|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace NuonicPluginInstaller\Controller; |
| 6 | + |
| 7 | +use NuonicPluginInstaller\Service\PackageService; |
| 8 | +use NuonicPluginInstaller\Struct\PackageVersion; |
| 9 | +use OpenApi\Attributes as OA; |
| 10 | +use Shopware\Core\Framework\Context; |
| 11 | +use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; |
| 12 | +use Symfony\Component\HttpFoundation\JsonResponse; |
| 13 | +use Symfony\Component\HttpFoundation\Request; |
| 14 | +use Symfony\Component\HttpFoundation\Response; |
| 15 | +use Symfony\Component\Routing\Attribute\Route; |
| 16 | + |
| 17 | +#[Route(defaults: ['_routeScope' => ['api']])] |
| 18 | +class InstallController extends AbstractController |
| 19 | +{ |
| 20 | + public function __construct( |
| 21 | + private readonly PackageService $packageService, |
| 22 | + ) { |
| 23 | + } |
| 24 | + |
| 25 | + #[OA\Post( |
| 26 | + path: '/api/_action/nuonic-plugin-installer/install', |
| 27 | + operationId: 'executeWebhook', |
| 28 | + requestBody: new OA\RequestBody(content: new OA\JsonContent()), |
| 29 | + tags: ['Admin Api'], |
| 30 | + responses: [ |
| 31 | + new OA\Response(response: Response::HTTP_NO_CONTENT, description: 'MediaProxy returns data'), |
| 32 | + new OA\Response(response: Response::HTTP_BAD_REQUEST, description: 'Invalid input data'), |
| 33 | + ] |
| 34 | + )] |
| 35 | + #[Route( |
| 36 | + path: '/api/_action/nuonic-plugin-installer/install', |
| 37 | + name: 'api.action.nuonic_plugin_installer.install.execute', |
| 38 | + defaults: ['auth_required' => false], |
| 39 | + methods: ['POST'] |
| 40 | + )] |
| 41 | + public function execute(Request $request, Context $context): Response |
| 42 | + { |
| 43 | + $requestData = $request->toArray(); |
| 44 | + if (!isset($requestData['package'], $requestData['version'])) { |
| 45 | + $this->malformedRequestError(); |
| 46 | + } |
| 47 | + |
| 48 | + $this->packageService->install(new PackageVersion( |
| 49 | + $requestData['package'], |
| 50 | + $requestData['version'] |
| 51 | + ), $context); |
| 52 | + |
| 53 | + return new Response(status: 201); |
| 54 | + } |
| 55 | + |
| 56 | + protected function malformedRequestError(): Response |
| 57 | + { |
| 58 | + return new JsonResponse( |
| 59 | + ['status' => 'error', 'message' => 'Request data is not formed correctly.'], |
| 60 | + status: 400 |
| 61 | + ); |
| 62 | + } |
| 63 | +} |
0 commit comments