Skip to content

Commit

Permalink
Introduce trait
Browse files Browse the repository at this point in the history
  • Loading branch information
mattamon committed Apr 22, 2024
1 parent 01ed8c9 commit b10c989
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 14 deletions.
9 changes: 3 additions & 6 deletions src/EventSubscriber/ApiExceptionSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
*/
final class ApiExceptionSubscriber implements EventSubscriberInterface
{
use StudioApiPathTrait;

public static function getSubscribedEvents(): array
{
return [
Expand All @@ -39,7 +41,7 @@ public function onKernelException(ExceptionEvent $event): void
$exception = $event->getThrowable();
$request = $event->getRequest();

if(!$exception instanceof ApiExceptionInterface && !$this->isStudioApiCall($request->getPathInfo())) {
if(!$exception instanceof ApiExceptionInterface && !$this->isStudioApiPath($request->getPathInfo())) {
return;
}
/** @var ApiExceptionInterface $exception */
Expand All @@ -52,9 +54,4 @@ public function onKernelException(ExceptionEvent $event): void

$event->setResponse($response);
}

private function isStudioApiCall(string $path): bool
{
return str_starts_with($path, AbstractApiController::API_PATH);
}
}
13 changes: 5 additions & 8 deletions src/EventSubscriber/CorsSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@

final readonly class CorsSubscriber implements EventSubscriberInterface
{
use StudioApiPathTrait;

public function __construct(private array $allowedHosts = [])
{
}
Expand All @@ -46,7 +48,7 @@ public function onKernelRequest(RequestEvent $event): void

$request = $event->getRequest();

if(!$this->checkStudioApiPath($request->getPathInfo())) {
if(!$this->isStudioApiPath($request->getPathInfo())) {
return;
}

Expand All @@ -67,11 +69,11 @@ public function onKernelResponse(ResponseEvent $event): void
{
$request = $event->getRequest();

if(!$this->checkStudioApiPath($request->getPathInfo())) {
if (!$this->isStudioApiPath($request->getPathInfo())) {
return;
}
// Run CORS check in here to ensure domain is in the system
$corsOrigin = $request->headers->get('origin');
$corsOrigin = $request->headers->get('origin');

if (in_array($corsOrigin, $this->allowedHosts, true)) {
$response = $event->getResponse();
Expand All @@ -84,9 +86,4 @@ public function onKernelResponse(ResponseEvent $event): void

}
}

private function checkStudioApiPath(string $path): bool
{
return str_contains($path, AbstractApiController::API_PATH);
}
}
30 changes: 30 additions & 0 deletions src/EventSubscriber/StudioApiPathTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php
declare(strict_types=1);

/**
* Pimcore
*
* This source file is available under two different licenses:
* - GNU General Public License version 3 (GPLv3)
* - Pimcore Commercial License (PCL)
* Full copyright and license information is available in
* LICENSE.md which is distributed with this source code.
*
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org)
* @license http://www.pimcore.org/license GPLv3 and PCL
*/

namespace Pimcore\Bundle\StudioApiBundle\EventSubscriber;

use Pimcore\Bundle\StudioApiBundle\Controller\Api\AbstractApiController;

/**
* @internal
*/
trait StudioApiPathTrait
{
private function isStudioApiPath(string $path): bool
{
return str_starts_with($path, AbstractApiController::API_PATH);
}
}

0 comments on commit b10c989

Please sign in to comment.