MessyPhysicalAddress::equals()   F
last analyzed

Complexity

Conditions 20
Paths 1465

Size

Total Lines 24
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 20
eloc 16
nc 1465
nop 1
dl 0
loc 24
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 InvalidArgumentException;
8
use Talentify\ValueObject\Geography\Address\City;
9
use Talentify\ValueObject\Geography\Address\Country;
10
use Talentify\ValueObject\Geography\Address\PostalCode;
11
use Talentify\ValueObject\Geography\Address\Region;
12
use Talentify\ValueObject\Geography\Address\Street;
13
use Talentify\ValueObject\StringUtils;
14
use Talentify\ValueObject\ValueObject;
15
use function count;
16
use function is_object;
17
18
/**
19
 * A confuse physical address.
20
 *
21
 * Example:
22
 * - new MessyPhysicalAddress('400 Broad St, Seattle')
23
 * - new MessyPhysicalAddress('400 Broad St', new City('Seattle'), new Region('Washington', 'WA'), CountryList::US())
24
 */
25
class MessyPhysicalAddress implements PhysicalAddress
26
{
27
    /** @var string */
28
    private $messyAddress;
29
    /** @var \Talentify\ValueObject\Geography\Address\City|null */
30
    private $city;
31
    /** @var \Talentify\ValueObject\Geography\Address\Region|null */
32
    private $region;
33
    /** @var \Talentify\ValueObject\Geography\Address\Country|null */
34
    private $country;
35
    /** @var string */
36
    private $formattedAddress;
37
38
    /**
39
     * @throws \InvalidArgumentException
40
     */
41
    public function __construct(
42
        string $messyAddress,
43
        ?City $city = null,
44
        ?Region $region = null,
45
        ?Country $country = null
46
    ) {
47
        $this->city    = $city;
48
        $this->region  = $region;
49
        $this->country = $country;
50
        $this->setMessyAddress($messyAddress);
51
        $this->setFormattedAddress();
52
    }
53
54
    protected function setMessyAddress(string $messyAddress) : void
55
    {
56
        $normalized = StringUtils::trimSpacesWisely($messyAddress);
57
        if ($normalized === null || $normalized === '') {
58
            throw new InvalidArgumentException(sprintf('The given value "%s" is an empty string.', $messyAddress));
59
        }
60
61
        $this->messyAddress = StringUtils::convertCaseToTitle($normalized);
62
    }
63
64
    protected function setFormattedAddress() : void
65
    {
66
        $notNull = array_filter([$this->city, $this->region, $this->country], function ($element) {
67
            return $element !== null;
68
        });
69
70
        if (count($notNull) === 0) {
71
            $this->formattedAddress = $this->messyAddress;
72
            return;
73
        }
74
75
        $secondSegment = implode(', ', $notNull);
76
        if (stripos($this->messyAddress, $secondSegment) !== false) {
77
            $this->formattedAddress = $this->messyAddress;
78
            return;
79
        }
80
81
        $this->formattedAddress = sprintf('%s, %s', $this->messyAddress, $secondSegment);
82
    }
83
84
    public function getMessyAddress() : string
85
    {
86
        return $this->messyAddress;
87
    }
88
89
    /**
90
     * {@inheritDoc}
91
     */
92
    public function getStreet() : ?Street
93
    {
94
        return null;
95
    }
96
97
    /**
98
     * {@inheritDoc}
99
     */
100
    public function getCity() : ?City
101
    {
102
        return $this->city;
103
    }
104
105
    /**
106
     * {@inheritDoc}
107
     */
108
    public function getRegion() : ?Region
109
    {
110
        return $this->region;
111
    }
112
113
    /**
114
     * {@inheritDoc}
115
     */
116
    public function getPostalCode() : ?PostalCode
117
    {
118
        return null;
119
    }
120
121
    /**
122
     * {@inheritDoc}
123
     */
124
    public function getCountry() : ?Country
125
    {
126
        return $this->country;
127
    }
128
129
    /**
130
     * {@inheritDoc}
131
     */
132
    public function getAddress() : string
133
    {
134
        return $this->formattedAddress;
135
    }
136
137
    /**
138
     * {@inheritDoc}
139
     */
140
    public function equals(?ValueObject $object) : bool
141
    {
142
        if (!$object instanceof self) {
143
            return false;
144
        }
145
146
        return (
147
            (
148
                strtolower($this->getMessyAddress()) === strtolower($object->getMessyAddress())
149
            ) &&
150
            (
151
                (null === $this->getCity() && null === $object->getCity()) ||
152
                (is_object($this->getCity()) && $this->getCity()->equals($object->getCity())) ||
153
                (is_object($object->getCity()) && $object->getCity()->equals($this->getCity()))
154
            ) &&
155
            (
156
                (null === $this->getRegion() && null === $object->getRegion()) ||
157
                (is_object($this->getRegion()) && $this->getRegion()->equals($object->getRegion())) ||
158
                (is_object($object->getRegion()) && $object->getRegion()->equals($this->getRegion()))
159
            ) &&
160
            (
161
                (null === $this->getCountry() && null === $object->getCountry()) ||
162
                (is_object($this->getCountry()) && $this->getCountry()->equals($object->getCountry())) ||
163
                (is_object($object->getCountry()) && $object->getCountry()->equals($this->getCountry()))
164
            )
165
        );
166
    }
167
168
    /**
169
     * {@inheritDoc}
170
     */
171
    public function __toString() : string
172
    {
173
        return $this->getAddress();
174
    }
175
}
176