Completed
Push — master ( 1df3bf...f204e2 )
by Xu
06:18
created

BaseMessage::getAttributes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * @link http://www.tintsoft.com/
4
 * @copyright Copyright (c) 2012 TintSoft Technology Co. Ltd.
5
 * @license http://www.tintsoft.com/license/
6
 */
7
8
namespace yuncms\broadcast;
9
10
11
use Yii;
12
use yii\base\BaseObject;
13
use yii\base\ErrorHandler;
14
use yuncms\helpers\Json;
15
16
/**
17
 * Class BaseMessage
18
 *
19
 * @author Tongle Xu <[email protected]>
20
 * @since 3.0
21
 */
22
abstract class BaseMessage extends BaseObject implements MessageInterface
23
{
24
    /**
25
     * @var BroadcastInterface the broadcast instance that created this message.
26
     * For independently created messages this is `null`.
27
     */
28
    public $broadcast;
29
30
    /**
31
     * @var array
32
     */
33
    protected $body;
34
35
    /**
36
     * @var string message tag.
37
     */
38
    protected $tag;
39
40
    /**
41
     * @var null|array message attributes.
42
     */
43
    protected $attributes;
44
45
    /**
46
     * get the message tag
47
     * @return string
48
     */
49
    public function getTag()
50
    {
51
        return $this->tag;
52
    }
53
54
    /**
55
     * set the message tag
56
     * @param string $tag
57
     * @return BaseMessage
58
     */
59
    public function setTag($tag)
60
    {
61
        $this->tag = $tag;
62
        return $this;
63
    }
64
65
    /**
66
     * @return array|null
67
     */
68
    public function getAttributes()
69
    {
70
        return $this->attributes;
71
    }
72
73
    /**
74
     * set message attributes
75
     * @param array $attributes
76
     * @return BaseMessage
77
     */
78
    public function setAttributes(array $attributes)
79
    {
80
        $this->attributes = $attributes;
81
        return $this;
82
    }
83
84
    /**
85
     * Sends this broadcast message.
86
     * @param BroadcastInterface $broadcast the broadcast that should be used to send this message.
87
     * If no broadcast is given it will first check if [[broadcast]] is set and if not,
88
     * the "broadcast" application component will be used instead.
89
     * @return bool whether this message is sent successfully.
90
     */
91
    public function send(BroadcastInterface $broadcast = null)
92
    {
93
        if ($broadcast === null && $this->broadcast === null) {
94
            $broadcast = Yii::$app->getBroadcast();
95
        } elseif ($broadcast === null) {
96
            $broadcast = $this->broadcast;
97
        }
98
99
        return $broadcast->send($this);
0 ignored issues
show
Bug introduced by
The method send() does not exist on yuncms\broadcast\BroadcastInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to yuncms\broadcast\BroadcastInterface. ( Ignorable by Annotation )

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

99
        return $broadcast->/** @scrutinizer ignore-call */ send($this);
Loading history...
100
    }
101
102
    /**
103
     * Returns string representation of this message.
104
     * @return string the string representation of this message.
105
     */
106
    public function toString()
107
    {
108
        return Json::encode($this->body);
109
    }
110
111
    /**
112
     * PHP magic method that returns the string representation of this object.
113
     * @return string the string representation of this object.
114
     */
115
    public function __toString()
116
    {
117
        // __toString cannot throw exception
118
        // use trigger_error to bypass this limitation
119
        try {
120
            return $this->toString();
121
        } catch (\Exception $e) {
122
            ErrorHandler::convertExceptionToError($e);
123
            return '';
124
        }
125
    }
126
}