NotificationTrait   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 6
eloc 13
c 2
b 0
f 1
dl 0
loc 42
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A broadcastOn() 0 14 4
A exportFor() 0 6 2
1
<?php
2
/**
3
 * @copyright Anton Tuyakhov <[email protected]>
4
 */
5
6
namespace tuyakhov\notifications;
7
8
use tuyakhov\notifications\messages\AbstractMessage;
9
use yii\helpers\Inflector;
10
11
trait NotificationTrait
12
{
13
    /**
14
     * @return array
15
     */
16
    public function broadcastOn()
17
    {
18
        $channels = [];
19
        $methods = get_class_methods($this);
20
        foreach ($methods as $method) {
21
            if (strpos($method, 'exportFor') === false) {
22
                continue;
23
            }
24
            $channel = str_replace('exportFor', '', $method);
25
            if (!empty($channel)) {
26
                $channels[] = Inflector::camel2id($channel);
27
            }
28
        }
29
        return $channels;
30
    }
31
32
    /**
33
     * Determines on which channels the notification will be delivered.
34
     * ```php
35
     * public function exportForMail() {
36
     *      return Yii::createObject([
37
     *          'class' => 'tuyakhov\notifications\messages\MailMessage',
38
     *          'view' => ['html' => 'welcome'],
39
     *          'viewData' => [...]
40
     *      ])
41
     * }
42
     * ```
43
     * @param $channel
44
     * @return AbstractMessage
45
     * @throws \InvalidArgumentException
46
     */
47
    public function exportFor($channel)
48
    {
49
        if (method_exists($this, $method = 'exportFor'.Inflector::id2camel($channel))) {
50
            return $this->{$method}();
51
        }
52
        throw new \InvalidArgumentException("Can not find message export for chanel `{$channel}`");
53
    }
54
55
}