1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Zenstruck\ScheduleBundle\Schedule\Extension; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\HttpClient\HttpClient; |
6
|
|
|
use Symfony\Contracts\HttpClient\HttpClientInterface; |
7
|
|
|
use Zenstruck\ScheduleBundle\Event\AfterScheduleEvent; |
8
|
|
|
use Zenstruck\ScheduleBundle\Event\AfterTaskEvent; |
9
|
|
|
use Zenstruck\ScheduleBundle\Event\BeforeScheduleEvent; |
10
|
|
|
use Zenstruck\ScheduleBundle\Event\BeforeTaskEvent; |
11
|
|
|
use Zenstruck\ScheduleBundle\Schedule\Extension; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @author Kevin Bond <[email protected]> |
15
|
|
|
*/ |
16
|
|
|
final class PingExtension extends SelfHandlingExtension |
17
|
|
|
{ |
18
|
|
|
private $hook; |
19
|
|
|
private $url; |
20
|
|
|
private $method; |
21
|
|
|
private $options; |
22
|
|
|
private $httpClient; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @param array $options See HttpClientInterface::OPTIONS_DEFAULTS |
26
|
|
|
*/ |
27
|
6 |
|
private function __construct(string $hook, string $url, string $method = 'GET', array $options = []) |
28
|
|
|
{ |
29
|
6 |
|
if (!\interface_exists(HttpClientInterface::class)) { |
30
|
|
|
throw new \LogicException(\sprintf('Symfony HttpClient is required to use the "%s" extension. Install with "composer require symfony/http-client".', static::class)); |
31
|
|
|
} |
32
|
|
|
|
33
|
6 |
|
$this->hook = $hook; |
34
|
6 |
|
$this->url = $url; |
35
|
6 |
|
$this->method = $method; |
36
|
6 |
|
$this->options = $options; |
37
|
6 |
|
} |
38
|
|
|
|
39
|
2 |
|
public function __toString(): string |
40
|
|
|
{ |
41
|
2 |
|
return "{$this->hook}, ping \"{$this->url}\""; |
42
|
|
|
} |
43
|
|
|
|
44
|
3 |
|
public function beforeSchedule(BeforeScheduleEvent $event): void |
45
|
|
|
{ |
46
|
3 |
|
$this->pingIf(self::SCHEDULE_BEFORE); |
47
|
3 |
|
} |
48
|
|
|
|
49
|
3 |
|
public function afterSchedule(AfterScheduleEvent $event): void |
50
|
|
|
{ |
51
|
3 |
|
$this->pingIf(self::SCHEDULE_AFTER); |
52
|
3 |
|
} |
53
|
|
|
|
54
|
2 |
|
public function onScheduleSuccess(AfterScheduleEvent $event): void |
55
|
|
|
{ |
56
|
2 |
|
$this->pingIf(self::SCHEDULE_SUCCESS); |
57
|
2 |
|
} |
58
|
|
|
|
59
|
2 |
|
public function onScheduleFailure(AfterScheduleEvent $event): void |
60
|
|
|
{ |
61
|
2 |
|
$this->pingIf(self::SCHEDULE_FAILURE); |
62
|
2 |
|
} |
63
|
|
|
|
64
|
3 |
|
public function beforeTask(BeforeTaskEvent $event): void |
65
|
|
|
{ |
66
|
3 |
|
$this->pingIf(self::TASK_BEFORE); |
67
|
3 |
|
} |
68
|
|
|
|
69
|
3 |
|
public function afterTask(AfterTaskEvent $event): void |
70
|
|
|
{ |
71
|
3 |
|
$this->pingIf(self::TASK_AFTER); |
72
|
3 |
|
} |
73
|
|
|
|
74
|
2 |
|
public function onTaskSuccess(AfterTaskEvent $event): void |
75
|
|
|
{ |
76
|
2 |
|
$this->pingIf(self::TASK_SUCCESS); |
77
|
2 |
|
} |
78
|
|
|
|
79
|
2 |
|
public function onTaskFailure(AfterTaskEvent $event): void |
80
|
|
|
{ |
81
|
2 |
|
$this->pingIf(self::TASK_FAILURE); |
82
|
2 |
|
} |
83
|
|
|
|
84
|
3 |
|
public static function taskBefore(string $url, string $method = 'GET', array $options = []): self |
85
|
|
|
{ |
86
|
3 |
|
return new self(Extension::TASK_BEFORE, $url, $method, $options); |
87
|
|
|
} |
88
|
|
|
|
89
|
3 |
|
public static function taskAfter(string $url, string $method = 'GET', array $options = []): self |
90
|
|
|
{ |
91
|
3 |
|
return new self(Extension::TASK_AFTER, $url, $method, $options); |
92
|
|
|
} |
93
|
|
|
|
94
|
3 |
|
public static function taskSuccess(string $url, string $method = 'GET', array $options = []): self |
95
|
|
|
{ |
96
|
3 |
|
return new self(Extension::TASK_SUCCESS, $url, $method, $options); |
97
|
|
|
} |
98
|
|
|
|
99
|
5 |
|
public static function taskFailure(string $url, string $method = 'GET', array $options = []): self |
100
|
|
|
{ |
101
|
5 |
|
return new self(Extension::TASK_FAILURE, $url, $method, $options); |
102
|
|
|
} |
103
|
|
|
|
104
|
3 |
|
public static function scheduleBefore(string $url, string $method = 'GET', array $options = []): self |
105
|
|
|
{ |
106
|
3 |
|
return new self(Extension::SCHEDULE_BEFORE, $url, $method, $options); |
107
|
|
|
} |
108
|
|
|
|
109
|
3 |
|
public static function scheduleAfter(string $url, string $method = 'GET', array $options = []): self |
110
|
|
|
{ |
111
|
3 |
|
return new self(Extension::SCHEDULE_AFTER, $url, $method, $options); |
112
|
|
|
} |
113
|
|
|
|
114
|
3 |
|
public static function scheduleSuccess(string $url, string $method = 'GET', array $options = []): self |
115
|
|
|
{ |
116
|
3 |
|
return new self(Extension::SCHEDULE_SUCCESS, $url, $method, $options); |
117
|
|
|
} |
118
|
|
|
|
119
|
3 |
|
public static function scheduleFailure(string $url, string $method = 'GET', array $options = []): self |
120
|
|
|
{ |
121
|
3 |
|
return new self(Extension::SCHEDULE_FAILURE, $url, $method, $options); |
122
|
|
|
} |
123
|
|
|
|
124
|
4 |
|
public function setHttpClient(HttpClientInterface $httpClient): self |
125
|
|
|
{ |
126
|
4 |
|
$this->httpClient = $httpClient; |
127
|
|
|
|
128
|
4 |
|
return $this; |
129
|
|
|
} |
130
|
|
|
|
131
|
4 |
|
private function pingIf(string $expectedHook): void |
132
|
|
|
{ |
133
|
4 |
|
if ($expectedHook === $this->hook) { |
134
|
4 |
|
$this->getHttpClient()->request($this->method, $this->url, $this->options)->getStatusCode(); |
135
|
|
|
} |
136
|
4 |
|
} |
137
|
|
|
|
138
|
4 |
|
private function getHttpClient(): HttpClientInterface |
139
|
|
|
{ |
140
|
4 |
|
return $this->httpClient ?: $this->httpClient = HttpClient::create(); |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|