BasePriceProvider::calculateDays()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 6
rs 10
c 1
b 0
f 0
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();
0 ignored issues
show
Bug introduced by
The method getProduct() 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

66
        /** @scrutinizer ignore-call */ 
67
        $product = $command->getProduct();
Loading history...
67
        $days = $this->calculateDays($command);
68
        $result = $product ? $product->getPrice() * (int)$days->days : 0;
0 ignored issues
show
introduced by
$product is of type Ticaje\Contract\Patterns...rfaces\Dto\DtoInterface, thus it always evaluated to true.
Loading history...
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();
0 ignored issues
show
Bug introduced by
The method getFromDate() 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

88
        /** @scrutinizer ignore-call */ 
89
        $from = $command->getFromDate();
Loading history...
89
        $to = $command->getToDate();
0 ignored issues
show
Bug introduced by
The method getToDate() 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

89
        /** @scrutinizer ignore-call */ 
90
        $to = $command->getToDate();
Loading history...
90
91
        return date_diff(new DateTime($to), new DateTime($from));
92
    }
93
}
94