Comment   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 149
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 32
dl 0
loc 149
rs 10
c 0
b 0
f 0
wmc 17

12 Methods

Rating   Name   Duplication   Size   Complexity  
A getLikers() 0 13 2
A isPinned() 0 3 1
A getMedia() 0 20 5
A getSourceId() 0 3 1
A isEdited() 0 3 1
A getAuthor() 0 3 1
A isFavorited() 0 3 1
A getId() 0 3 1
A getParentCommentId() 0 3 1
A getDate() 0 3 1
A getLevel() 0 3 1
A getText() 0 3 1
1
<?php
2
3
namespace Osnova\Services\Timeline;
4
5
use GuzzleHttp\Exception\RequestException;
6
use Osnova\Enums\MediaType;
7
use Osnova\Services\Media\Image;
8
use Osnova\Services\Media\Video;
9
use Osnova\Services\ServiceEntity;
10
use Osnova\Services\Timeline\Traits\HasLikes;
11
12
class Comment extends ServiceEntity
13
{
14
    use HasLikes;
15
16
    /**
17
     * Get comment ID.
18
     *
19
     * @return int|null
20
     */
21
    public function getId()
22
    {
23
        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...
24
    }
25
26
    /**
27
     * Get comment author.
28
     *
29
     * @return Author|null
30
     */
31
    public function getAuthor()
32
    {
33
        return $this->makeEntity(Author::class, 'author');
34
    }
35
36
    /**
37
     * Get comment's publication date.
38
     *
39
     * @return \DateTimeImmutable
40
     */
41
    public function getDate()
42
    {
43
        return new \DateTimeImmutable($this->getData('date'), 'Europe/Moscow');
0 ignored issues
show
Bug introduced by
It seems like $this->getData('date') 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

43
        return new \DateTimeImmutable(/** @scrutinizer ignore-type */ $this->getData('date'), 'Europe/Moscow');
Loading history...
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

43
        return new \DateTimeImmutable($this->getData('date'), /** @scrutinizer ignore-type */ 'Europe/Moscow');
Loading history...
44
    }
45
46
    /**
47
     * Get comment text.
48
     *
49
     * @return string|null
50
     */
51
    public function getText()
52
    {
53
        return $this->getData('text') ?? '';
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->getData('text') ?? '' also could return the type array which is incompatible with the documented return type null|string.
Loading history...
54
    }
55
56
    /**
57
     * Get comment media list.
58
     *
59
     * @return array
60
     */
61
    public function getMedia()
62
    {
63
        $media = $this->getData('media', []);
0 ignored issues
show
Bug introduced by
array() of type array is incompatible with the type null|string expected by parameter $default of Osnova\Services\ServiceEntity::getData(). ( Ignorable by Annotation )

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

63
        $media = $this->getData('media', /** @scrutinizer ignore-type */ []);
Loading history...
64
65
        if (!is_array($media) || empty($media)) {
66
            return [];
67
        }
68
69
        $entities = array_map(function (array $item) {
70
            switch ($item['type'] ?? 0) {
71
                case MediaType::IMAGE:
72
                    return $this->makeEntityFrom(Image::class, $item);
73
                case MediaType::VIDEO:
74
                    return $this->makeEntityFrom(Video::class, $item);
75
                default:
76
                    return null;
77
            }
78
        }, $media);
79
80
        return array_filter($entities);
81
    }
82
83
    /**
84
     * Get comment source ID.
85
     *
86
     * @return int|null
87
     */
88
    public function getSourceId()
89
    {
90
        return $this->getData('source_id');
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->getData('source_id') also could return the type array|string which is incompatible with the documented return type integer|null.
Loading history...
91
    }
92
93
    /**
94
     * Get comment's depth level.
95
     *
96
     * @return int|null
97
     */
98
    public function getLevel()
99
    {
100
        return $this->getData('level');
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->getData('level') also could return the type array|string which is incompatible with the documented return type integer|null.
Loading history...
101
    }
102
103
    /**
104
     * Get comment's parent comment ID.
105
     *
106
     * @return int|null
107
     */
108
    public function getParentCommentId()
109
    {
110
        return $this->getData('replyTo');
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->getData('replyTo') also could return the type array|string which is incompatible with the documented return type integer|null.
Loading history...
111
    }
112
113
    /**
114
     * Determines whether the comment is pinned.
115
     *
116
     * @return bool
117
     */
118
    public function isPinned()
119
    {
120
        return $this->getData('isPinned') === true;
121
    }
122
123
    /**
124
     * Determines whether the comment was favorited by the current user.
125
     *
126
     * @return bool
127
     */
128
    public function isFavorited()
129
    {
130
        return $this->getData('isFavorited') === true;
131
    }
132
133
    /**
134
     * Determnes whether the comment was edited.
135
     *
136
     * @return bool
137
     */
138
    public function isEdited()
139
    {
140
        return $this->getData('isEdited') === true;
141
    }
142
143
    /**
144
     * Get comment likers list.
145
     *
146
     * @return array|Liker[]
147
     */
148
    public function getLikers()
149
    {
150
        try {
151
            $response = $this->getApiProvider()->getClient()->request('GET', 'comment/likers/'.$this->getId());
152
153
            return $this->getEntitiesBuilder(Liker::class)
154
                ->fromResponse($response)
155
                ->collection();
156
        } catch (RequestException $e) {
157
            //
158
        }
159
160
        return [];
161
    }
162
}
163