Passed
Branch feature/refactoring (13cbf0)
by Alexey
03:46
created

App::getIcon()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * @author   Ne-Lexa
7
 * @license  MIT
8
 *
9
 * @see      https://github.com/Ne-Lexa/google-play-scraper
10
 */
11
12
namespace Nelexa\GPlay\Model;
13
14
use Nelexa\GPlay\GPlayApps;
15
use Nelexa\GPlay\Model\Builder\AppBuilder;
16
17
/**
18
 * Contains basic information about the application from the Google Play store.
19
 *
20
 * Some sections, such as search, similar applications, applications categories, etc. display
21
 * a list of applications with limited data. To get detailed information about the application,
22
 * you must call the appropriate method in the class {@see GPlayApps}.
23
 *
24
 * @see AppId Application ID, application locale and country.
25
 * @see GPlayApps::search() Returns a list of applications from the Google Play
26
 *     store for a search query.
27
 * @see GPlayApps::getSimilarApps() Returns a list of similar applications in
28
 *     the Google Play store.
29
 * @see GPlayApps::getDeveloperApps() Returns a list of developer applications
30
 *     in the Google Play store.
31
 * @see GPlayApps::getTopApps() Returns a list of applications with basic
32
 *     information from the category and collection of the Google Play store.
33
 */
34
class App extends AppId implements \JsonSerializable
35
{
36
    use JsonSerializableTrait;
37
38
    /** @var string Application name. */
39
    private $name;
40
41
    /** @var string|null Application summary. */
42
    private $summary;
43
44
    /** @var Developer Application developer. */
45
    private $developer;
46
47
    /** @var GoogleImage Application icon. */
48
    private $icon;
49
50
    /** @var float Application rating on a five-point scale. */
51
    private $score;
52
53
    /** @var string|null Price of the application or null if it is free. */
54
    private $priceText;
55
56
    /**
57
     * Creates an object containing basic information about the Android application.
58
     *
59
     * @param AppBuilder $builder application builder
60
     *
61
     * @ignore
62
     */
63 53
    public function __construct(AppBuilder $builder)
64
    {
65 53
        if (empty($builder->getId())) {
66 1
            throw new \InvalidArgumentException(
67 1
                'Application ID cannot be null or empty. Solution: $appBuilder->setId(...);'
68
            );
69
        }
70
71 53
        if (empty($builder->getLocale())) {
72 1
            throw new \InvalidArgumentException(
73 1
                'Locale cannot be null or empty. Solution: $appBuilder->setLocale(...);'
74
            );
75
        }
76
77 53
        if (empty($builder->getCountry())) {
78 1
            throw new \InvalidArgumentException(
79 1
                'Country cannot be null or empty. Solution: $appBuilder->setCountry(...);'
80
            );
81
        }
82
83 53
        if (empty($builder->getName())) {
84 1
            throw new \InvalidArgumentException(
85 1
                'The application name cannot be null or empty. Solution: $appBuilder->setName(...);'
86
            );
87
        }
88
89 53
        if ($builder->getDeveloper() === null) {
90 1
            throw new \InvalidArgumentException(
91 1
                'Application developer cannot be null. Solution: $appBuilder->setDeveloper(...);'
92
            );
93
        }
94
95 53
        if ($builder->getIcon() === null) {
96 1
            throw new \InvalidArgumentException(
97 1
                'Application icon cannot be null. Solution: $appBuilder->setIcon(...);'
98
            );
99
        }
100
101 53
        parent::__construct(
102 53
            $builder->getId(),
103 53
            $builder->getLocale(),
104 53
            $builder->getCountry()
105
        );
106
107 53
        $this->name = $builder->getName();
108 53
        $this->summary = $builder->getSummary();
109 53
        $this->developer = $builder->getDeveloper();
110 53
        $this->icon = $builder->getIcon();
111 53
        $this->score = $builder->getScore();
112 53
        $this->priceText = $builder->getPriceText();
113 53
    }
114
115
    /**
116
     * Returns application name.
117
     *
118
     * @return string application name
119
     */
120 15
    public function getName(): string
121
    {
122 15
        return $this->name;
123
    }
124
125
    /**
126
     * Returns application summary.
127
     *
128
     * @return string|null application summary
129
     */
130 1
    public function getSummary(): ?string
131
    {
132 1
        return $this->summary;
133
    }
134
135
    /**
136
     * Returns application developer.
137
     *
138
     * @return Developer application developer
139
     */
140 3
    public function getDeveloper(): Developer
141
    {
142 3
        return $this->developer;
143
    }
144
145
    /**
146
     * Returns application icon.
147
     *
148
     * @return GoogleImage application icon
149
     */
150 13
    public function getIcon(): GoogleImage
151
    {
152 13
        return $this->icon;
153
    }
154
155
    /**
156
     * Returns application rating on a five-point scale.
157
     *
158
     * @return float rating application
159
     */
160 1
    public function getScore(): float
161
    {
162 1
        return $this->score;
163
    }
164
165
    /**
166
     * Returns the price of the application.
167
     *
168
     * @return string|null application price or null if it is free
169
     */
170 1
    public function getPriceText(): ?string
171
    {
172 1
        return $this->priceText;
173
    }
174
175
    /**
176
     * Checks that this application is free.
177
     *
178
     * @return bool `true` if the application is free and `false` if paid
179
     */
180 1
    public function isFree(): bool
181
    {
182 1
        return $this->priceText === null;
183
    }
184
185
    /**
186
     * Creates a new application builder.
187
     *
188
     * @return AppBuilder application builder
189
     * @ignore
190
     */
191 53
    public static function newBuilder(): AppBuilder
192
    {
193 53
        return new AppBuilder();
194
    }
195
196
    /**
197
     * Returns class properties as an array.
198
     *
199
     * @return array class properties as an array
200
     */
201
    public function asArray(): array
202
    {
203
        return [
204
            'id' => $this->getId(),
205
            'url' => $this->getUrl(),
206
            'locale' => $this->getLocale(),
207
            'country' => $this->getCountry(),
208
            'name' => $this->name,
209
            'summary' => $this->summary,
210
            'developer' => $this->developer->asArray(),
211
            'icon' => $this->icon->getUrl(),
212
            'score' => $this->score,
213
            'priceText' => $this->priceText,
214
        ];
215
    }
216
}
217