InsuranceFactory   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 14
c 2
b 0
f 0
lcom 1
cbo 7
dl 0
loc 83
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
B create() 0 26 5
A areEmpty() 0 6 3
A hasValidDateFormat() 0 7 3
A isEqualToOneYear() 0 8 3
1
<?php
2
3
namespace Madkom\RegistryApplication\Domain\CarManagement\Insurances;
4
5
use Madkom\RegistryApplication\Domain\CarManagement\CarExceptions\InvalidDatesException;
6
use Madkom\RegistryApplication\Domain\CarManagement\Insurances\Exceptions\EmptyInsuranceDateException;
7
use Madkom\RegistryApplication\Domain\CarManagement\Insurances\Exceptions\UnknownInsuranceTypeException;
8
9
/**
10
 * Class InsuranceFactory.
11
 */
12
class InsuranceFactory
13
{
14
    /**
15
     * @param string $insuranceType
16
     * @param \DateTime $dateFrom
17
     * @param \DateTime $dateTo
18
     * @param string $insuranceId
19
     * @param string $insurerId
20
     *
21
     * @throws \Madkom\RegistryApplication\Domain\CarManagement\Insurances\Exceptions\EmptyInsuranceDateException
22
     * @throws \Madkom\RegistryApplication\Domain\CarManagement\Insurances\Exceptions\UnknownInsuranceTypeException
23
     * @throws \InvalidArgumentException
24
     * @throws \Madkom\RegistryApplication\Domain\CarManagement\CarExceptions\InvalidDatesException
25
     *
26
     * @return Insurance
27
     */
28
    public function create($insuranceId, $insuranceType, $dateFrom, $dateTo, $insurerId)
29
    {
30
        $this->areEmpty($dateFrom, $dateTo);
31
        $this->hasValidDateFormat($dateFrom, $dateTo);
32
        $this->isEqualToOneYear($dateFrom, $dateTo);
33
34
        switch ($insuranceType) {
35
            case CarInsurance::INSURANCE_TYPE:
36
                $insurance = new CarInsurance($insuranceId, $dateFrom, $dateTo, $insurerId);
37
                break;
38
            case AccidentInsurance::INSURANCE_TYPE:
39
                $insurance = new AccidentInsurance($insuranceId, $dateFrom, $dateTo, $insurerId);
40
                break;
41
            case AssistanceInsurance::INSURANCE_TYPE:
42
                $insurance = new AssistanceInsurance($insuranceId, $dateFrom, $dateTo, $insurerId);
43
                break;
44
            case LiabilityInsurance::INSURANCE_TYPE:
45
                $insurance = new LiabilityInsurance($insuranceId, $dateFrom, $dateTo, $insurerId);
46
                break;
47
            default:
48
                throw new UnknownInsuranceTypeException('Unknown insurance type: '.$insuranceType);
49
                break;
0 ignored issues
show
Unused Code introduced by
break; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
50
        }
51
52
        return $insurance;
53
    }
54
55
    /**
56
     * @param \DateTime $dateFrom
57
     * @param \DateTime $dateTo
58
     *
59
     * @throws \Madkom\RegistryApplication\Domain\CarManagement\Insurances\Exceptions\EmptyInsuranceDateException
60
     */
61
    private function areEmpty($dateFrom, $dateTo)
62
    {
63
        if ($dateFrom === '' or $dateTo === '') {
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as or instead of || is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
64
            throw new EmptyInsuranceDateException('The \'date from\' or \'date to\' is empty');
65
        }
66
    }
67
68
    /**
69
     * @param \DateTime $dateFrom
70
     * @param \DateTime $dateTo
71
     *
72
     * @throws \InvalidArgumentException
73
     */
74
    private function hasValidDateFormat($dateFrom, $dateTo)
75
    {
76
        if (!($dateFrom instanceof \DateTime) or !($dateTo instanceof \DateTime)) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as or instead of || is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
77
            throw new \InvalidArgumentException('The \'date from\' or \'date to\' has not be instance of DateTime class.'
78
            );
79
        }
80
    }
81
82
    /**
83
     * @param \DateTime $dateFrom
84
     * @param \DateTime $dateTo
85
     */
86
    private function isEqualToOneYear($dateFrom, $dateTo)
87
    {
88
        $interval = $dateTo->diff($dateFrom);
89
90
        if ($interval->days < 365 or $interval->days > 366) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as or instead of || is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
91
            throw new InvalidDatesException('Umowa z ubezpieczeniem może być tylko na rok.');
92
        }
93
    }
94
}
95