Skip to content

Commit

Permalink
feat: adds the __invoke() method to the service interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
matiux committed Oct 19, 2022
1 parent 6d23386 commit 094d05d
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 3 deletions.
6 changes: 6 additions & 0 deletions src/Matiux/DDDStarterPack/Service/Domain/CommandService.php
Expand Up @@ -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;
}
7 changes: 7 additions & 0 deletions src/Matiux/DDDStarterPack/Service/Domain/NoRequestService.php
Expand Up @@ -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);
}
7 changes: 7 additions & 0 deletions src/Matiux/DDDStarterPack/Service/Domain/Service.php
Expand Up @@ -16,4 +16,11 @@ interface Service
* @return O
*/
public function execute($request);

/**
* @param I $request
*
* @return O
*/
public function __invoke($request);
}
Expand Up @@ -103,6 +103,14 @@ public function execute($request): void
{
$this->repoA->add($request->data());
}

/**
* {@inheritDoc}
*/
public function __invoke($request): void
{
$this->execute($request);
}
}

/**
Expand All @@ -128,4 +136,14 @@ public function execute($request): void
{
$this->executeInTransaction($request);
}

/**
* {@inheritDoc}
*
* @throws TransactionFailedException
*/
public function __invoke($request): void
{
$this->execute($request);
}
}
Expand Up @@ -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
Expand Down
14 changes: 11 additions & 3 deletions tests/Unit/DDDStarterPack/Service/Domain/CommandServiceTest.php
Expand Up @@ -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);
}
}

Expand Down
Expand Up @@ -36,4 +36,9 @@ public function execute($request = null): array
{
return ['foo' => 'bar'];
}

public function __invoke($request = null)
{
return $this->execute($request);
}
}
10 changes: 10 additions & 0 deletions tests/Unit/DDDStarterPack/Service/Domain/ServiceTest.php
Expand Up @@ -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
Expand Down

0 comments on commit 094d05d

Please sign in to comment.