-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(actions): move PdkActions to a proper service with interface (…
- Loading branch information
1 parent
e7356af
commit 94508ee
Showing
9 changed files
with
278 additions
and
104 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace MyParcelNL\Pdk\App\Api\Contract; | ||
|
||
use Symfony\Component\HttpFoundation\Response; | ||
|
||
interface PdkActionsServiceInterface | ||
{ | ||
/** | ||
* @param string|\Symfony\Component\HttpFoundation\Request $action | ||
* @param array $parameters | ||
* | ||
* @return \Symfony\Component\HttpFoundation\Response | ||
* @throws \MyParcelNL\Pdk\Api\Exception\ApiException | ||
* @throws \MyParcelNL\Pdk\Api\Exception\PdkEndpointException | ||
*/ | ||
public function execute($action, array $parameters = []): Response; | ||
|
||
/** | ||
* @param string $context | ||
* | ||
* @return $this | ||
*/ | ||
public function setContext(string $context): self; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace MyParcelNL\Pdk\App\Api\Service; | ||
|
||
use InvalidArgumentException; | ||
use MyParcelNL\Pdk\Api\Exception\PdkEndpointException; | ||
use MyParcelNL\Pdk\App\Api\Contract\PdkActionsServiceInterface; | ||
use MyParcelNL\Pdk\App\Api\PdkEndpoint; | ||
use MyParcelNL\Pdk\Base\Support\Collection; | ||
use MyParcelNL\Pdk\Facade\Config; | ||
use MyParcelNL\Pdk\Facade\Pdk; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpFoundation\Response; | ||
|
||
class PdkActionsService implements PdkActionsServiceInterface | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
private $context = PdkEndpoint::CONTEXT_BACKEND; | ||
|
||
/** | ||
* @param string|\Symfony\Component\HttpFoundation\Request $action | ||
* @param array $parameters | ||
* | ||
* @return \Symfony\Component\HttpFoundation\Response | ||
* @throws \MyParcelNL\Pdk\Api\Exception\ApiException | ||
* @throws \MyParcelNL\Pdk\Api\Exception\PdkEndpointException | ||
*/ | ||
public function execute($action, array $parameters = []): Response | ||
{ | ||
$request = $this->createRequest($action, $parameters); | ||
|
||
$actionClass = $this->resolveAction($request); | ||
|
||
/** @var \MyParcelNL\Pdk\App\Action\Contract\ActionInterface $action */ | ||
$action = Pdk::get($actionClass); | ||
|
||
return $action->handle($request); | ||
} | ||
|
||
/** | ||
* @param string $context | ||
* | ||
* @return $this | ||
*/ | ||
public function setContext(string $context): PdkActionsServiceInterface | ||
{ | ||
$this->context = $context; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* @param $input | ||
* @param array $parameters | ||
* | ||
* @return \Symfony\Component\HttpFoundation\Request | ||
*/ | ||
protected function createRequest($input, array $parameters = []): Request | ||
{ | ||
if ($input instanceof Request) { | ||
if (empty($input->get('action'))) { | ||
throw new InvalidArgumentException('Required parameter "action" is missing.'); | ||
} | ||
|
||
return $input; | ||
} | ||
|
||
if (is_string($input)) { | ||
$request = Request::createFromGlobals(); | ||
|
||
foreach ($parameters as $key => $value) { | ||
$request->query->set($key, $value); | ||
} | ||
|
||
$request->query->set('action', $input); | ||
|
||
return $request; | ||
} | ||
|
||
throw new InvalidArgumentException('Input must be a string or a Request object.'); | ||
} | ||
|
||
/** | ||
* @param \Symfony\Component\HttpFoundation\Request $request | ||
* | ||
* @return string | ||
* @throws \MyParcelNL\Pdk\Api\Exception\PdkEndpointException | ||
*/ | ||
private function resolveAction(Request $request): string | ||
{ | ||
$action = $request->get('action'); | ||
|
||
if (! $this->context || ! in_array($this->context, PdkEndpoint::CONTEXTS, true)) { | ||
throw new PdkEndpointException('Context is invalid.', Response::HTTP_UNPROCESSABLE_ENTITY); | ||
} | ||
|
||
$actions = new Collection(Config::get('actions')); | ||
|
||
$match = $actions->dataGet("$this->context.$action") ?? $actions->dataGet("shared.$action"); | ||
$actionClass = $match['action'] ?? null; | ||
|
||
if (! $actionClass || ! class_exists($actionClass)) { | ||
throw new PdkEndpointException("Action \"$action\" does not exist.", Response::HTTP_UNPROCESSABLE_ENTITY); | ||
} | ||
|
||
return $actionClass; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace MyParcelNL\Pdk\Tests\Bootstrap; | ||
|
||
use MyParcelNL\Pdk\App\Api\Service\PdkActionsService; | ||
use MyParcelNL\Pdk\Base\Support\Collection; | ||
use Symfony\Component\HttpFoundation\Response; | ||
use Symfony\Contracts\Service\ResetInterface; | ||
|
||
final class MockPdkActionsService extends PdkActionsService implements ResetInterface | ||
{ | ||
/** | ||
* @var \MyParcelNL\Pdk\Base\Support\Collection | ||
*/ | ||
private $calls; | ||
|
||
public function __construct() | ||
{ | ||
$this->reset(); | ||
} | ||
|
||
/** | ||
* @param string|\Symfony\Component\HttpFoundation\Request $action | ||
* @param array $parameters | ||
* | ||
* @return \Symfony\Component\HttpFoundation\Response | ||
* @throws \MyParcelNL\Pdk\Api\Exception\ApiException | ||
* @throws \MyParcelNL\Pdk\Api\Exception\PdkEndpointException | ||
*/ | ||
public function execute($action, array $parameters = []): Response | ||
{ | ||
$this->calls->push([ | ||
'action' => $action, | ||
'parameters' => $parameters, | ||
]); | ||
|
||
return parent::execute($action, $parameters); | ||
} | ||
|
||
/** | ||
* @return \MyParcelNL\Pdk\Base\Support\Collection | ||
*/ | ||
public function getCalls(): Collection | ||
{ | ||
return $this->calls; | ||
} | ||
|
||
public function reset(): void | ||
{ | ||
$this->calls = new Collection(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.