Manager   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 14
c 1
b 0
f 0
dl 0
loc 52
ccs 0
cts 14
cp 0
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A registerAdaptor() 0 3 1
A sendNotification() 0 13 3
A send() 0 4 1
1
<?php
2
3
namespace App\Notifier;
4
5
use App\Facades\Log;
6
use Ronanchilvers\Foundation\Config;
7
8
/**
9
 * Notifier manager class
10
 *
11
 * @author Ronan Chilvers <[email protected]>
12
 */
13
class Manager
14
{
15
    /**
16
     * @var array
17
     */
18
    protected $adaptors = [];
19
20
    /**
21
     * Register a notifier with the manager
22
     *
23
     * @param \App\Notifier\AdaptorInterface
24
     * @author Ronan Chilvers <[email protected]>
25
     */
26
    public function registerAdaptor(AdaptorInterface $adaptor)
27
    {
28
        $this->adaptors[get_class($adaptor)] = $adaptor;
29
    }
30
31
    /**
32
     * Send a notification through all adaptors
33
     *
34
     * @param \App\Notifier\Notification $notification
35
     * @param array $options An array of options for all notifiers, keyed by notifier key
36
     * @author Ronan Chilvers <[email protected]>
37
     */
38
    public function sendNotification(Notification $notification, array $options)
39
    {
40
        Log::debug(sprintf('Sending notification to %d adaptors', count($this->adaptors)), [
41
            'message' => $notification->getMessage(),
42
        ]);
43
        foreach ($this->adaptors as $adaptor) {
44
            Log::debug(sprintf('Using %s adaptor', get_class($adaptor)));
45
            $key = $adaptor->getKey();
46
            if (!isset($options[$key])) {
47
                continue;
48
            }
49
            $adaptorOptions = $options[$key];
50
            $adaptor->send($notification, $adaptorOptions);
51
        }
52
    }
53
54
    /**
55
     * Send a string message
56
     *
57
     * @param string $message
58
     * @param array $options An array of options for all notifiers, keyed by notifier key
59
     * @author Ronan Chilvers <[email protected]>
60
     */
61
    public function send($message, array $options)
62
    {
63
        $notification = new Notification($message);
64
        $this->sendNotification($notification, $options);
65
    }
66
}
67