Passed
Push — master ( f17217...1fc1f2 )
by Dong
08:32
created

Notify::getTypes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
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
     * @return string[]
18
     */
19
    public static function getTypes(): array
20
    {
21
        return array_keys(self::$types);
22
    }
23
    private static function buildConnector($type)
24
    {
25
        $type = strtolower($type);
26
        if ( ! isset(self::$types[$type])) {
27
            throw new \Exception('错误的通知类型:'.$type);
28
        }
29
30
        $class = false !== strpos($type, '\\') ? $type : '\\Dongdavid\\Notify\\sender\\'.self::$types[$type];
31
32
        return new $class();
33
    }
34
35
    public static function send($data)
36
    {
37
        return self::buildConnector($data['type'])
38
            ->setConfig($data['config'])
39
            ->send($data['data']);
40
    }
41
42
    public static function name($type)
43
    {
44
        return self::buildConnector($type);
45
    }
46
}
47