From 7645fa5db4b8db5368cdee71a325b59963002ae4 Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Thu, 3 Jun 2021 21:38:04 -0700 Subject: [PATCH] (REF) Civi/API/Event - Extract RequestTrait --- Civi/API/Event/Event.php | 32 ++-------------- Civi/API/Event/PrepareEvent.php | 6 +-- Civi/API/Event/RequestTrait.php | 67 +++++++++++++++++++++++++++++++++ Civi/API/Event/ResolveEvent.php | 3 +- 4 files changed, 75 insertions(+), 33 deletions(-) create mode 100644 Civi/API/Event/RequestTrait.php diff --git a/Civi/API/Event/Event.php b/Civi/API/Event/Event.php index 9a68b74339..c5ad2cc92b 100644 --- a/Civi/API/Event/Event.php +++ b/Civi/API/Event/Event.php @@ -17,6 +17,8 @@ namespace Civi\API\Event; */ class Event extends \Symfony\Component\EventDispatcher\Event { + use RequestTrait; + /** * @var \Civi\API\Kernel */ @@ -28,14 +30,6 @@ class Event extends \Symfony\Component\EventDispatcher\Event { */ protected $apiProvider; - /** - * @var array - * The full description of the API request. - * - * @see \Civi\API\Request::create - */ - protected $apiRequest; - /** * @param \Civi\API\Provider\ProviderInterface $apiProvider * The API responsible for executing the request. @@ -46,7 +40,7 @@ class Event extends \Symfony\Component\EventDispatcher\Event { public function __construct($apiProvider, $apiRequest, $apiKernel) { $this->apiKernel = $apiKernel; $this->apiProvider = $apiProvider; - $this->apiRequest = $apiRequest; + $this->setApiRequest($apiRequest); } /** @@ -65,24 +59,4 @@ class Event extends \Symfony\Component\EventDispatcher\Event { return $this->apiProvider; } - /** - * @return array - */ - public function getApiRequest() { - return $this->apiRequest; - } - - /** - * Create a brief string identifying the entity/action. Useful for - * pithy matching/switching. - * - * Ex: if ($e->getApiRequestSig() === '3.contact.get') { ... } - * - * @return string - * Ex: '3.contact.get' - */ - public function getApiRequestSig() { - return mb_strtolower($this->apiRequest['version'] . '.' . $this->apiRequest['entity'] . '.' . $this->apiRequest['action']); - } - } diff --git a/Civi/API/Event/PrepareEvent.php b/Civi/API/Event/PrepareEvent.php index 1c6576173c..452ee4c9c2 100644 --- a/Civi/API/Event/PrepareEvent.php +++ b/Civi/API/Event/PrepareEvent.php @@ -26,11 +26,11 @@ class PrepareEvent extends Event { /** * @param array $apiRequest * The full description of the API request. - * @return PrepareEvent + * @return static */ public function setApiRequest($apiRequest) { - $this->apiRequest = $apiRequest; - return $this; + // Elevate from 'protected' to 'public'. + return parent::setApiRequest($apiRequest); } /** diff --git a/Civi/API/Event/RequestTrait.php b/Civi/API/Event/RequestTrait.php new file mode 100644 index 0000000000..b216c5e1d7 --- /dev/null +++ b/Civi/API/Event/RequestTrait.php @@ -0,0 +1,67 @@ +apiRequest; + } + + /** + * @param \Civi\Api4\Generic\AbstractAction|array $apiRequest + * The full description of the API request. + * @return static + */ + protected function setApiRequest($apiRequest) { + $this->apiRequest = $apiRequest; + return $this; + } + + /** + * Create a brief string identifying the entity/action. Useful for + * pithy matching/switching. + * + * Ex: if ($e->getApiRequestSig() === '3.contact.get') { ... } + * + * @return string + * Ex: '3.contact.get' + */ + public function getApiRequestSig(): string { + return mb_strtolower($this->apiRequest['version'] . '.' . $this->apiRequest['entity'] . '.' . $this->apiRequest['action']); + } + + /** + * @return string + * Ex: 'Contact', 'Activity' + */ + public function getEntityName(): string { + return $this->apiRequest['entity']; + } + + /** + * @return string + * Ex: 'create', 'update' + */ + public function getActionName(): string { + return $this->apiRequest['action']; + } + +} diff --git a/Civi/API/Event/ResolveEvent.php b/Civi/API/Event/ResolveEvent.php index 8d574a0327..cd2cba839e 100644 --- a/Civi/API/Event/ResolveEvent.php +++ b/Civi/API/Event/ResolveEvent.php @@ -46,7 +46,8 @@ class ResolveEvent extends Event { * The full description of the API request. */ public function setApiRequest($apiRequest) { - $this->apiRequest = $apiRequest; + // Elevate from 'protected' to 'public'. + return parent::setApiRequest($apiRequest); } } -- 2.25.1