AggregateBase   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 14
dl 0
loc 61
rs 10
c 1
b 0
f 0
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A launchLogic() 0 6 1
A __construct() 0 10 1
A getAggregateClassName() 0 7 2
1
<?php
2
declare(strict_types=1);
3
/**
4
 * Application Handler Class
5
 * @package Ticaje_BookingApi
6
 * @author  Hector Luis Barrientos <[email protected]>
7
 */
8
9
namespace Ticaje\BookingApi\Application\UseCase\Handler;
10
11
use Exception;
12
use Ticaje\BookingApi\Application\Signatures\Provider\ServiceProviderSignature;
13
use Ticaje\Contract\Application\Service\ServiceLocatorInterface;
14
use Ticaje\Hexagonal\Application\Signatures\Responder\ResponseInterface;
15
use Ticaje\Hexagonal\Application\Signatures\UseCase\HandlerInterface;
16
use Ticaje\Hexagonal\Application\Signatures\UseCase\UseCaseCommandInterface;
17
18
/**
19
 * Class AggregateBase
20
 * @package Ticaje\BookingApi\Application\UseCase\Handler
21
 */
22
abstract class AggregateBase implements HandlerInterface
23
{
24
    /** @var ResponseInterface */
25
    protected $responder;
26
27
    /** @var ServiceLocatorInterface */
28
    private $serviceLocator;
29
30
    /** @var string */
31
    private $serviceClassMapPath;
32
33
    /** @var string */
34
    private $aggregateName;
35
36
    /**
37
     * AggregateBase constructor.
38
     *
39
     * @param ResponseInterface       $responder
40
     * @param ServiceLocatorInterface $serviceLocator
41
     * @param string                  $serviceClassMapPath
42
     * @param string                  $aggregateName
43
     */
44
    public function __construct(
45
        ResponseInterface $responder,
46
        ServiceLocatorInterface $serviceLocator,
47
        string $serviceClassMapPath,
48
        string $aggregateName
49
    ) {
50
        $this->responder = $responder;
51
        $this->serviceLocator = $serviceLocator;
52
        $this->serviceClassMapPath = $serviceClassMapPath;
53
        $this->aggregateName = $aggregateName;
54
    }
55
56
    /**
57
     * @param UseCaseCommandInterface $command
58
     *
59
     * @return mixed
60
     * @throws Exception
61
     */
62
    protected function launchLogic(UseCaseCommandInterface $command)
63
    {
64
        /** @var ServiceProviderSignature $serviceProvider */
65
        $serviceProvider = $this->serviceLocator->get($this->getAggregateClassName($command));
66
67
        return $serviceProvider->execute($command);
68
    }
69
70
    /**
71
     * @param UseCaseCommandInterface $command
72
     *
73
     * @return string
74
     * @throws Exception
75
     */
76
    private function getAggregateClassName(UseCaseCommandInterface $command): string
77
    {
78
        if (!($aggregate = $command->getAggregate())) {
0 ignored issues
show
Bug introduced by
The method getAggregate() does not exist on Ticaje\Hexagonal\Applica...UseCaseCommandInterface. It seems like you code against a sub-type of Ticaje\Hexagonal\Applica...UseCaseCommandInterface such as Ticaje\BookingApi\Applic...and\DisabledDaysCommand or Ticaje\BookingApi\Applic...etPickupLocationCommand or Ticaje\BookingApi\Applic...Command\GetPriceCommand or Ticaje\BookingApi\Applic...mmand\GetPackageCommand or Ticaje\BookingApi\Applic...\GetAvailabilityCommand. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

78
        if (!($aggregate = $command->/** @scrutinizer ignore-call */ getAggregate())) {
Loading history...
79
            throw new Exception('Aggregate does not exist');
80
        }
81
82
        return "{$this->serviceClassMapPath}\\{$aggregate}\\{$this->aggregateName}";
83
    }
84
}
85