AllRecommendationModel::getSourceLiked()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 1
Metric Value
cc 1
eloc 7
c 2
b 1
f 1
nc 1
nop 2
dl 0
loc 10
rs 10
1
<?php
2
3
namespace MalScraper\Model\Lists;
4
5
use MalScraper\Helper\Helper;
6
use MalScraper\Model\MainModel;
7
8
/**
9
 * AllRecommendationModel class.
10
 */
11
class AllRecommendationModel extends MainModel
12
{
13
    /**
14
     * Either anime or manga.
15
     *
16
     * @var string
17
     */
18
    private $_type;
19
20
    /**
21
     * Page number.
22
     *
23
     * @var int|string
24
     */
25
    private $_page;
26
27
    /**
28
     * Default constructor.
29
     *
30
     * @param string     $type
31
     * @param string|int $page
32
     * @param string     $parserArea
33
     *
34
     * @return void
35
     */
36
    public function __construct($type, $page, $parserArea = '#content')
37
    {
38
        $this->_type = $type;
39
        $this->_page = 100 * ($page - 1);
40
        $this->_url = $this->_myAnimeListUrl.'/recommendations.php?s=recentrecs&t='.$type.'&show='.$this->_page;
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 page.
75
     *
76
     * @return string
77
     */
78
    private function getPage()
79
    {
80
        return $this->_page;
81
    }
82
83
    /**
84
     * Get username.
85
     *
86
     * @param \simplehtmldom_1_5\simple_html_dom $each_recom
87
     *
88
     * @return string
89
     */
90
    private function getUsername($each_recom)
91
    {
92
        return $each_recom->find('table', 0)->next_sibling()->next_sibling()->find('a', 1)->plaintext;
93
    }
94
95
    /**
96
     * Get date.
97
     *
98
     * @param \simplehtmldom_1_5\simple_html_dom $each_recom
99
     *
100
     * @return string
101
     */
102
    private function getDate($each_recom)
103
    {
104
        $date = $each_recom->find('table', 0)->next_sibling()->next_sibling()->plaintext;
105
        $date = explode('-', $date);
106
107
        return trim($date[1]);
108
    }
109
110
    /**
111
     * Get source.
112
     *
113
     * @param \simplehtmldom_1_5\simple_html_dom $each_recom
114
     *
115
     * @return array
116
     */
117
    private function getSource($each_recom)
118
    {
119
        $source = [];
120
        $source_area = $each_recom->find('table tr', 0);
121
        $source['liked'] = $this->getSourceLiked($source_area, 0);
122
        $source['recommendation'] = $this->getSourceLiked($source_area, 1);
123
124
        return $source;
125
    }
126
127
    /**
128
     * Get source liked.
129
     *
130
     * @param \simplehtmldom_1_5\simple_html_dom $source_area
131
     * @param int                                $key
132
     *
133
     * @return array
134
     */
135
    private function getSourceLiked($source_area, $key)
136
    {
137
        $liked = [];
138
        $source_area = $source_area->find('td', $key);
139
        $liked['id'] = $this->getSourceId($source_area);
140
        $liked['title'] = $this->getSourceTitle($source_area);
141
        $liked['type'] = $this->getType();
142
        $liked['image'] = $this->getSourceImage($source_area);
143
144
        return $liked;
145
    }
146
147
    /**
148
     * Get source id.
149
     *
150
     * @param \simplehtmldom_1_5\simple_html_dom $source_area
151
     *
152
     * @return string
153
     */
154
    private function getSourceId($source_area)
155
    {
156
        $id = $source_area->find('a', 0)->href;
157
        $id = explode('/', $id);
158
159
        return $id[4];
160
    }
161
162
    /**
163
     * Get source title.
164
     *
165
     * @param \simplehtmldom_1_5\simple_html_dom $source_area
166
     *
167
     * @return string
168
     */
169
    private function getSourceTitle($source_area)
170
    {
171
        return $source_area->find('strong', 0)->plaintext;
172
    }
173
174
    /**
175
     * Get source image.
176
     *
177
     * @param \simplehtmldom_1_5\simple_html_dom $source_area
178
     *
179
     * @return string
180
     */
181
    private function getSourceImage($source_area)
182
    {
183
        $image = $source_area->find('img', 0)->getAttribute('data-src');
184
185
        return Helper::imageUrlCleaner($image);
186
    }
187
188
    /**
189
     * Get recommendation text.
190
     *
191
     * @param \simplehtmldom_1_5\simple_html_dom $each_recom
192
     *
193
     * @return string
194
     */
195
    private function getRecomText($each_recom)
196
    {
197
        $text = $each_recom->find('.recommendations-user-recs-text', 0)->plaintext;
198
        $text = preg_replace('/\s{2,}/', "\n", $text);
199
200
        return $text;
201
    }
202
203
    /**
204
     * Get anime/mange recommendation.
205
     *
206
     * @return array
207
     */
208
    private function getAllInfo()
209
    {
210
        $data = [];
211
        $recommendation_area = $this->_parser->find('div[class="spaceit borderClass"]');
212
        foreach ($recommendation_area as $each_recom) {
213
            $tmp = [];
214
215
            $tmp['username'] = $this->getUsername($each_recom);
216
            $tmp['date'] = $this->getDate($each_recom);
217
            $tmp['source'] = $this->getSource($each_recom);
218
            $tmp['recommendation'] = $this->getRecomText($each_recom);
219
220
            $data[] = $tmp;
221
        }
222
223
        return $data;
224
    }
225
}
226