User::getEntries()   A
last analyzed

Complexity

Conditions 2
Paths 3

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 9
nc 3
nop 1
dl 0
loc 16
rs 9.9666
c 0
b 0
f 0
1
<?php
2
3
namespace Osnova\Services\Users;
4
5
use GuzzleHttp\Exception\RequestException;
6
use Osnova\Services\ServiceEntity;
7
use Osnova\Services\ServiceRequest;
8
use Osnova\Services\Timeline\Comment;
9
use Osnova\Services\Timeline\Entry;
10
11
class User extends ServiceEntity
12
{
13
    /**
14
     * Get user ID.
15
     *
16
     * @return int|null
17
     */
18
    public function getId()
19
    {
20
        return $this->getData('id');
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->getData('id') also could return the type array|string which is incompatible with the documented return type integer|null.
Loading history...
21
    }
22
23
    /**
24
     * Get user hash
25
     *
26
     * @return string|null
27
     */
28
    public function getHash()
29
    {
30
        return $this->getData('user_hash');
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->getData('user_hash') also could return the type array which is incompatible with the documented return type null|string.
Loading history...
31
    }
32
33
    /**
34
     * Get user name.
35
     *
36
     * @return string|null
37
     */
38
    public function getName()
39
    {
40
        return $this->getData('name');
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->getData('name') also could return the type array which is incompatible with the documented return type null|string.
Loading history...
41
    }
42
43
    /**
44
     * Get avatar URL.
45
     *
46
     * @return string|null
47
     */
48
    public function getAvatarUrl()
49
    {
50
        return $this->getData('avatar_url');
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->getData('avatar_url') also could return the type array which is incompatible with the documented return type null|string.
Loading history...
51
    }
52
53
    /**
54
     * Get user URL.
55
     *
56
     * @return string|null
57
     */
58
    public function getUrl()
59
    {
60
        return $this->getData('url');
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->getData('url') also could return the type array which is incompatible with the documented return type null|string.
Loading history...
61
    }
62
63
    /**
64
     * Get karma value.
65
     *
66
     * @return int|null
67
     */
68
    public function getKarma()
69
    {
70
        return $this->getData('karma');
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->getData('karma') also could return the type array|string which is incompatible with the documented return type integer|null.
Loading history...
71
    }
72
73
    /**
74
     * Get entries count.
75
     *
76
     * @return int
77
     */
78
    public function getEntriesCount()
79
    {
80
        return intval($this->getData('counters.entries'));
81
    }
82
83
    /**
84
     * Get comments count.
85
     *
86
     * @return int
87
     */
88
    public function getCommentsCount()
89
    {
90
        return intval($this->getData('counters.comments'));
91
    }
92
93
    /**
94
     * Get favorites count.
95
     *
96
     * @return int
97
     */
98
    public function getFavoritesCount()
99
    {
100
        return intval($this->getData('counters.favorites'));
101
    }
102
103
    /**
104
     * Get registration date.
105
     *
106
     * @return \DateTimeImmutable
107
     */
108
    public function getCreatedAtDate()
109
    {
110
        return new \DateTimeImmutable($this->getData('created'), 'Europe/Moscow');
0 ignored issues
show
Bug introduced by
'Europe/Moscow' of type string is incompatible with the type DateTimeZone expected by parameter $timezone of DateTimeImmutable::__construct(). ( Ignorable by Annotation )

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

110
        return new \DateTimeImmutable($this->getData('created'), /** @scrutinizer ignore-type */ 'Europe/Moscow');
Loading history...
Bug introduced by
It seems like $this->getData('created') can also be of type array; however, parameter $time of DateTimeImmutable::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

110
        return new \DateTimeImmutable(/** @scrutinizer ignore-type */ $this->getData('created'), 'Europe/Moscow');
Loading history...
111
    }
112
113
    /**
114
     * Get internal API URL.
115
     *
116
     * @param string $path = ''
117
     *
118
     * @return string
119
     */
120
    protected function apiUrl(string $path = '')
121
    {
122
        return 'users/'.$this->getId().($path ? '/'.ltrim($path) : '');
123
    }
124
125
    /**
126
     * Get user's entries list.
127
     *
128
     * @param ServiceRequest $request
129
     *
130
     * @return array|Entry[]
131
     */
132
    public function getEntries(ServiceRequest $request)
133
    {
134
        try {
135
            $response = $this->getApiProvider()->getClient()->request('GET', $this->apiUrl('entries'), [
136
                'query' => $request->getParams(),
137
            ]);
138
139
            return $this->getEntitiesBuilder(Entry::class)
140
                ->fromResponse($response)
141
                ->with($this->getApiProvider(), $this->getOsnovaResource())
142
                ->collection();
143
        } catch (RequestException $e) {
144
            //
145
        }
146
147
        return [];
148
    }
149
150
    /**
151
     * Get user's comments list.
152
     *
153
     * @param ServiceRequest $request
154
     *
155
     * @return array|Comment[]
156
     */
157
    public function getComments(ServiceRequest $request)
158
    {
159
        try {
160
            $response = $this->getApiProvider()->getClient()->request('GET', $this->apiUrl('comments'), [
161
                'query' => $request->getParams(),
162
            ]);
163
164
            return $this->getEntitiesBuilder(Comment::class)
165
                ->fromResponse($response)
166
                ->with($this->getApiProvider(), $this->getOsnovaResource())
167
                ->collection();
168
        } catch (RequestException $e) {
169
            //
170
        }
171
172
        return [];
173
    }
174
}
175