Passed
Push — master ( 8f5376...4142cd )
by Mihail
12:41
created

EntityCommentData::make()   C

Complexity

Conditions 7
Paths 10

Size

Total Lines 40
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 40
rs 6.7272
cc 7
eloc 26
nc 10
nop 0
1
<?php
2
3
namespace Apps\Model\Api\Comments;
4
5
6
use Apps\ActiveRecord\CommentAnswer;
7
use Apps\ActiveRecord\CommentPost;
8
use Ffcms\Core\App;
9
use Ffcms\Core\Arch\Model;
10
use Ffcms\Core\Exception\JsonException;
11
use Ffcms\Core\Helper\Date;
12
use Ffcms\Core\Helper\Type\Str;
13
14
/**
15
 * Class EntityCommentPostData. Unified model to get comment specified output to json response.
16
 * @package Apps\Model\Api\Comments
17
 */
18
class EntityCommentData extends Model
19
{
20
    /** @var \Apps\ActiveRecord\CommentPost|\Apps\ActiveRecord\CommentAnswer $_record */
21
    private $_record;
22
    private $_type;
23
24
    /**
25
     * EntityCommentPostData constructor. Pass inside the model active record object of comment post or answer
26
     * @param $record
27
     * @throws JsonException
28
     */
29
    public function __construct($record)
30
    {
31
        $this->_record = $record;
32
        if ($this->_record instanceof CommentPost) {
33
            $this->_type = 'post';
34
        } elseif ($this->_record instanceof CommentAnswer) {
35
            $this->_type = 'answer';
36
        } else {
37
            throw new JsonException('Unknown comment request');
38
        }
39
        parent::__construct();
40
    }
41
42
    /**
43
     * Prepare output comment post information array
44
     * @return array|null
45
     */
46
    public function make()
47
    {
48
        if ($this->_record === null) {
49
            return null;
50
        }
51
52
        // build user data
53
        $userName = __('Unknown');
54
        $userAvatar = App::$Alias->scriptUrl . '/upload/user/avatar/small/default.jpg';
55
        $userObject = $this->_record->getUser();
56
        if ($userObject !== null) {
57
            $userName = $userObject->getProfile()->nick;
58
            $userAvatar = $userObject->getProfile()->getAvatarUrl('small');
59
        } else {
60
            if (!Str::likeEmpty($this->_record->guest_name)) {
0 ignored issues
show
Bug introduced by
It seems like $this->_record->guest_name can also be of type string; however, Ffcms\Core\Helper\Type\Str::likeEmpty() does only seem to accept null, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
61
                $userName = App::$Security->strip_tags($this->_record->guest_name);
62
            }
63
        }
64
65
        // return output json data
66
        $res = [
67
            'type' => $this->_type,
68
            'id' => $this->_record->id,
69
            'text' => $this->_record->message,
70
            'date' => Date::convertToDatetime($this->_record->created_at, Date::FORMAT_TO_HOUR),
71
            'user' => [
72
                'id' => $this->_record->user_id,
73
                'name' => $userName,
74
                'avatar' => $userAvatar
75
            ]
76
        ];
77
78
        if ($this->_type === 'post' && method_exists($this->_record, 'getAnswerCount')) {
79
            $res['answers'] = $this->_record->getAnswerCount();
80
        } elseif ($this->_type === 'answer') {
81
            $res['comment_id'] = $this->_record->comment_id;
0 ignored issues
show
Documentation introduced by
The property comment_id does not exist on object<Apps\ActiveRecord\CommentPost>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
82
        }
83
84
        return $res;
85
    }
86
87
}