|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Reallyli\Uninotify; |
|
4
|
|
|
|
|
5
|
|
|
use Ixudra\Curl\Facades\Curl; |
|
6
|
|
|
use Illuminate\Config\Repository; |
|
7
|
|
|
use Illuminate\Support\Facades\App; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Class UniquewayNotificationService. |
|
11
|
|
|
*/ |
|
12
|
|
|
class UniNotifyService |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* @var string |
|
16
|
|
|
*/ |
|
17
|
|
|
protected $logPrefix = '[Packages.Reallyli.UniNotify]'; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @var array |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $config; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Method description:__construct. |
|
26
|
|
|
* |
|
27
|
|
|
* @author reallyli <[email protected]> |
|
28
|
|
|
* |
|
29
|
|
|
* @since 18/6/29 |
|
30
|
|
|
* |
|
31
|
|
|
* @param Repository $config |
|
32
|
|
|
* |
|
33
|
|
|
* @return mixed |
|
|
|
|
|
|
34
|
|
|
* 返回值类型:string,array,object,mixed(多种,不确定的),void(无返回值) |
|
35
|
|
|
*/ |
|
36
|
|
|
public function __construct(Repository $config) |
|
37
|
|
|
{ |
|
38
|
|
|
$this->config = $config->get('uninotify'); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Method description:send. |
|
43
|
|
|
* |
|
44
|
|
|
* @author reallyli <[email protected]> |
|
45
|
|
|
* |
|
46
|
|
|
* @since 18/6/29 |
|
47
|
|
|
* |
|
48
|
|
|
* @param string $message |
|
49
|
|
|
* @param string $type |
|
50
|
|
|
* |
|
51
|
|
|
* @return mixed |
|
52
|
|
|
* 返回值类型:string,array,object,mixed(多种,不确定的),void(无返回值) |
|
53
|
|
|
*/ |
|
54
|
|
|
public function send(string $message, string $type) |
|
55
|
|
|
{ |
|
56
|
|
|
throw_unless( |
|
57
|
|
|
$this->config, |
|
58
|
|
|
'\Exception', |
|
59
|
|
|
$this->logPrefix.'Please publish the configuration file, php artisan vendor:publish' |
|
60
|
|
|
); |
|
61
|
|
|
|
|
62
|
|
|
$logLevel = in_array($this->config['logger_level'], ['error', 'info', 'debug']) ? |
|
63
|
|
|
$this->config['logger_level'] : |
|
64
|
|
|
'error'; |
|
65
|
|
|
|
|
66
|
|
|
if (is_array($this->config['execlude_notify_environment'])) { |
|
67
|
|
|
foreach ($this->config['execlude_notify_environment'] as $env) { |
|
68
|
|
|
if ($env === App::environment()) { |
|
69
|
|
|
logger()->info($this->logPrefix.'Current environment notify is excluded'); |
|
70
|
|
|
|
|
71
|
|
|
return true; |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
throw_unless( |
|
77
|
|
|
array_key_exists($type, $this->config['channel_url']), |
|
78
|
|
|
'\Exception', |
|
79
|
|
|
$this->logPrefix.'Channel url not found' |
|
80
|
|
|
); |
|
81
|
|
|
|
|
82
|
|
|
throw_unless( |
|
83
|
|
|
$this->config['channel_url'][$type], |
|
84
|
|
|
'\Exception', |
|
85
|
|
|
$this->logPrefix.'Channel url not set' |
|
86
|
|
|
); |
|
87
|
|
|
|
|
88
|
|
|
try { |
|
89
|
|
|
Curl::to($this->config['channel_url'][$type]) |
|
90
|
|
|
->withData(['text' => $message]) |
|
91
|
|
|
->asJson() |
|
92
|
|
|
->post(); |
|
93
|
|
|
} catch (\Exception $exception) { |
|
94
|
|
|
logger()->{$logLevel}($exception->getMessage()); |
|
95
|
|
|
throw_unless( |
|
96
|
|
|
$this->config['enabled_throw_exception'], |
|
97
|
|
|
'\Exception', |
|
98
|
|
|
$this->logPrefix.$exception->getMessage() |
|
99
|
|
|
); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
return true; |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
|
Adding a
@returnannotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.