Test Failed
Push — master ( e3c39f...fe570d )
by Mihail
07:20
created

Apps/Model/Api/Comments/EntityCommentData.php (2 issues)

1
<?php
2
3
namespace Apps\Model\Api\Comments;
4
5
use Apps\ActiveRecord\CommentAnswer;
6
use Apps\ActiveRecord\CommentPost;
7
use Ffcms\Core\App;
8
use Ffcms\Core\Arch\Model;
9
use Ffcms\Core\Exception\JsonException;
10
use Ffcms\Core\Helper\Date;
11
use Ffcms\Core\Helper\Type\Str;
12
13
/**
14
 * Class EntityCommentData. Unified model to get comment specified output to json response.
15
 * @package Apps\Model\Api\Comments
16
 */
17
class EntityCommentData extends Model
18
{
19
    /** @var \Apps\ActiveRecord\CommentPost|\Apps\ActiveRecord\CommentAnswer $_record */
20
    private $_record;
21
    private $_type;
22
    private $_calcAnswers = true;
23
24
    /**
25
     * EntityCommentPostData constructor. Pass inside the model active record object of comment post or answer
26
     * @param $record
27
     * @param bool $calcAnswers
28
     * @throws JsonException
29
     */
30
    public function __construct($record, $calcAnswers = true)
31
    {
32
        $this->_record = $record;
33
        $this->_calcAnswers = (bool)$calcAnswers;
34
        if ($this->_record instanceof CommentPost) {
35
            $this->_type = 'post';
36
        } elseif ($this->_record instanceof CommentAnswer) {
37
            $this->_type = 'answer';
38
        } else {
39
            throw new JsonException('Unknown comment request');
40
        }
41
        parent::__construct();
42
    }
43
44
    /**
45
     * Prepare output comment post information array
46
     * @return array|null
47
     */
48
    public function make(): ?array
49
    {
50
        if (!$this->_record) {
51
            return null;
52
        }
53
54
        // build user data
55
        $userName = __('Unknown');
56
        $userAvatar = App::$Alias->scriptUrl . '/upload/user/avatar/small/default.jpg';
57
        $userColor = 0;
58
        if ($this->_record->user && $this->_record->user->id > 0) {
59
            $userName = $this->_record->user->profile->getNickname();
60
            $userAvatar = $this->_record->user->profile->getAvatarUrl('small');
61
            $userColor = $this->_record->user->role->color;
62
        } else {
63
            if (!Str::likeEmpty($this->_record->guest_name)) {
64
                $userName = App::$Security->strip_tags($this->_record->guest_name);
65
            }
66
        }
67
68
        // return output json data
69
        $res = [
70
            'type' => $this->_type,
71
            'id' => $this->_record->id,
72
            'text' => $this->_record->message,
73
            'date' => Date::convertToDatetime($this->_record->created_at, Date::FORMAT_TO_HOUR),
74
            'app_name' => $this->_record->app_name,
0 ignored issues
show
The property app_name does not seem to exist on Apps\ActiveRecord\CommentAnswer. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
75
            'app_id' => $this->_record->app_relation_id,
0 ignored issues
show
The property app_relation_id does not seem to exist on Apps\ActiveRecord\CommentAnswer. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
76
            'moderate' => (int)$this->_record->moderate,
77
            'user' => [
78
                'id' => $this->_record->user_id,
79
                'name' => $userName,
80
                'avatar' => $userAvatar,
81
                'color' => $userColor
82
            ]
83
        ];
84
85
        if ($this->_type === 'post' && method_exists($this->_record, 'getAnswerCount') && $this->_calcAnswers) {
86
            $res['answers'] = $this->_record->getAnswerCount();
87
        } elseif ($this->_type === 'answer') {
88
            $res['comment_id'] = $this->_record->comment_id;
89
        }
90
91
        return $res;
92
    }
93
}
94