Passed
Push — develop ( 5c3296...f8ad08 )
by axel
02:33
created

AnimeMangaRecommendationModel::getType()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace MalScraper\Model\Additional;
4
5
use MalScraper\Helper\Helper;
6
use MalScraper\Model\MainModel;
7
8
/**
9
 * AnimeMangaRecommendationModel class.
10
 */
11
class AnimeMangaRecommendationModel extends MainModel
12
{
13
    /**
14
     * Either anime or manga.
15
     *
16
     * @var string
17
     */
18
    private $_type;
19
20
    /**
21
     * Id of the anime/manga.
22
     *
23
     * @var string|int
24
     */
25
    private $_id;
26
27
    /**
28
     * Default constructor.
29
     *
30
     * @param string     $type
31
     * @param string|int $id
32
     * @param string     $parserArea
33
     *
34
     * @return void
35
     */
36
    public function __construct($type, $id, $parserArea = '.js-scrollfix-bottom-rel')
37
    {
38
        $this->_type = $type;
39
        $this->_id = $id;
40
        $this->_url = $this->_myAnimeListUrl.'/'.$type.'/'.$id.'/a/userrecs';
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 type.
65
     *
66
     * @return string
67
     */
68
    private function getType()
69
    {
70
        return $this->_type;
71
    }
72
73
    /**
74
     * Get anime/manga id.
75
     *
76
     * @return string
77
     */
78
    private function getId()
79
    {
80
        return $this->_id;
81
    }
82
83
    /**
84
     * Get recommendation anime/manga id.
85
     *
86
     * @param \simplehtmldom_1_5\simple_html_dom $content_area
87
     *
88
     * @return string
89
     */
90
    private function getRecomId($content_area)
91
    {
92
        $id = $content_area->find('a', 0)->href;
93
        $id = explode('/', $id);
94
95
        return $id[4];
96
    }
97
98
    /**
99
     * Get recommendation anime/manga title.
100
     *
101
     * @param \simplehtmldom_1_5\simple_html_dom $content_area
102
     *
103
     * @return string
104
     */
105
    private function getRecomTitle($content_area)
106
    {
107
        return $content_area->find('strong', 0)->plaintext;
108
    }
109
110
    /**
111
     * Get recommendation anime/manga image.
112
     *
113
     * @param \simplehtmldom_1_5\simple_html_dom $each_recom
114
     *
115
     * @return string
116
     */
117
    private function getRecomImage($each_recom)
118
    {
119
        $image = $each_recom->find('img', 0)->getAttribute('data-src');
120
121
        return Helper::imageUrlCleaner($image);
122
    }
123
124
    /**
125
     * Get recommendation anime/manga user.
126
     *
127
     * @param \simplehtmldom_1_5\simple_html_dom $content_area
128
     *
129
     * @return string
130
     */
131
    private function getRecomUsername($content_area)
132
    {
133
        return $content_area->find('.borderClass', 0)->find('.spaceit_pad', 1)->find('a', 1)->plaintext;
134
    }
135
136
    /**
137
     * Get recommendation anime/manga text.
138
     *
139
     * @param \simplehtmldom_1_5\simple_html_dom $content_area
140
     *
141
     * @return string
142
     */
143
    private function getRecomText($content_area)
144
    {
145
        $text = $content_area->find('.borderClass', 0)->find('.spaceit_pad', 0)->plaintext;
146
        $useless_area = $content_area->find('.js-toggle-recommendation-button', 0) ? $content_area->find('.js-toggle-recommendation-button', 0)->plaintext : null;
147
        $text = str_replace($useless_area, '', $text);
148
        $text = str_replace('&lt;', '<', $text);
149
        $text = preg_replace('/\s{2,}/', "\n", $text);
150
151
        return $text;
152
    }
153
154
    /**
155
     * Get recommendation anime/manga other recommendation.
156
     *
157
     * @param \simplehtmldom_1_5\simple_html_dom $other_area
158
     *
159
     * @return array
160
     */
161
    private function getRecomOther($other_area)
162
    {
163
        $other = [];
164
        if ($other_area) {
0 ignored issues
show
introduced by
$other_area is of type simplehtmldom_1_5\simple_html_dom, thus it always evaluated to true.
Loading history...
165
            foreach ($other_area->find('.borderClass') as $each_other) {
166
                $tmp = [];
167
168
                $tmp['username'] = $this->getOtherUser($each_other);
169
                $tmp['recommendation'] = $this->getOtherRecom($each_other);
170
171
                $other[] = $tmp;
172
            }
173
        }
174
175
        return $other;
176
    }
177
178
    /**
179
     * Get recommendation anime/manga other user.
180
     *
181
     * @param \simplehtmldom_1_5\simple_html_dom $each_other
182
     *
183
     * @return string
184
     */
185
    private function getOtherUser($each_other)
186
    {
187
        return $each_other->find('.spaceit_pad', 1)->find('a', 1)->plaintext;
188
    }
189
190
    /**
191
     * Get recommendation anime/manga other recommendation.
192
     *
193
     * @param \simplehtmldom_1_5\simple_html_dom $each_other
194
     *
195
     * @return string
196
     */
197
    private function getOtherRecom($each_other)
198
    {
199
        $text = $each_other->find('.spaceit_pad', 0)->plaintext;
200
        $useless_area = $each_other->find('.js-toggle-recommendation-button', 0) ? $each_other->find('.js-toggle-recommendation-button', 0)->plaintext : null;
201
        $text = str_replace($useless_area, '', $text);
202
        $text = str_replace('&lt;', '<', $text);
203
        $text = preg_replace('/\s{2,}/', "\n", $text);
204
205
        return $text;
206
    }
207
208
    /**
209
     * Get anime/mange review.
210
     *
211
     * @return array
212
     */
213
    private function getAllInfo()
214
    {
215
        $data = [];
216
        $recommendation_area = $this->_parser->find('.borderClass table');
217
        foreach ($recommendation_area as $each_recom) {
218
            $tmp = [];
219
220
            $content_area = $each_recom->find('td', 1);
221
            $other_area = $each_recom->find('div[id^=simaid]', 0);
222
223
            $tmp['id'] = $this->getRecomId($content_area);
224
            $tmp['title'] = $this->getRecomTitle($content_area);
225
            $tmp['image'] = $this->getRecomImage($each_recom);
226
            $tmp['username'] = $this->getRecomUsername($content_area);
227
            $tmp['recommendation'] = $this->getRecomText($content_area);
228
            $tmp['other'] = $this->getRecomOther($other_area);
229
230
            $data[] = $tmp;
231
        }
232
233
        return $data;
234
    }
235
}
236