1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @copyright Anton Tuyakhov <[email protected]> |
4
|
|
|
*/ |
5
|
|
|
|
6
|
|
|
namespace tuyakhov\notifications; |
7
|
|
|
|
8
|
|
|
|
9
|
|
|
use yii\base\Behavior; |
10
|
|
|
use yii\base\Event; |
11
|
|
|
use yii\di\Instance; |
12
|
|
|
|
13
|
|
|
class NotifiableBehavior extends Behavior |
14
|
|
|
{ |
15
|
|
|
public $notifications = []; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var Notifier |
19
|
|
|
*/ |
20
|
|
|
public $notifier = 'notifier'; |
21
|
|
|
|
22
|
|
|
public function init() |
23
|
|
|
{ |
24
|
|
|
parent::init(); |
25
|
|
|
$this->notifier = Instance::of($this->notifier); |
|
|
|
|
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @inheritdoc |
30
|
|
|
*/ |
31
|
|
|
public function attach($owner) |
32
|
|
|
{ |
33
|
|
|
$this->owner = $owner; |
34
|
|
View Code Duplication |
foreach ($this->notifications as $event => $notifications) { |
|
|
|
|
35
|
|
|
if (!is_array($notifications)) { |
36
|
|
|
$notifications = [$notifications]; |
37
|
|
|
} |
38
|
|
|
foreach ($notifications as $notification) { |
39
|
|
|
$owner->on($event, [$this, 'handle'], ['notification' => $notification]); |
40
|
|
|
} |
41
|
|
|
} |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @inheritdoc |
46
|
|
|
*/ |
47
|
|
|
public function detach() |
48
|
|
|
{ |
49
|
|
|
if ($this->owner) { |
50
|
|
View Code Duplication |
foreach ($this->notifications as $event => $notifications) { |
|
|
|
|
51
|
|
|
if (!is_array($notifications)) { |
52
|
|
|
$notifications = [$notifications]; |
53
|
|
|
} |
54
|
|
|
foreach ($notifications as $notification) { |
55
|
|
|
$this->owner->off($event, [$this, 'handle']); |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
$this->owner = null; |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Handles the event using public properties. |
64
|
|
|
* @param Event $event |
65
|
|
|
* @throws \InvalidArgumentException |
66
|
|
|
* @throws \RuntimeException |
67
|
|
|
*/ |
68
|
|
|
public function handle(Event $event) |
69
|
|
|
{ |
70
|
|
|
if (!isset($event->data['notification'])) { |
71
|
|
|
throw new \InvalidArgumentException('Can not find `notification` in event data'); |
72
|
|
|
} |
73
|
|
|
if (!$this->owner instanceof NotifiableInterface) { |
74
|
|
|
throw new \RuntimeException('Owner should implement `NotifiableInterface`'); |
75
|
|
|
} |
76
|
|
|
$notification = $event->data['notification']; |
77
|
|
|
$config = []; |
|
|
|
|
78
|
|
|
foreach (get_object_vars($event) as $param) { |
79
|
|
|
$config[$param] = $event->$param; |
80
|
|
|
} |
81
|
|
|
$config['class'] = $notification; |
82
|
|
|
/** |
83
|
|
|
* @var $notification NotificationInterface |
84
|
|
|
*/ |
85
|
|
|
$notification = \Yii::createObject($config); |
86
|
|
|
$this->notifier->send($this->owner, $notification); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
|
90
|
|
|
} |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: