createFromArray()   B
last analyzed

Complexity

Conditions 10
Paths 128

Size

Total Lines 33
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 10
eloc 23
nc 128
nop 1
dl 0
loc 33
rs 7.4333
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
declare(strict_types=1);
3
4
namespace TK\API\ValueObject\Factory;
5
6
use DateTimeImmutable;
7
use TK\API\Exception\InvalidArgumentException;
8
use TK\API\ValueObject\CalculateFlightMilesParameters;
9
10
class CalculateFlightMilesParametersFactory implements ValueObjectFactoryInterface
11
{
12
    /**
13
     * @param array $parameters
14
     * @return CalculateFlightMilesParameters
15
     * @throws \Exception
16
     */
17
    public static function createFromArray(array $parameters) : CalculateFlightMilesParameters
18
    {
19
        $calculateFlightMilesParameters = new CalculateFlightMilesParameters(
20
            $parameters['origin'],
21
            $parameters['destination']
22
        );
23
        if (array_key_exists('cabin_code', $parameters) && $parameters['cabin_code'] === 'Y') {
24
            $calculateFlightMilesParameters = $calculateFlightMilesParameters->withCabinCode();
25
        }
26
        if (array_key_exists('class_code', $parameters) && $parameters['class_code'] === 'Y') {
27
            $calculateFlightMilesParameters = $calculateFlightMilesParameters->withClassCode();
28
        }
29
        if (array_key_exists('marketingClassCode', $parameters)) {
30
            $calculateFlightMilesParameters = $calculateFlightMilesParameters
31
                ->withMarketingClassCode($parameters['marketingClassCode']);
32
        }
33
        if (array_key_exists('card_type', $parameters)) {
34
            $calculateFlightMilesParameters = $calculateFlightMilesParameters
35
                ->withCardType($parameters['card_type']);
36
        }
37
        if (array_key_exists('flightDate', $parameters)) {
38
            $calculateFlightMilesParameters = $calculateFlightMilesParameters
39
                ->withFlightDate(DateTimeImmutable::createFromFormat('d.m.Y', $parameters['flightDate']));
0 ignored issues
show
Bug introduced by
It seems like DateTimeImmutable::creat...rameters['flightDate']) can also be of type false; however, parameter $flightDate of TK\API\ValueObject\Calcu...eters::withFlightDate() does only seem to accept DateTimeImmutable, maybe add an additional type check? ( Ignorable by Annotation )

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

39
                ->withFlightDate(/** @scrutinizer ignore-type */ DateTimeImmutable::createFromFormat('d.m.Y', $parameters['flightDate']));
Loading history...
40
        }
41
        if (array_key_exists('operatingFlightNumber', $parameters)) {
42
            $calculateFlightMilesParameters = $calculateFlightMilesParameters
43
                ->withOperatingFlightNumber($parameters['operatingFlightNumber']);
44
        }
45
        if (array_key_exists('marketingFlightNumber', $parameters)) {
46
            $calculateFlightMilesParameters = $calculateFlightMilesParameters
47
                ->withMarketingFlightNumber($parameters['marketingFlightNumber']);
48
        }
49
        return $calculateFlightMilesParameters;
50
    }
51
52
53
    /**
54
     * @param string $json
55
     * @return CalculateFlightMilesParameters
56
     * @throws InvalidArgumentException
57
     * @throws \Exception
58
     */
59
    public static function createFromJson(string $json) : CalculateFlightMilesParameters
60
    {
61
        $parameters = json_decode($json, (bool) JSON_OBJECT_AS_ARRAY);
62
        if (json_last_error() !== JSON_ERROR_NONE) {
63
            throw new InvalidArgumentException(
64
                'CalculateFlightMilesParametersFactory Error: ' . json_last_error_msg()
65
            );
66
        }
67
        return self::createFromArray($parameters);
68
    }
69
}
70