From 094d05d33cc5ba144cc5a1b8f1ac992259ae0e6a Mon Sep 17 00:00:00 2001 From: Matteo Galacci Date: Thu, 20 Oct 2022 01:20:52 +0200 Subject: [PATCH] feat: adds the __invoke() method to the service interfaces --- .../Service/Domain/CommandService.php | 6 ++++++ .../Service/Domain/NoRequestService.php | 7 +++++++ .../DDDStarterPack/Service/Domain/Service.php | 7 +++++++ .../TransactionalApplicationServiceTest.php | 18 ++++++++++++++++++ .../Application/ApplicationServiceTest.php | 10 ++++++++++ .../Service/Domain/CommandServiceTest.php | 14 +++++++++++--- .../Service/Domain/NoRequestServiceTest.php | 5 +++++ .../Service/Domain/ServiceTest.php | 10 ++++++++++ 8 files changed, 74 insertions(+), 3 deletions(-) diff --git a/src/Matiux/DDDStarterPack/Service/Domain/CommandService.php b/src/Matiux/DDDStarterPack/Service/Domain/CommandService.php index ef1ff72..2977d9f 100644 --- a/src/Matiux/DDDStarterPack/Service/Domain/CommandService.php +++ b/src/Matiux/DDDStarterPack/Service/Domain/CommandService.php @@ -18,4 +18,10 @@ interface CommandService extends Service * @psalm-assert DomainCommand $command */ public function execute($command): void; + + /** + * @param I $command + * @psalm-assert DomainCommand $command + */ + public function __invoke($command): void; } diff --git a/src/Matiux/DDDStarterPack/Service/Domain/NoRequestService.php b/src/Matiux/DDDStarterPack/Service/Domain/NoRequestService.php index 46bf742..c1e8f28 100644 --- a/src/Matiux/DDDStarterPack/Service/Domain/NoRequestService.php +++ b/src/Matiux/DDDStarterPack/Service/Domain/NoRequestService.php @@ -17,4 +17,11 @@ interface NoRequestService extends Service * @return O */ public function execute($request = null); + + /** + * @param null $request + * + * @return O + */ + public function __invoke($request = null); } diff --git a/src/Matiux/DDDStarterPack/Service/Domain/Service.php b/src/Matiux/DDDStarterPack/Service/Domain/Service.php index 8feaad9..2be0232 100644 --- a/src/Matiux/DDDStarterPack/Service/Domain/Service.php +++ b/src/Matiux/DDDStarterPack/Service/Domain/Service.php @@ -16,4 +16,11 @@ interface Service * @return O */ public function execute($request); + + /** + * @param I $request + * + * @return O + */ + public function __invoke($request); } diff --git a/tests/Integration/DDDStarterPack/Service/Application/TransactionalApplicationServiceTest.php b/tests/Integration/DDDStarterPack/Service/Application/TransactionalApplicationServiceTest.php index a602708..b56a8bc 100644 --- a/tests/Integration/DDDStarterPack/Service/Application/TransactionalApplicationServiceTest.php +++ b/tests/Integration/DDDStarterPack/Service/Application/TransactionalApplicationServiceTest.php @@ -103,6 +103,14 @@ public function execute($request): void { $this->repoA->add($request->data()); } + + /** + * {@inheritDoc} + */ + public function __invoke($request): void + { + $this->execute($request); + } } /** @@ -128,4 +136,14 @@ public function execute($request): void { $this->executeInTransaction($request); } + + /** + * {@inheritDoc} + * + * @throws TransactionFailedException + */ + public function __invoke($request): void + { + $this->execute($request); + } } diff --git a/tests/Unit/DDDStarterPack/Service/Application/ApplicationServiceTest.php b/tests/Unit/DDDStarterPack/Service/Application/ApplicationServiceTest.php index 17a7201..bdd9236 100644 --- a/tests/Unit/DDDStarterPack/Service/Application/ApplicationServiceTest.php +++ b/tests/Unit/DDDStarterPack/Service/Application/ApplicationServiceTest.php @@ -40,6 +40,16 @@ public function execute($request): array { return $request->getData(); } + + /** + * @param MyApplicationRequest $request + * + * @return array + */ + public function __invoke($request): array + { + return $this->execute($request); + } } class MyApplicationRequest diff --git a/tests/Unit/DDDStarterPack/Service/Domain/CommandServiceTest.php b/tests/Unit/DDDStarterPack/Service/Domain/CommandServiceTest.php index 315bd04..adc5a3b 100644 --- a/tests/Unit/DDDStarterPack/Service/Domain/CommandServiceTest.php +++ b/tests/Unit/DDDStarterPack/Service/Domain/CommandServiceTest.php @@ -55,11 +55,19 @@ public function __construct( } /** - * @param MyCommand $request + * @param MyCommand $command */ - public function execute($request): void + public function execute($command): void { - $this->store->log($request->operation()); + $this->store->log($command->operation()); + } + + /** + * @param MyCommand $command + */ + public function __invoke($command): void + { + $this->execute($command); } } diff --git a/tests/Unit/DDDStarterPack/Service/Domain/NoRequestServiceTest.php b/tests/Unit/DDDStarterPack/Service/Domain/NoRequestServiceTest.php index 584376f..97d6c78 100644 --- a/tests/Unit/DDDStarterPack/Service/Domain/NoRequestServiceTest.php +++ b/tests/Unit/DDDStarterPack/Service/Domain/NoRequestServiceTest.php @@ -36,4 +36,9 @@ public function execute($request = null): array { return ['foo' => 'bar']; } + + public function __invoke($request = null) + { + return $this->execute($request); + } } diff --git a/tests/Unit/DDDStarterPack/Service/Domain/ServiceTest.php b/tests/Unit/DDDStarterPack/Service/Domain/ServiceTest.php index 4757122..f3f59d0 100644 --- a/tests/Unit/DDDStarterPack/Service/Domain/ServiceTest.php +++ b/tests/Unit/DDDStarterPack/Service/Domain/ServiceTest.php @@ -39,6 +39,16 @@ public function execute($request): array { return $request->getData(); } + + /** + * @param MyRequest $request + * + * @return array + */ + public function __invoke($request): array + { + return $this->execute($request); + } } class MyRequest