Notify::send()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 5
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
namespace Dongdavid\Notify;
4
5
class Notify
6
{
7
    protected static $types = [
8
        'email' => 'Email',
9
        'wechatoffical' => 'WechatOffical',
10
        'wechatwork' => 'WechatWork',
11
        'miniprogram' => 'MiniProgram',
12
        'alisms' => 'AliSms',
13
    ];
14
15
    /**
16
     * 获取可发送消息类型.
17
     *
18
     * @return string[]
19
     */
20
    public static function getTypes()
21
    {
22
        return array_keys(self::$types);
23
    }
24
25
    private static function buildConnector($type)
26
    {
27
        $type = strtolower($type);
28
        if (!isset(self::$types[$type])) {
29
            throw new \Exception('错误的通知类型:'.$type);
30
        }
31
32
        $class = false !== strpos($type, '\\') ? $type : '\\Dongdavid\\Notify\\sender\\'.self::$types[$type];
33
34
        return new $class();
35
    }
36
37
    public static function send($data)
38
    {
39
        return self::buildConnector($data['type'])
40
            ->setConfig($data['config'])
41
            ->send($data['data']);
42
    }
43
44
    public static function name($type)
45
    {
46
        return self::buildConnector($type);
47
    }
48
}
49