1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Notimatica\Driver\Providers; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\Client; |
6
|
|
|
use GuzzleHttp\Pool; |
7
|
|
|
use Notimatica\Driver\Contracts\Notification; |
8
|
|
|
use Notimatica\Driver\Contracts\Subscriber; |
9
|
|
|
use Notimatica\Driver\Project; |
10
|
|
|
|
11
|
|
|
abstract class AbstractProvider |
12
|
|
|
{ |
13
|
|
|
const NAME = null; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var Project |
17
|
|
|
*/ |
18
|
|
|
protected $project; |
19
|
|
|
/** |
20
|
|
|
* @var array |
21
|
|
|
*/ |
22
|
|
|
protected $config = []; |
23
|
|
|
/** |
24
|
|
|
* @var Client |
25
|
|
|
*/ |
26
|
|
|
protected static $browser; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Create a new Provider. |
30
|
|
|
* |
31
|
|
|
* @param array $config |
32
|
|
|
*/ |
33
|
16 |
|
public function __construct(array $config = []) |
34
|
|
|
{ |
35
|
16 |
|
$this->config = $config; |
36
|
16 |
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Set project. |
40
|
|
|
* |
41
|
|
|
* @param Project $project |
42
|
|
|
* @return $this |
43
|
|
|
*/ |
44
|
15 |
|
public function setProject(Project $project) |
45
|
|
|
{ |
46
|
15 |
|
$this->project = $project; |
47
|
|
|
|
48
|
15 |
|
return $this; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Send notification. |
53
|
|
|
* |
54
|
|
|
* @param Notification $notification |
55
|
|
|
* @param Subscriber[] $subscribers |
56
|
|
|
*/ |
57
|
|
|
abstract public function send(Notification $notification, array $subscribers); |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Send request. |
61
|
|
|
* |
62
|
|
|
* @param array $subscribers |
63
|
|
|
* @param mixed $payload |
64
|
|
|
* @return \Generator |
65
|
|
|
*/ |
66
|
|
|
abstract protected function prepareRequests($subscribers, $payload = null); |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Send data to provider. |
70
|
|
|
* |
71
|
|
|
* @param array $subscribers |
72
|
|
|
* @param \Closure $success |
73
|
|
|
* @param \Closure $fail |
74
|
|
|
*/ |
75
|
2 |
|
protected function flush(array $subscribers, \Closure $success = null, \Closure $fail = null) |
76
|
|
|
{ |
77
|
2 |
|
if (is_null($success)) { |
78
|
|
|
$success = function ($response, $index) {}; |
|
|
|
|
79
|
|
|
} |
80
|
2 |
|
if (is_null($fail)) { |
81
|
|
|
$fail = function ($reason, $index) {}; |
|
|
|
|
82
|
|
|
} |
83
|
|
|
|
84
|
2 |
|
$pool = new Pool(static::$browser, $this->prepareRequests($subscribers), [ |
85
|
2 |
|
'concurrency' => $this->config['concurrent_requests'], |
86
|
2 |
|
'fulfilled' => $success, |
87
|
2 |
|
'rejected' => $fail, |
88
|
2 |
|
]); |
89
|
|
|
|
90
|
|
|
// Initiate the transfers and create a promise |
91
|
2 |
|
$promise = $pool->promise(); |
92
|
|
|
|
93
|
|
|
// Force the pool of requests to complete. |
94
|
2 |
|
$promise->wait(); |
95
|
2 |
|
} |
96
|
|
|
} |
97
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.