Passed
Branch develop (2fd4b5)
by Alexey
01:46
created

Developer::asArray()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 12
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 10
nc 4
nop 0
dl 0
loc 12
ccs 0
cts 10
cp 0
crap 12
rs 9.9332
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
/**
5
 * @author   Ne-Lexa
6
 * @license  MIT
7
 * @link     https://github.com/Ne-Lexa/google-play-scraper
8
 */
9
10
namespace Nelexa\GPlay\Model;
11
12
use Nelexa\GPlay\Model\Builder\DeveloperBuilder;
13
14
/**
15
 * Contains data on the application developer in the Google Play store.
16
 *
17
 * @see \Nelexa\GPlay\GPlayApps::getDeveloperInfo() Returns information about the
18
 *     developer: name, icon, cover, description and website address.
19
 * @see \Nelexa\GPlay\GPlayApps::getDeveloperInfoInLocales() Returns information
20
 *     about the developer for the locale array.
21
 * @see \Nelexa\GPlay\GPlayApps::getApp() Returns detailed information about the
22
 *     Android application from the Google Play store.
23
 * @see \Nelexa\GPlay\GPlayApps::getApps() Returns detailed information about
24
 *     many android packages.
25
 * @see \Nelexa\GPlay\GPlayApps::getAppInLocales() Returns detailed information
26
 *     about an application from the Google Play store for an array of locales.
27
 * @see \Nelexa\GPlay\GPlayApps::getAppInAvailableLocales() Returns detailed
28
 *     information about the application in all available locales.
29
 */
30
class Developer implements \JsonSerializable
31
{
32
    use JsonSerializableTrait;
33
34
    /** @var string Developer id */
35
    private $id;
36
37
    /** @var string url Developer page url in Google Play store */
38
    private $url;
39
40
    /** @var string Developer name */
41
    private $name;
42
43
    /** @var string|null Description of the developer */
44
    private $description;
45
46
    /** @var string|null Developer website */
47
    private $website;
48
49
    /** @var GoogleImage|null Developer icon */
50
    private $icon;
51
52
    /** @var GoogleImage|null Developer cover */
53
    private $cover;
54
55
    /** @var string|null Developer email */
56
    private $email;
57
58
    /** @var string|null Developer address */
59
    private $address;
60
61
    /**
62
     * Creates an object with information about the developer of Google Play.
63
     *
64
     * @param DeveloperBuilder $builder Developer builder.
65
     *
66
     * @throws \InvalidArgumentException
67
     *
68
     * @internal
69
     */
70 16
    public function __construct(DeveloperBuilder $builder)
71
    {
72 16
        $this->id = $builder->getId();
73 16
        $this->url = $builder->getUrl();
74 16
        $this->name = $builder->getName();
75 16
        $this->description = $builder->getDescription();
76 16
        $this->website = $builder->getWebsite();
77 16
        $this->icon = $builder->getIcon();
78 16
        $this->cover = $builder->getCover();
79 16
        $this->email = $builder->getEmail();
80 16
        $this->address = $builder->getAddress();
81
82 16
        if (empty($this->id)) {
83 1
            throw new \InvalidArgumentException('Developer id cannot be null or empty. ' .
84 1
                'Solution: $developerBuilder->setId(...);');
85
        }
86 16
        if (empty($this->url)) {
87 1
            throw new \InvalidArgumentException('Developer url cannot be null or empty. ' .
88 1
                'Solution: $developerBuilder->setUrl(...);');
89
        }
90 16
        if (empty($this->name)) {
91 1
            throw new \InvalidArgumentException('Developer name cannot be null or empty. ' .
92 1
                'Solution: $developerBuilder->setName(...);');
93
        }
94 16
    }
95
96
    /**
97
     * Returns developer id.
98
     *
99
     * @return string Developer id.
100
     */
101 5
    public function getId(): string
102
    {
103 5
        return $this->id;
104
    }
105
106
    /**
107
     * Returns the URL of the developer’s page in Google Play.
108
     *
109
     * @return string Developer page url.
110
     */
111 3
    public function getUrl(): string
112
    {
113 3
        return $this->url;
114
    }
115
116
    /**
117
     * Returns the name of the developer.
118
     *
119
     * @return string Developer name
120
     */
121 3
    public function getName(): string
122
    {
123 3
        return $this->name;
124
    }
125
126
    /**
127
     * Returns a description of the developer.
128
     *
129
     * @return string|null Description of the developer or `null`.
130
     */
131 3
    public function getDescription(): ?string
132
    {
133 3
        return $this->description;
134
    }
135
136
    /**
137
     * Returns the developer's website.
138
     *
139
     * @return string|null Developer website or `null`.
140
     */
141 3
    public function getWebsite(): ?string
142
    {
143 3
        return $this->website;
144
    }
145
146
    /**
147
     * Returns the developer icon.
148
     *
149
     * @return GoogleImage|null Developer icon or `null`.
150
     */
151 3
    public function getIcon(): ?GoogleImage
152
    {
153 3
        return $this->icon;
154
    }
155
156
    /**
157
     * Returns the developer cover.
158
     *
159
     * @return GoogleImage|null Developer cover or `null`.
160
     */
161 3
    public function getCover(): ?GoogleImage
162
    {
163 3
        return $this->cover;
164
    }
165
166
    /**
167
     * Returns developer email.
168
     *
169
     * @return string|null Developer email or `null`.
170
     */
171 3
    public function getEmail(): ?string
172
    {
173 3
        return $this->email;
174
    }
175
176
    /**
177
     * Returns the address of the developer.
178
     *
179
     * @return string|null Developer address or `null`.
180
     */
181 3
    public function getAddress(): ?string
182
    {
183 3
        return $this->address;
184
    }
185
186
    /**
187
     * Creates a new developer builder.
188
     *
189
     * @return DeveloperBuilder Developer builder.
190
     *
191
     * @internal
192
     */
193 16
    public static function newBuilder(): DeveloperBuilder
194
    {
195 16
        return new DeveloperBuilder();
196
    }
197
198
    /**
199
     * Checks for equality of developer.
200
     *
201
     * @param Developer $otherDeveloper Developer with which is compared.
202
     *
203
     * @return bool `true` if the contents of the objects being changed are the same
204
     *     and `false` if the objects contain different data.
205
     *
206
     * @internal
207
     */
208
    public function equals(Developer $otherDeveloper): bool
209
    {
210
        if ($this->id !== $otherDeveloper->id) {
211
            return false;
212
        }
213
        if ($this->name !== $otherDeveloper->name) {
214
            return false;
215
        }
216
        if ($this->description !== $otherDeveloper->description) {
217
            return false;
218
        }
219
220
        if ($this->icon !== null && $otherDeveloper->icon !== null) {
221
            if ($this->icon->getOriginalSizeUrl() !== $otherDeveloper->icon->getOriginalSizeUrl()) {
222
                return false;
223
            }
224
        } elseif ($this->icon !== $otherDeveloper->icon) {
225
            return false;
226
        }
227
228
        if ($this->cover !== null && $otherDeveloper->cover !== null) {
229
            if ($this->cover->getOriginalSizeUrl() !== $otherDeveloper->cover->getOriginalSizeUrl()) {
230
                return false;
231
            }
232
        } elseif ($this->cover !== $otherDeveloper->cover) {
233
            return false;
234
        }
235
236
        return true;
237
    }
238
239
    /**
240
     * Returns class properties as an array.
241
     *
242
     * @return array Class properties as an array.
243
     */
244
    public function asArray(): array
245
    {
246
        return [
247
            'id' => $this->id,
248
            'url' => $this->url,
249
            'name' => $this->name,
250
            'description' => $this->description,
251
            'website' => $this->website,
252
            'icon' => $this->icon !== null ? $this->icon->getUrl() : null,
253
            'cover' => $this->cover !== null ? $this->cover->getUrl() : null,
254
            'email' => $this->email,
255
            'address' => $this->address,
256
        ];
257
    }
258
}
259