Passed
Branch feature/super-model (24c950)
by axel
02:55
created

CharacterModel   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 247
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 101
dl 0
loc 247
rs 10
c 0
b 0
f 0
wmc 23

11 Methods

Rating   Name   Duplication   Size   Complexity  
A getMedia() 0 34 4
A getVa() 0 39 4
A getAllInfo() 0 16 1
A __call() 0 5 2
A getNickname() 0 8 2
A getName() 0 11 3
A getImage() 0 5 2
A __construct() 0 7 1
A getAbout() 0 17 2
A getId() 0 3 1
A getFavorite() 0 7 1
1
<?php
2
3
namespace MalScraper\Model;
4
5
use MalScraper\Helper\Helper;
6
7
/**
8
 * CharacterModel class.
9
 */
10
class CharacterModel extends MainModel
11
{
12
    /**
13
     * Id of the character.
14
     *
15
     * @var string|int
16
     */
17
	private $_id;
18
19
    /**
20
     * Default constructor.
21
     *
22
     * @param string|int $id
23
     * @param string $parserArea
24
     *
25
     * @return void
26
     */
27
	public function __construct($id, $parserArea = '#contentWrapper')
28
    {
29
    	$this->_id = $id;
30
        $this->_url = $this->_myAnimeListUrl.'/character/'.$id;
31
    	$this->_parserArea = $parserArea;
32
33
        parent::errorCheck($this);
34
    }
35
36
    /**
37
     * Default call.
38
     *
39
     * @param string $method
40
     * @param array  $arguments
41
     *
42
     * @return array|string|int
43
     */
44
    public function __call($method, $arguments)
45
    {
46
        if ($this->_error)
47
            return $this->_error;
48
        return call_user_func_array([$this, $method], $arguments);
49
    }
50
51
    /**
52
     * Get character id.
53
     *
54
     * @return string
55
     */
56
    private function getId()
57
    {
58
        return $this->_id;
59
    }
60
61
    /**
62
     * Get character image.
63
     *
64
     * @return string
65
     */
66
    private function getImage()
67
    {
68
        $image = $this->_parser->find('#content table tr', 0);
69
        $image = $image->find('td', 0)->find('div', 0)->find('a', 0)->find('img', 0);
70
        return $image ? $image->src : '';
0 ignored issues
show
Bug Best Practice introduced by
The expression return $image ? $image->src : '' also could return the type boolean which is incompatible with the documented return type string.
Loading history...
71
    }
72
73
    /**
74
     * Get character image.
75
     *
76
     * @return string
77
     */
78
    private function getNickname()
79
    {
80
        $nickname = $this->_parser->find('h1', 0)->plaintext;
81
        $nickname = trim(preg_replace('/\s+/', ' ', $nickname));
82
        preg_match('/\"([^"])*/', $nickname, $nickname);
0 ignored issues
show
Bug introduced by
$nickname of type string is incompatible with the type array|null expected by parameter $matches of preg_match(). ( Ignorable by Annotation )

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

82
        preg_match('/\"([^"])*/', $nickname, /** @scrutinizer ignore-type */ $nickname);
Loading history...
83
        if ($nickname)
84
            return substr($nickname[0], 1, strlen($nickname[0]) - 2);
85
        return null;
86
    }
87
88
    /**
89
     * Get character name.
90
     *
91
     * @param bool $isKanji
92
     *
93
     * @return string
94
     */
95
    private function getName($isKanji=false)
96
    {
97
        $html = $this->_parser->find('#content table tr', 0);
98
        $html = $html->find('td', 0)->next_sibling()->find('div[class=normal_header]', 0);
99
100
        $name_kanji = $html->find('small', 0);
101
        $name_kanji = $name_kanji ? $name_kanji->plaintext : '';
102
103
        if ($isKanji)
104
            return preg_replace('/(\(|\))/', '', $name_kanji);
105
        return trim(str_replace($name_kanji, '', $html->plaintext));
106
    }
107
108
    /**
109
     * Get number of user who favorite the character.
110
     *
111
     * @return string
112
     */
113
    private function getFavorite()
114
    {
115
        $favorite = $this->_parser->find('#content table tr', 0)->find('td', 0)->plaintext;
116
        preg_match('/(Member Favorites: ).+/', $favorite, $parsed_favorite);
117
        $favorite = trim($parsed_favorite[0]);
118
        $parsed_favorite = explode(': ', $favorite);
119
        return str_replace(',', '', $parsed_favorite[1]);
120
    }
121
122
    /**
123
     * Get character about.
124
     *
125
     * @return string
126
     */
127
    private function getAbout()
128
    {
129
        $html = $this->_parser->find('#content table tr', 0)->find('td', 0)->next_sibling();
130
131
        preg_match('/(<div class="normal_header" style="height: 15px;">).*(<div class="normal_header">)/', $html, $about);
132
133
        $html = $html->find('div[class=normal_header]', 0);
134
        $about = str_replace($html->outertext, '', $about[0]);
135
        $about = str_replace('<div class="normal_header">', '', $about);
136
137
        preg_match('/(No biography written)/', $about, $temp_about);
138
        if (!$temp_about) {
139
            $about = str_replace(['<br>', '<br />', '  '], ["\n", "\n", ' '], $about);
140
            $about = strip_tags($about);
141
            return preg_replace('/\n[^\S\n]*/', "\n", $about);
142
        } else {
143
            return null;
144
        }
145
    }
146
147
    /**
148
     * Get character role in anime/manga.
149
     *
150
     * @param string $type Either anime or manga
151
     *
152
     * @return array
153
     */
154
    private function getMedia($type='anime')
155
    {
156
        $mediaography = [];
157
        $mediaography_index = 0;
158
159
        $html = $this->_parser->find('#content table tr', 0)->find('td', 0);
160
        $mediaography_area = $type == 'anime' ? $html->find('table', 0) : $html->find('table', 1);
161
        $mediaography_area = $mediaography_area->find('tr');
0 ignored issues
show
Bug introduced by
The method find() does not exist on null. ( Ignorable by Annotation )

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

161
        /** @scrutinizer ignore-call */ 
162
        $mediaography_area = $mediaography_area->find('tr');

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
162
        if ($mediaography_area) {
163
            foreach ($mediaography_area as $each_media) {
164
                $media_image = $each_media->find('td', 0)->find('img', 0)->src;
165
                $media_image = Helper::imageUrlCleaner($media_image);
166
                $mediaography[$mediaography_index]['image'] = $media_image;
167
168
                $each_media = $each_media->find('td', 1);
169
170
                // id
171
                $media_id = $each_media->find('a', 0)->href;
172
                $parsed_media_id = explode('/', $media_id);
173
                $media_id = $parsed_media_id[4];
174
                $mediaography[$mediaography_index]['id'] = $media_id;
175
176
                // title
177
                $media_title = $each_media->find('a', 0)->plaintext;
178
                $mediaography[$mediaography_index]['title'] = $media_title;
179
180
                // role
181
                $media_role = $each_media->find('div small', 0)->plaintext;
182
                $mediaography[$mediaography_index]['role'] = $media_role;
183
184
                $mediaography_index++;
185
            }
186
        }
187
        return $mediaography;
188
    }
189
190
    /**
191
     * Get character voice actor list.
192
     *
193
     * @return array
194
     */
195
    private function getVa()
196
    {
197
        $va = [];
198
        $va_index = 0;
199
        $html = $this->_parser->find('#content table tr', 0)->find('td', 0)->next_sibling();
200
        $va_area = $html->find('div[class=normal_header]', 1);
201
        $va_area = $va_area->next_sibling();
202
        if ($va_area->tag == 'table') {
203
            while (true) {
204
205
                // id
206
                $va_name_area = $va_area->find('td', 1);
207
                $va_id = $va_name_area->find('a', 0)->href;
208
                $parsed_va_id = explode('/', $va_id);
209
                $va_id = $parsed_va_id[4];
210
                $va[$va_index]['id'] = $va_id;
211
212
                // name
213
                $va_name = $va_name_area->find('a', 0)->plaintext;
214
                $va[$va_index]['name'] = $va_name;
215
216
                // role
217
                $va_role = $va_name_area->find('small', 0)->plaintext;
218
                $va[$va_index]['role'] = $va_role;
219
220
                // image
221
                $va_image = $va_area->find('img', 0)->src;
222
                $va_image = Helper::imageUrlCleaner($va_image);
223
                $va[$va_index]['image'] = $va_image;
224
225
                $va_area = $va_area->next_sibling();
226
                if ($va_area->tag != 'table') {
227
                    break;
228
                } else {
229
                    $va_index++;
230
                }
231
            }
232
        }
233
        return $va;
234
    }
235
236
    /**
237
     * Get character all information.
238
     *
239
     * @return array
240
     */
241
    private function getAllInfo()
242
    {
243
        $data = [
244
            'id'           => self::getId(),
0 ignored issues
show
Bug Best Practice introduced by
The method MalScraper\Model\CharacterModel::getId() is not static, but was called statically. ( Ignorable by Annotation )

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

244
            'id'           => self::/** @scrutinizer ignore-call */ getId(),
Loading history...
245
            'image'        => self::getImage(),
0 ignored issues
show
Bug Best Practice introduced by
The method MalScraper\Model\CharacterModel::getImage() is not static, but was called statically. ( Ignorable by Annotation )

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

245
            'image'        => self::/** @scrutinizer ignore-call */ getImage(),
Loading history...
246
            'nickname'     => self::getNickname(),
0 ignored issues
show
Bug Best Practice introduced by
The method MalScraper\Model\CharacterModel::getNickname() is not static, but was called statically. ( Ignorable by Annotation )

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

246
            'nickname'     => self::/** @scrutinizer ignore-call */ getNickname(),
Loading history...
247
            'name'         => self::getName(),
0 ignored issues
show
Bug Best Practice introduced by
The method MalScraper\Model\CharacterModel::getName() is not static, but was called statically. ( Ignorable by Annotation )

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

247
            'name'         => self::/** @scrutinizer ignore-call */ getName(),
Loading history...
248
            'name_kanji'   => self::getName(true),
249
            'favorite'     => self::getFavorite(),
0 ignored issues
show
Bug Best Practice introduced by
The method MalScraper\Model\CharacterModel::getFavorite() is not static, but was called statically. ( Ignorable by Annotation )

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

249
            'favorite'     => self::/** @scrutinizer ignore-call */ getFavorite(),
Loading history...
250
            'about'        => self::getAbout(),
0 ignored issues
show
Bug Best Practice introduced by
The method MalScraper\Model\CharacterModel::getAbout() is not static, but was called statically. ( Ignorable by Annotation )

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

250
            'about'        => self::/** @scrutinizer ignore-call */ getAbout(),
Loading history...
251
            'animeography' => self::getMedia('anime'),
0 ignored issues
show
Bug Best Practice introduced by
The method MalScraper\Model\CharacterModel::getMedia() is not static, but was called statically. ( Ignorable by Annotation )

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

251
            'animeography' => self::/** @scrutinizer ignore-call */ getMedia('anime'),
Loading history...
252
            'mangaography' => self::getMedia('manga'),
253
            'va'           => self::getVa(),
0 ignored issues
show
Bug Best Practice introduced by
The method MalScraper\Model\CharacterModel::getVa() is not static, but was called statically. ( Ignorable by Annotation )

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

253
            'va'           => self::/** @scrutinizer ignore-call */ getVa(),
Loading history...
254
        ];
255
256
        return $data;
257
    }
258
}