1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @copyright Anton Tuyakhov <[email protected]> |
4
|
|
|
*/ |
5
|
|
|
|
6
|
|
|
namespace tuyakhov\notifications\behaviors; |
7
|
|
|
|
8
|
|
|
|
9
|
|
|
use tuyakhov\notifications\NotifiableInterface; |
10
|
|
|
use tuyakhov\notifications\NotificationInterface; |
11
|
|
|
use tuyakhov\notifications\Notifier; |
12
|
|
|
use yii\base\Behavior; |
13
|
|
|
use yii\base\Event; |
14
|
|
|
use yii\di\Instance; |
15
|
|
|
|
16
|
|
|
class NotifiableBehavior extends Behavior |
17
|
|
|
{ |
18
|
|
|
public $notifications = []; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var Notifier |
22
|
|
|
*/ |
23
|
|
|
public $notifier = 'notifier'; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @throws \yii\base\InvalidConfigException |
27
|
|
|
*/ |
28
|
|
|
public function init() |
29
|
|
|
{ |
30
|
|
|
parent::init(); |
31
|
|
|
$this->notifier = Instance::ensure($this->notifier, 'tuyakhov\notifications\Notifier'); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @inheritdoc |
36
|
|
|
*/ |
37
|
|
|
public function attach($owner) |
38
|
|
|
{ |
39
|
|
|
$this->owner = $owner; |
40
|
|
View Code Duplication |
foreach ($this->notifications as $event => $notifications) { |
|
|
|
|
41
|
|
|
if (!is_array($notifications)) { |
42
|
|
|
$notifications = [$notifications]; |
43
|
|
|
} |
44
|
|
|
foreach ($notifications as $notification) { |
45
|
|
|
$owner->on($event, [$this, 'handle'], ['notification' => $notification]); |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @inheritdoc |
52
|
|
|
*/ |
53
|
|
|
public function detach() |
54
|
|
|
{ |
55
|
|
|
if ($this->owner) { |
56
|
|
View Code Duplication |
foreach ($this->notifications as $event => $notifications) { |
|
|
|
|
57
|
|
|
if (!is_array($notifications)) { |
58
|
|
|
$notifications = [$notifications]; |
59
|
|
|
} |
60
|
|
|
foreach ($notifications as $notification) { |
61
|
|
|
$this->owner->off($event, [$this, 'handle']); |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
$this->owner = null; |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Handles the event using public properties. |
70
|
|
|
* @param Event $event |
71
|
|
|
* @throws \yii\base\InvalidConfigException |
72
|
|
|
*/ |
73
|
|
|
public function handle(Event $event) |
74
|
|
|
{ |
75
|
|
|
if (!isset($event->data['notification'])) { |
76
|
|
|
throw new \InvalidArgumentException('Can not find `notification` in event data'); |
77
|
|
|
} |
78
|
|
|
if (!$this->owner instanceof NotifiableInterface) { |
79
|
|
|
throw new \RuntimeException('Owner should implement `NotifiableInterface`'); |
80
|
|
|
} |
81
|
|
|
$notification = $event->data['notification']; |
82
|
|
|
$config = []; |
|
|
|
|
83
|
|
|
foreach (get_object_vars($event) as $param => $value) { |
84
|
|
|
$config[$param] = $value; |
85
|
|
|
} |
86
|
|
|
$config['class'] = $notification; |
87
|
|
|
/** |
88
|
|
|
* @var $notification NotificationInterface |
89
|
|
|
*/ |
90
|
|
|
$notification = \Yii::createObject($config); |
91
|
|
|
$this->notifier->send($this->owner, $notification); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
|
95
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.