Test Setup Failed
Push — feature/super-model ( 86f2ea...9eaa75 )
by axel
09:29 queued 07:56
created

FriendModel   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 133
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 31
dl 0
loc 133
rs 10
c 0
b 0
f 0
wmc 11

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getFriendSince() 0 6 1
A __call() 0 7 2
A getImage() 0 3 1
A __construct() 0 7 1
A getAllInfo() 0 19 3
A getUsername() 0 3 1
A getName() 0 6 1
A getLastOnline() 0 5 1
1
<?php
2
3
namespace MalScraper\Model\User;
4
5
use MalScraper\Helper\Helper;
6
use MalScraper\Model\MainModel;
7
8
/**
9
 * FriendModel class.
10
 */
11
class FriendModel 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.'/friends';
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
        }
50
51
        return call_user_func_array([$this, $method], $arguments);
52
    }
53
54
    /**
55
     * Get username.
56
     *
57
     * @return string
58
     */
59
    private function getUsername()
60
    {
61
        return $this->_user;
62
    }
63
64
    /**
65
     * Get friend image.
66
     *
67
     * @param \simplehtmldom_1_5\simple_html_dom $f
68
     *
69
     * @return string
70
     */
71
    private function getImage($f)
72
    {
73
        return Helper::imageUrlCleaner($f->find('a img', 0)->src);
74
    }
75
76
    /**
77
     * Get friend name.
78
     *
79
     * @param \simplehtmldom_1_5\simple_html_dom $f
80
     *
81
     * @return string
82
     */
83
    private function getName($f)
84
    {
85
        $name_temp = $f->find('a', 0)->href;
86
        $name_temp = explode('/', $name_temp);
87
88
        return $name_temp[4];
89
    }
90
91
    /**
92
     * Get friend last online.
93
     *
94
     * @param \simplehtmldom_1_5\simple_html_dom $f
95
     *
96
     * @return string
97
     */
98
    private function getLastOnline($f)
99
    {
100
        $last_online = $f->find('strong', 0)->parent()->parent()->next_sibling();
101
102
        return trim($last_online->plaintext);
103
    }
104
105
    /**
106
     * Get friend since.
107
     *
108
     * @param \simplehtmldom_1_5\simple_html_dom $f
109
     *
110
     * @return string
111
     */
112
    private function getFriendSince($f)
113
    {
114
        $friend_since = $f->find('strong', 0)->parent()->parent()->next_sibling()->next_sibling();
115
        $friend_since = str_replace('Friends since', '', $friend_since->plaintext);
116
117
        return trim($friend_since);
118
    }
119
120
    /**
121
     * Get user friend list.
122
     *
123
     * @return string
124
     */
125
    private function getAllInfo()
126
    {
127
        $friend = [];
128
        $friend_area = $this->_parser->find('.majorPad', 0);
129
        if ($friend_area) {
130
            foreach ($friend_area->find('.friendHolder') as $f) {
131
                $f_dump = [];
132
                $f = $f->find('.friendBlock', 0);
133
134
                $f_dump['image'] = $this->getImage($f);
135
                $f_dump['name'] = $this->getName($f);
136
                $f_dump['last_online'] = $this->getLastOnline($f);
137
                $f_dump['friend_since'] = $this->getFriendSince($f);
138
139
                $friend[] = $f_dump;
140
            }
141
        }
142
143
        return $friend;
144
    }
145
}
146