Passed
Push — feature/super-model ( 0f230d...88c1f6 )
by axel
02:19
created

UserModel::getAllInfo()   A

Complexity

Conditions 6
Paths 32

Size

Total Lines 26
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 15
nc 32
nop 0
dl 0
loc 26
rs 9.2222
c 0
b 0
f 0
1
<?php
2
3
namespace MalScraper\Model\User;
4
5
use MalScraper\Helper\Helper;
6
use MalScraper\Model\MainModel;
7
8
/**
9
 * UserModel class.
10
 */
11
class UserModel extends MainModel
12
{
13
    /**
14
     * Username
15
     *
16
     * @var string
17
     */
18
	private $_user;
19
20
    /**
21
     * Default constructor.
22
     *
23
     * @param string $user
24
     * @param string $parserArea
25
     *
26
     * @return void
27
     */
28
	public function __construct($user, $parserArea = '#content')
29
    {
30
    	$this->_user = $user;
31
        $this->_url = $this->_myAnimeListUrl.'/profile/'.$user;
32
    	$this->_parserArea = $parserArea;
33
34
        parent::errorCheck($this);
35
    }
36
37
    /**
38
     * Default call.
39
     *
40
     * @param string $method
41
     * @param array  $arguments
42
     *
43
     * @return array|string|int
44
     */
45
    public function __call($method, $arguments)
46
    {
47
        if ($this->_error)
48
            return $this->_error;
49
        return call_user_func_array([$this, $method], $arguments);
50
    }
51
52
    /**
53
     * Get username.
54
     *
55
     * @return string
56
     */
57
    private function getUsername()
58
    {
59
        return $this->_user;
60
    }
61
62
    /**
63
     * Get user image.
64
     *
65
     * @return string
66
     */
67
    private function getImage()
68
    {
69
        $image = $this->_parser->find('.container-left .user-profile', 0);
70
        $image = $image->find('.user-image img', 0);
71
        return $image ? Helper::imageUrlCleaner($image->src) : '';
72
    }
73
74
    /**
75
     * Get user status.
76
     *
77
     * @return string
78
     */
79
    private function getStatus()
80
    {
81
        $status = [];
82
        $status_area = $this->_parser->find('.container-left .user-profile', 0);
83
        $status_area = $status_area->find('.user-status', 0);
84
        foreach ($status_area->find('li') as $each_status) {
85
            $status_type = trim($each_status->find('span', 0)->plaintext);
86
            $status_value = trim($each_status->find('span', 1)->plaintext);
87
88
            $status[$status_type] = $status_value;
89
        }
90
        return $status;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $status returns the type array which is incompatible with the documented return type string.
Loading history...
91
    }
92
93
    /**
94
     * Get user status.
95
     *
96
     * @return string
97
     */
98
    private function getStatus2($status)
99
    {
100
        $status_area = $this->_parser->find('.container-left .user-profile', 0);
101
        $status_area = $status_area->find('.user-status', 2);
102
103
        $liNo = 0;
104
        switch ($status) {
105
            case 'forum':
106
            $liNo = 0;
107
            break;
108
109
            case 'review':
110
            $liNo = 1;
111
            break;
112
113
            case 'recommendation':
114
            $liNo = 2;
115
            break;
116
117
            case 'blog':
118
            $liNo = 3;
119
            break;
120
121
            case 'club':
122
            $liNo = 4;
123
            break;
124
125
            default:
126
            return '';
127
        }
128
129
        return trim($status_area->find('li', $liNo)->find('span', 1)->plaintext);
130
    }
131
132
133
    /**
134
     * Get user information.
135
     *
136
     * @return array
137
     */
138
    private function getAllInfo()
139
    {
140
        $status = $this->getStatus();
141
142
        $data = [
143
            'username'      => $this->getUsername(),
144
            'image'         => $this->getImage(),
145
            'last_online'    => !empty($status['Last Online']) ? $status['Last Online'] : '',
146
            'gender'         => !empty($status['Gender']) ? $status['Gender'] : '',
147
            'birthday'       => !empty($status['Birthday']) ? $status['Birthday'] : '',
148
            'location'       => !empty($status['Location']) ? $status['Location'] : '',
149
            'joined_date'    => !empty($status['Joined']) ? $status['Joined'] : '',
150
            'forum_post'     => $this->getStatus2('forum'),
151
            'review'         => $this->getStatus2('review'),
152
            'recommendation' => $this->getStatus2('recommendation'),
153
            'blog_post'      => $this->getStatus2('blog'),
154
            'club'           => $this->getStatus2('club'),
155
            // 'sns'            => $sns,
156
            // 'friend'         => $friend,
157
            // 'about'          => $about,
158
            // 'anime_stat'     => $anime_stat,
159
            // 'manga_stat'     => $manga_stat,
160
            // 'favorite'       => $favorite,
161
        ];
162
163
        return $data;
164
    }
165
}