8
8
use phpbu \App \Result ;
9
9
use phpbu \App \Util \Arr ;
10
10
use phpbu \App \Util \Path ;
11
+ use phpbu \App \Util \Str ;
11
12
use Throwable ;
12
13
13
14
/**
@@ -91,6 +92,20 @@ class Webhook implements Listener, Logger
91
92
'application/xml ' => '\\phpbu \\App \\Log \\ResultFormatter \\Xml '
92
93
];
93
94
95
+ /**
96
+ * Call webhook only if errors occur
97
+ *
98
+ * @var bool
99
+ */
100
+ private bool $ sendOnlyOnError = false ;
101
+
102
+ /**
103
+ * Call the webhook while simulating
104
+ *
105
+ * @var bool
106
+ */
107
+ private bool $ sendSimulating = false ;
108
+
94
109
/**
95
110
* Constructor will only set the start time to be able to log duration
96
111
*/
@@ -140,6 +155,8 @@ public function setup(array $options)
140
155
throw new Exception ('webhook URI is invalid ' );
141
156
}
142
157
158
+ $ this ->sendOnlyOnError = Str::toBoolean (Arr::getValue ($ options , 'sendOnlyOnError ' ), false );
159
+ $ this ->sendSimulating = Str::toBoolean (Arr::getValue ($ options , 'sendOnSimulation ' ), true );
143
160
$ this ->uri = $ options ['uri ' ];
144
161
$ this ->method = Arr::getValue ($ options , 'method ' , 'GET ' );
145
162
$ this ->username = Arr::getValue ($ options , 'username ' , '' );
@@ -156,7 +173,10 @@ public function setup(array $options)
156
173
*/
157
174
public function onPhpbuEnd (Event \App \End $ event )
158
175
{
159
- $ result = $ event ->getResult ();
176
+ $ result = $ event ->getResult ();
177
+ if ($ this ->shouldWebhookBeCalled ($ result ) === false ) {
178
+ return ;
179
+ }
160
180
$ data = $ this ->getQueryStringData ($ result );
161
181
$ uri = $ this ->method === 'GET ' ? $ this ->buildGetUri ($ data ) : $ this ->uri ;
162
182
$ formatter = $ this ->getBodyFormatter ();
@@ -259,4 +279,15 @@ protected function fireRequest(string $uri, string $body = '')
259
279
throw new Exception ('could not reach webhook: ' . $ this ->uri );
260
280
}
261
281
}
282
+
283
+ /**
284
+ * Should a webhook be called
285
+ *
286
+ * @param \phpbu\App\Result $result
287
+ * @return bool
288
+ */
289
+ protected function shouldWebhookBeCalled (Result $ result ) : bool
290
+ {
291
+ return (!$ this ->sendOnlyOnError || !$ result ->allOk ()) && ($ this ->sendSimulating || !$ this ->isSimulation );
292
+ }
262
293
}
0 commit comments