Price   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 10
eloc 25
c 1
b 0
f 0
dl 0
loc 60
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getPrice() 0 10 1
A extractLogicFromSerializedPrices() 0 12 3
A normalizePrice() 0 18 6
1
<?php
2
declare(strict_types=1);
3
4
namespace Ticaje\BookingApi\Application\Service\Provider\AgencyCar;
5
6
use Ticaje\BookingApi\Application\Service\Provider\BasePriceProvider;
7
use Ticaje\BookingApi\Application\Signatures\Provider\PriceProviderSignature;
8
use Ticaje\Hexagonal\Application\Signatures\UseCase\UseCaseCommandInterface;
9
10
/**
11
 * Class Price
12
 * @package Ticaje\BookingApi\Application\Service\Provider\AgencyCar
13
 * Low level details kingdom.
14
 */
15
class Price extends BasePriceProvider implements PriceProviderSignature
16
{
17
    /**
18
     * @inheritDoc
19
     */
20
    public function getPrice(UseCaseCommandInterface $command): float
21
    {
22
        /** @var string $indexed */
23
        $indexed = (string)$this->instance->getSeasonBasedPrices();
0 ignored issues
show
Bug introduced by
The method getSeasonBasedPrices() does not exist on Ticaje\Contract\Persistence\Entity\EntityInterface. ( Ignorable by Annotation )

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

23
        $indexed = (string)$this->instance->/** @scrutinizer ignore-call */ getSeasonBasedPrices();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
24
        $days = $this->calculateDays($command);
25
        $key = "{$indexed}";
26
        $price = $this->extractLogicFromSerializedPrices($command->getProduct()->getData($key), $days->days);
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

26
        $price = $this->extractLogicFromSerializedPrices($command->/** @scrutinizer ignore-call */ getProduct()->getData($key), $days->days);
Loading history...
27
        $price *= (int)$days->days;
28
29
        return (float)$price;
30
    }
31
32
    /**
33
     * @param $unserialized
34
     * @param $days
35
     *
36
     * @return |null
0 ignored issues
show
Documentation Bug introduced by
The doc comment |null at position 0 could not be parsed: Unknown type name '|' at position 0 in |null.
Loading history...
37
     */
38
    private function extractLogicFromSerializedPrices($unserialized, $days)
39
    {
40
        $rules = $this->normalizePrice($unserialized);
41
        foreach ($rules as $range => $price) {
42
            $range = explode('..', $range);
43
            $storeDays = range($range[0], $range[1]);
44
            if (in_array($days, $storeDays)) {
45
                return $price;
46
            }
47
        }
48
49
        return null;
50
    }
51
52
    /**
53
     * @param $value
54
     *
55
     * @return array
56
     */
57
    private function normalizePrice($value): array
58
    {
59
        $normalizedArray = [];
60
        if (!$value) {// Guard clause
61
            return [];
62
        }
63
        $values = explode(';', $value);
64
        if (empty($values)) {// Guard clause
65
            return [];
66
        }
67
        foreach ($values as $val) {
68
            $indexValue = explode('=', $val);
69
            if (isset($indexValue[0]) && $indexValue[0]) {
70
                $normalizedArray[$indexValue[0]] = $indexValue[1];
71
            }
72
        }
73
74
        return $normalizedArray;
75
    }
76
}
77