SeasonModel::getProducerId()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 1
Metric Value
cc 1
eloc 3
c 2
b 1
f 1
nc 1
nop 1
dl 0
loc 6
rs 10
1
<?php
2
3
namespace MalScraper\Model\Seasonal;
4
5
use MalScraper\Helper\Helper;
6
use MalScraper\Model\MainModel;
7
8
/**
9
 * SeasonModel class.
10
 */
11
class SeasonModel extends MainModel
12
{
13
    /**
14
     * Year of season.
15
     *
16
     * @var string
17
     */
18
    private $_year;
19
20
    /**
21
     * Season name. Either spring, summer, fall, or winter.
22
     *
23
     * @var string
24
     */
25
    private $_season;
26
27
    /**
28
     * Default constructor.
29
     *
30
     * @param string|int $year
31
     * @param string     $season
32
     * @param string     $parserArea
33
     *
34
     * @return void
35
     */
36
    public function __construct($year = false, $season = false, $parserArea = '#content .js-categories-seasonal')
37
    {
38
        $this->_year = !$year ? date('Y') : $year;
39
        $this->_season = !$season ? Helper::getCurrentSeason() : $season;
40
        $this->_url = $this->_myAnimeListUrl.'/anime/season/'.$this->_year.'/'.$this->_season;
41
        $this->_parserArea = $parserArea;
42
43
        parent::errorCheck($this);
44
    }
45
46
    /**
47
     * Default call.
48
     *
49
     * @param string $method
50
     * @param array  $arguments
51
     *
52
     * @return array|string|int
53
     */
54
    public function __call($method, $arguments)
55
    {
56
        if ($this->_error) {
57
            return $this->_error;
58
        }
59
60
        return call_user_func_array([$this, $method], $arguments);
61
    }
62
63
    /**
64
     * Get image.
65
     *
66
     * @param \simplehtmldom_1_5\simple_html_dom $each_anime
67
     *
68
     * @return string
69
     */
70
    private function getImage($each_anime)
71
    {
72
        $temp_image = $each_anime->find('div[class=image]', 0)->find('img', 0);
73
        $image = $temp_image->src;
74
        $image = !$image ? $temp_image->getAttribute('data-src') : $image;
75
76
        return Helper::imageUrlCleaner($image);
0 ignored issues
show
Bug introduced by
It seems like $image can also be of type true; however, parameter $str of MalScraper\Helper\Helper::imageUrlCleaner() 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

76
        return Helper::imageUrlCleaner(/** @scrutinizer ignore-type */ $image);
Loading history...
77
    }
78
79
    /**
80
     * Get id.
81
     *
82
     * @param \simplehtmldom_1_5\simple_html_dom $name_area
83
     *
84
     * @return string
85
     */
86
    private function getId($name_area)
87
    {
88
        $id = $name_area->find('p a', 0)->href;
89
        $parsed_char_id = explode('/', $id);
90
91
        return $parsed_char_id[4];
92
    }
93
94
    /**
95
     * Get title.
96
     *
97
     * @param \simplehtmldom_1_5\simple_html_dom $name_area
98
     *
99
     * @return string
100
     */
101
    private function getTitle($name_area)
102
    {
103
        return $name_area->find('p a', 0)->plaintext;
104
    }
105
106
    /**
107
     * Get producer.
108
     *
109
     * @param \simplehtmldom_1_5\simple_html_dom $producer_area
110
     *
111
     * @return array
112
     */
113
    private function getProducer($producer_area)
114
    {
115
        $producer = [];
116
        $temp_producer = $producer_area->find('span[class=producer]', 0);
117
        foreach ($temp_producer->find('a') as $each_producer) {
118
            $temp_prod = [];
119
120
            $temp_prod['id'] = $this->getProducerId($each_producer);
121
            $temp_prod['name'] = $this->getProducerName($each_producer);
122
123
            $producer[] = $temp_prod;
124
        }
125
126
        return $producer;
127
    }
128
129
    /**
130
     * Get producer id.
131
     *
132
     * @param \simplehtmldom_1_5\simple_html_dom $each_producer
133
     *
134
     * @return string
135
     */
136
    private function getProducerId($each_producer)
137
    {
138
        $prod_id = $each_producer->href;
139
        $parsed_prod_id = explode('/', $prod_id);
140
141
        return $parsed_prod_id[3];
142
    }
143
144
    /**
145
     * Get producer name.
146
     *
147
     * @param \simplehtmldom_1_5\simple_html_dom $each_producer
148
     *
149
     * @return string
150
     */
151
    private function getProducerName($each_producer)
152
    {
153
        return $each_producer->plaintext;
154
    }
155
156
    /**
157
     * Get episode.
158
     *
159
     * @param \simplehtmldom_1_5\simple_html_dom $producer_area
160
     *
161
     * @return string
162
     */
163
    private function getEpisode($producer_area)
164
    {
165
        $episode = $producer_area->find('div[class=eps]', 0)->plaintext;
166
        $episode = trim(str_replace(['eps', 'ep'], '', $episode));
167
168
        return $episode == '?' ? '' : $episode;
169
    }
170
171
    /**
172
     * Get source.
173
     *
174
     * @param \simplehtmldom_1_5\simple_html_dom $producer_area
175
     *
176
     * @return string
177
     */
178
    private function getSource($producer_area)
179
    {
180
        return trim($producer_area->find('span[class=source]', 0)->plaintext);
181
    }
182
183
    /**
184
     * Get genre.
185
     *
186
     * @param \simplehtmldom_1_5\simple_html_dom $each_anime
187
     *
188
     * @return array
189
     */
190
    private function getGenre($each_anime)
191
    {
192
        $genre = [];
193
        $genre_area = $each_anime->find('div[class="genres js-genre"]', 0);
194
        foreach ($genre_area->find('a') as $each_genre) {
195
            $genre[] = $each_genre->plaintext;
196
        }
197
198
        return $genre;
199
    }
200
201
    /**
202
     * Get synopsis.
203
     *
204
     * @param \simplehtmldom_1_5\simple_html_dom $each_anime
205
     *
206
     * @return string
207
     */
208
    private function getSynopsis($each_anime)
209
    {
210
        $synopsis = $each_anime->find('div[class="synopsis js-synopsis"]', 0)->plaintext;
211
        preg_match('/(No synopsis)/', $synopsis, $temp_synopsis);
212
        if (!$temp_synopsis) {
213
            $synopsis = trim(preg_replace("/([\s])+/", ' ', $synopsis));
214
        } else {
215
            $synopsis = '';
216
        }
217
218
        return $synopsis;
219
    }
220
221
    /**
222
     * Get licensor.
223
     *
224
     * @param \simplehtmldom_1_5\simple_html_dom $each_anime
225
     *
226
     * @return array
227
     */
228
    private function getLicensor($each_anime)
229
    {
230
        $temp_licensor = $each_anime->find('div[class="synopsis js-synopsis"] .licensors', 0)->getAttribute('data-licensors');
231
        $licensor = explode(',', $temp_licensor);
232
233
        return array_filter($licensor);
234
    }
235
236
    /**
237
     * Get type.
238
     *
239
     * @param \simplehtmldom_1_5\simple_html_dom $info_area
240
     *
241
     * @return string
242
     */
243
    private function getType($info_area)
244
    {
245
        $type = $info_area->find('.info', 0)->plaintext;
246
        $type = explode('-', $type);
247
248
        return trim($type[0]);
249
    }
250
251
    /**
252
     * Get airing start.
253
     *
254
     * @param \simplehtmldom_1_5\simple_html_dom $info_area
255
     *
256
     * @return string
257
     */
258
    private function getAiring($info_area)
259
    {
260
        $airing_start = $info_area->find('.info .remain-time', 0)->plaintext;
261
262
        return trim(str_replace(['?', ' ,'], ['', ','], $airing_start));
263
    }
264
265
    /**
266
     * Get member.
267
     *
268
     * @param \simplehtmldom_1_5\simple_html_dom $info_area
269
     *
270
     * @return string
271
     */
272
    private function getMember($info_area)
273
    {
274
        $member = $info_area->find('.scormem span[class^=member]', 0)->plaintext;
275
276
        return trim(str_replace(',', '', $member));
277
    }
278
279
    /**
280
     * Get score.
281
     *
282
     * @param \simplehtmldom_1_5\simple_html_dom $info_area
283
     *
284
     * @return string
285
     */
286
    private function getScore($info_area)
287
    {
288
        $score = $info_area->find('.scormem .score', 0)->plaintext;
289
290
        return trim(str_replace('N/A', '', $score));
291
    }
292
293
    /**
294
     * Get result list.
295
     *
296
     * @return array
297
     */
298
    private function getAllInfo()
299
    {
300
        $data = [];
301
302
        $anime_table = $this->_parser->find('div[class="seasonal-anime js-seasonal-anime"]');
303
        foreach ($anime_table as $each_anime) {
304
            $result = [];
305
306
            $name_area = $each_anime->find('div[class=title]', 0);
307
            $producer_area = $each_anime->find('div[class=prodsrc]', 0);
308
            $info_area = $each_anime->find('.information', 0);
309
310
            $result['image'] = $this->getImage($each_anime);
311
            $result['id'] = $this->getId($name_area);
312
            $result['title'] = $this->getTitle($name_area);
313
            $result['producer'] = $this->getProducer($producer_area);
314
            $result['episode'] = $this->getEpisode($producer_area);
315
            $result['source'] = $this->getSource($producer_area);
316
            $result['genre'] = $this->getGenre($each_anime);
317
            $result['synopsis'] = $this->getSynopsis($each_anime);
318
            $result['licensor'] = $this->getLicensor($each_anime);
319
            $result['type'] = $this->getType($info_area);
320
            $result['airing_start'] = $this->getAiring($info_area);
321
            $result['member'] = $this->getMember($info_area);
322
            $result['score'] = $this->getScore($info_area);
323
324
            $data[] = $result;
325
        }
326
327
        return $data;
328
    }
329
}
330