GenericPhysicalAddress::equals()   F
last analyzed

Complexity

Conditions 31
Paths > 20000

Size

Total Lines 31
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 31
eloc 22
nc 162516
nop 1
dl 0
loc 31
rs 0
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
3
declare(strict_types=1);
4
5
namespace Talentify\ValueObject\Geography\Address\Physical;
6
7
use Talentify\ValueObject\Geography\Address\City;
8
use Talentify\ValueObject\Geography\Address\Country;
9
use Talentify\ValueObject\Geography\Address\PostalCode;
10
use Talentify\ValueObject\Geography\Address\Region;
11
use Talentify\ValueObject\Geography\Address\Street;
12
use Talentify\ValueObject\StringUtils;
13
use Talentify\ValueObject\ValueObject;
14
15
/**
16
 * A generic structure for a physical address.
17
 */
18
class GenericPhysicalAddress implements PhysicalAddress
19
{
20
    /** @var \Talentify\ValueObject\Geography\Address\Street|null */
21
    protected $street;
22
    /** @var \Talentify\ValueObject\Geography\Address\City|null */
23
    protected $city;
24
    /** @var \Talentify\ValueObject\Geography\Address\Region|null */
25
    protected $region;
26
    /** @var \Talentify\ValueObject\Geography\Address\PostalCode|null */
27
    protected $postalCode;
28
    /** @var \Talentify\ValueObject\Geography\Address\Country|null */
29
    protected $country;
30
    /** @var string */
31
    protected $formattedAddress;
32
33
    /**
34
     * @throws \InvalidArgumentException
35
     */
36
    public function __construct(
37
        ?Street $street = null,
38
        ?City $city = null,
39
        ?Region $region = null,
40
        ?PostalCode $postalCode = null,
41
        ?Country $country = null,
42
        ?string $formattedAddress = null
43
    ) {
44
        $this->street           = $street;
45
        $this->city             = $city;
46
        $this->region           = $region;
47
        $this->postalCode       = $postalCode;
48
        $this->country          = $country;
49
        $this->formattedAddress = $formattedAddress;
50
    }
51
52
    public function getStreet() : ?Street
53
    {
54
        return $this->street;
55
    }
56
57
    public function getCity() : ?City
58
    {
59
        return $this->city;
60
    }
61
62
    public function getRegion() : ?Region
63
    {
64
        return $this->region;
65
    }
66
67
    public function getCountry() : ?Country
68
    {
69
        return $this->country;
70
    }
71
72
    public function getPostalCode() : ?PostalCode
73
    {
74
        return $this->postalCode;
75
    }
76
77
    public function equals(?ValueObject $object) : bool
78
    {
79
        if (!$object instanceof self) {
80
            return false;
81
        }
82
83
        return (
84
            (
85
                (null === $this->getStreet() && null === $object->getStreet()) ||
86
                (\is_object($this->getStreet()) && $this->getStreet()->equals($object->getStreet())) ||
87
                (\is_object($object->getStreet()) && $object->getStreet()->equals($this->getStreet()))
88
            ) &&
89
            (
90
                (null === $this->getCity() && null === $object->getCity()) ||
91
                (\is_object($this->getCity()) && $this->getCity()->equals($object->getCity())) ||
92
                (\is_object($object->getCity()) && $object->getCity()->equals($this->getCity()))
93
            ) &&
94
            (
95
                (null === $this->getRegion() && null === $object->getRegion()) ||
96
                (\is_object($this->getRegion()) && $this->getRegion()->equals($object->getRegion())) ||
97
                (\is_object($object->getRegion()) && $object->getRegion()->equals($this->getRegion()))
98
            ) &&
99
            (
100
                (null === $this->getPostalCode() && null === $object->getPostalCode()) ||
101
                (\is_object($this->getPostalCode()) && $this->getPostalCode()->equals($object->getPostalCode())) ||
102
                (\is_object($object->getPostalCode()) && $object->getPostalCode()->equals($this->getPostalCode()))
103
            ) &&
104
            (
105
                (null === $this->getCountry() && null === $object->getCountry()) ||
106
                (\is_object($this->getCountry()) && $this->getCountry()->equals($object->getCountry())) ||
107
                (\is_object($object->getCountry()) && $object->getCountry()->equals($this->getCountry()))
108
            )
109
        );
110
    }
111
112
    public function getAddress() : string
113
    {
114
        if ($this->formattedAddress !== null) {
115
            return $this->formattedAddress;
116
        }
117
118
        $street     = $this->getStreet() ? $this->getStreet()->getFormatted() : '';
119
        $city       = $this->getCity() ? $this->getCity()->getFormatted() : '';
120
        $region     = $this->getRegion() ? $this->getRegion()->__toString() : '';
121
        $postalCode = $this->getPostalCode() ? $this->getPostalCode()->getFormatted() : '';
122
        $country    = $this->getCountry() ? $this->getCountry()->__toString() : '';
123
124
        $values = [$street, $city, $region, $postalCode, $country];
125
        $nonEmptyValues = [];
126
        foreach ($values as $value) {
127
            if($value !== ''){
128
                $nonEmptyValues[] = $value;
129
            }
130
        }
131
132
        $formatted = implode(', ', $nonEmptyValues);
133
134
        return StringUtils::trimSpacesWisely($formatted) ?? '';
135
    }
136
137
    public function __toString() : string
138
    {
139
        return $this->getAddress();
140
    }
141
}
142