Passed
Branch develop (29ed49)
by Alexey
01:58
created

App::getSummary()   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 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
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\AppBuilder;
13
14
/**
15
 * Contains basic information about the application from the Google Play store.
16
 *
17
 * Some sections, such as search, similar applications, applications categories, etc. display
18
 * a list of applications with limited data. To get detailed information about the application,
19
 * you must call the appropriate method in the class {@see \Nelexa\GPlay\GPlayApps}.
20
 *
21
 * @see \Nelexa\GPlay\Model\AppId Application ID, application locale and country.
22
 * @see \Nelexa\GPlay\GPlayApps::search() Returns a list of applications from the Google Play
23
 *     store for a search query.
24
 * @see \Nelexa\GPlay\GPlayApps::getSimilarApps() Returns a list of similar applications in
25
 *     the Google Play store.
26
 * @see \Nelexa\GPlay\GPlayApps::getDeveloperApps() Returns a list of developer applications
27
 *     in the Google Play store.
28
 * @see \Nelexa\GPlay\GPlayApps::getAppsByCategory() Returns a list of applications with basic
29
 *     information from the category and collection of the Google Play store.
30
 */
31
class App extends AppId implements \JsonSerializable
32
{
33
    use JsonSerializableTrait;
34
35
    /** @var string Application name. */
36
    private $name;
37
38
    /** @var string|null Application summary. */
39
    private $summary;
40
41
    /** @var Developer Application developer. */
42
    private $developer;
43
44
    /** @var GoogleImage Application icon. */
45
    private $icon;
46
47
    /** @var float Application rating on a five-point scale. */
48
    private $score;
49
50
    /** @var string|null Price of the application or null if it is free. */
51
    private $priceText;
52
53
    /**
54
     * Creates an object containing basic information about the Android application.
55
     *
56
     * @param AppBuilder $builder Application builder.
57
     *
58
     * @throws \InvalidArgumentException If not enough required data in the builder.
59
     *
60
     * @ignore
61
     */
62 13
    public function __construct(AppBuilder $builder)
63
    {
64 13
        $this->validateBuilder($builder);
65
66 13
        parent::__construct(
67 13
            $builder->getId(),
0 ignored issues
show
Bug introduced by
It seems like $builder->getId() can also be of type null; however, parameter $id of Nelexa\GPlay\Model\AppId::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

67
            /** @scrutinizer ignore-type */ $builder->getId(),
Loading history...
68 13
            $builder->getLocale(),
0 ignored issues
show
Bug introduced by
It seems like $builder->getLocale() can also be of type null; however, parameter $locale of Nelexa\GPlay\Model\AppId::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

68
            /** @scrutinizer ignore-type */ $builder->getLocale(),
Loading history...
69 13
            $builder->getCountry()
0 ignored issues
show
Bug introduced by
It seems like $builder->getCountry() can also be of type null; however, parameter $country of Nelexa\GPlay\Model\AppId::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

69
            /** @scrutinizer ignore-type */ $builder->getCountry()
Loading history...
70
        );
71 13
        $this->name = $builder->getName();
72 13
        $this->summary = $builder->getSummary();
73 13
        $this->developer = $builder->getDeveloper();
74 13
        $this->icon = $builder->getIcon();
75 13
        $this->score = $builder->getScore();
76 13
        $this->priceText = $builder->getPriceText();
77 13
    }
78
79
    /**
80
     * @param AppBuilder $builder
81
     */
82 13
    private function validateBuilder(AppBuilder $builder): void
83
    {
84 13
        if (empty($builder->getId())) {
85 1
            throw new \InvalidArgumentException('Application ID cannot be null or empty. Solution: $appBuilder->setId(...);');
86
        }
87 13
        if (empty($builder->getLocale())) {
88 1
            throw new \InvalidArgumentException('Locale cannot be null or empty. Solution: $appBuilder->setLocale(...);');
89
        }
90 13
        if (empty($builder->getCountry())) {
91 1
            throw new \InvalidArgumentException('Country cannot be null or empty. Solution: $appBuilder->setCountry(...);');
92
        }
93 13
        if (empty($builder->getName())) {
94 1
            throw new \InvalidArgumentException('The application name cannot be null or empty. Solution: $appBuilder->setName(...);');
95
        }
96 13
        if ($builder->getDeveloper() === null) {
97 1
            throw new \InvalidArgumentException('Application developer cannot be null. Solution: $appBuilder->setDeveloper(...);');
98
        }
99 13
        if ($builder->getIcon() === null) {
100 1
            throw new \InvalidArgumentException('Application icon cannot be null. Solution: $appBuilder->setIcon(...);');
101
        }
102 13
    }
103
104
    /**
105
     * Returns application name.
106
     *
107
     * @return string Application name.
108
     */
109 3
    public function getName(): string
110
    {
111 3
        return $this->name;
112
    }
113
114
    /**
115
     * Returns application summary.
116
     *
117
     * @return string|null Application summary.
118
     */
119 1
    public function getSummary(): ?string
120
    {
121 1
        return $this->summary;
122
    }
123
124
    /**
125
     * Returns application developer
126
     *
127
     * @return Developer Application developer.
128
     */
129 3
    public function getDeveloper(): Developer
130
    {
131 3
        return $this->developer;
132
    }
133
134
    /**
135
     * Returns application icon.
136
     *
137
     * @return GoogleImage Application icon.
138
     */
139 4
    public function getIcon(): GoogleImage
140
    {
141 4
        return $this->icon;
142
    }
143
144
    /**
145
     * Returns application rating on a five-point scale.
146
     *
147
     * @return float Rating application.
148
     */
149 1
    public function getScore(): float
150
    {
151 1
        return $this->score;
152
    }
153
154
    /**
155
     * Returns the price of the application.
156
     *
157
     * @return string|null Application price or null if it is free.
158
     */
159 1
    public function getPriceText(): ?string
160
    {
161 1
        return $this->priceText;
162
    }
163
164
    /**
165
     * Checks that this application is free.
166
     *
167
     * @return bool `true` if the application is free and `false` if paid.
168
     */
169 1
    public function isFree(): bool
170
    {
171 1
        return $this->priceText === null;
172
    }
173
174
    /**
175
     * Creates a new application builder.
176
     *
177
     * @return AppBuilder Application builder.
178
     * @ignore
179
     */
180 13
    public static function newBuilder(): AppBuilder
181
    {
182 13
        return new AppBuilder();
183
    }
184
185
    /**
186
     * Returns class properties as an array.
187
     *
188
     * @return array Class properties as an array.
189
     */
190
    public function asArray(): array
191
    {
192
        return [
193
            'id' => $this->getId(),
194
            'url' => $this->getUrl(),
195
            'locale' => $this->getLocale(),
196
            'country' => $this->getCountry(),
197
            'name' => $this->name,
198
            'summary' => $this->summary,
199
            'developer' => $this->developer->asArray(),
200
            'icon' => $this->icon->getUrl(),
201
            'score' => $this->score,
202
            'priceText' => $this->priceText,
203
        ];
204
    }
205
}
206