1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
/** |
4
|
|
|
* Application Service Class |
5
|
|
|
* @package Ticaje_BookingApi |
6
|
|
|
* @author Hector Luis Barrientos <[email protected]> |
7
|
|
|
* @author Lino Aboy <[email protected]> |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Ticaje\BookingApi\Application\Service\Provider; |
11
|
|
|
|
12
|
|
|
use DateInterval; |
13
|
|
|
use DateTime; |
14
|
|
|
use Exception; |
15
|
|
|
use Ticaje\BookingApi\Application\Signatures\Provider\PriceProviderGatewaySignature; |
16
|
|
|
use Ticaje\BookingApi\Application\Signatures\Provider\PriceProviderSignature; |
17
|
|
|
use Ticaje\BookingApi\Application\Signatures\Provider\ServiceProviderSignature; |
18
|
|
|
use Ticaje\Contract\Patterns\Interfaces\Dto\DtoInterface; |
19
|
|
|
use Ticaje\Contract\Persistence\Entity\EntityInterface; |
20
|
|
|
use Ticaje\Hexagonal\Application\Signatures\UseCase\UseCaseCommandInterface; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Class BasePriceProvider |
24
|
|
|
* @package Ticaje\BookingApi\Application\Service\Provider |
25
|
|
|
*/ |
26
|
|
|
abstract class BasePriceProvider implements ServiceProviderSignature, PriceProviderSignature |
27
|
|
|
{ |
28
|
|
|
/** @var EntityInterface instance */ |
29
|
|
|
protected $instance; |
30
|
|
|
|
31
|
|
|
/** @var PriceProviderGatewaySignature $priceProviderGatewaySignature */ |
32
|
|
|
private $priceProviderGatewaySignature; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* BasePriceProvider constructor. |
36
|
|
|
* |
37
|
|
|
* @param PriceProviderGatewaySignature $priceProviderGatewaySignature |
38
|
|
|
*/ |
39
|
|
|
public function __construct(PriceProviderGatewaySignature $priceProviderGatewaySignature) |
40
|
|
|
{ |
41
|
|
|
$this->priceProviderGatewaySignature = $priceProviderGatewaySignature; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @inheritDoc |
46
|
|
|
*/ |
47
|
|
|
public function execute(UseCaseCommandInterface $command) |
48
|
|
|
{ |
49
|
|
|
$price = (float)0; |
50
|
|
|
if ($this->instance = $this->priceProviderGatewaySignature->execute($command)) { |
51
|
|
|
$price = $this->getPrice($command); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
return (float)0 === $price ? $this->getBasePrice($command) : $price; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param UseCaseCommandInterface $command |
59
|
|
|
* |
60
|
|
|
* @return float|int |
61
|
|
|
* @throws Exception |
62
|
|
|
*/ |
63
|
|
|
protected function getBasePrice(UseCaseCommandInterface $command): float |
64
|
|
|
{ |
65
|
|
|
/** @var DtoInterface $product */ |
66
|
|
|
$product = $command->getProduct(); |
|
|
|
|
67
|
|
|
$days = $this->calculateDays($command); |
68
|
|
|
$result = $product ? $product->getPrice() * (int)$days->days : 0; |
|
|
|
|
69
|
|
|
|
70
|
|
|
return (float)$result; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param UseCaseCommandInterface $command |
75
|
|
|
* |
76
|
|
|
* @return float |
77
|
|
|
*/ |
78
|
|
|
abstract public function getPrice(UseCaseCommandInterface $command): float; |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param UseCaseCommandInterface $command |
82
|
|
|
* |
83
|
|
|
* @return DateInterval|false |
84
|
|
|
* @throws Exception |
85
|
|
|
*/ |
86
|
|
|
protected function calculateDays(UseCaseCommandInterface $command) |
87
|
|
|
{ |
88
|
|
|
$from = $command->getFromDate(); |
|
|
|
|
89
|
|
|
$to = $command->getToDate(); |
|
|
|
|
90
|
|
|
|
91
|
|
|
return date_diff(new DateTime($to), new DateTime($from)); |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|