-
Notifications
You must be signed in to change notification settings - Fork 0
/
PushallBehavior.php
93 lines (79 loc) · 1.96 KB
/
PushallBehavior.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
<?php
/**
* Created by PhpStorm.
* User: fgorsky
* Date: 06.02.17
* Time: 8:29
*/
namespace fgh151\pushall;
use platx\pushall\exceptions\InvalidIdException;
use platx\pushall\exceptions\InvalidKeyException;
use platx\pushall\exceptions\RequiredParameterException;
use platx\pushall\PushAll;
use Yii;
use yii\behaviors\AttributeBehavior;
use yii\db\ActiveRecord;
class PushallBehavior extends AttributeBehavior
{
/**
* @var string Attribute contains title
*/
public $titleAttribute;
/**
* @var string Attribute contains message
*/
public $messageAttribute;
/**
* @var string Attribute contains url to view page
*/
public $url;
/**
* @var integer feed id. Default from params
*/
public $feedId;
/**
* @var string feed key. Default from params
*/
public $feedKey;
/**
* @var string Chanel type
*/
public $chanelType = PushAll::TYPE_BROADCAST;
/**
* @return array
*/
public function events()
{
return [
ActiveRecord::EVENT_AFTER_INSERT => 'pushallSend',
];
}
/**
* @inheritdoc
*/
public function init()
{
parent::init();
if (!$this->feedId || !$this->feedKey) {
$this->feedId = Yii::$app->params['PushallFeedId'];
$this->feedKey = Yii::$app->params['PushallKey'];
}
}
/**
* @throws RequiredParameterException
* @throws InvalidIdException
* @throws InvalidKeyException
*/
public function pushallSend()
{
$messageText = $this->messageAttribute;
$message = $this->url !== '' ? $this->url . "\n" : '';
$message .= $this->owner->$messageText;
$titleAttribute = $this->titleAttribute;
(new PushAll($this->feedId, $this->feedKey))->send(array(
'type' => $this->chanelType,
'title' => $this->owner->$titleAttribute,
'text' => $message
));
}
}