5
5
use Drupal \Component \Serialization \SerializationInterface ;
6
6
use Drupal \Core \Config \ConfigFactoryInterface ;
7
7
use Drupal \Core \Entity \EntityInterface ;
8
+ use Drupal \Core \Utility \Error ;
8
9
use GuzzleHttp \ClientInterface ;
10
+ use GuzzleHttp \Exception \BadResponseException ;
9
11
use GuzzleHttp \Exception \RequestException ;
10
12
use GuzzleHttp \Psr7 ;
11
13
use GuzzleHttp \Psr7 \Request ;
14
+ use Psr \Log \LoggerInterface ;
12
15
13
16
/**
14
17
* Class OutgoingWebhook.
@@ -49,35 +52,42 @@ class OutgoingWebhook {
49
52
*/
50
53
protected $ config ;
51
54
55
+ /**
56
+ * @var \Psr\Log\LoggerInterface
57
+ */
58
+ protected $ logger ;
59
+
52
60
/**
53
61
* Constructs a new OutgoingWebhook object.
54
62
*/
55
63
public function __construct (
56
64
ClientInterface $ http_client ,
57
65
SerializationInterface $ serialization_json ,
58
- ConfigFactoryInterface $ config_factory
66
+ ConfigFactoryInterface $ config_factory ,
67
+ LoggerInterface $ logger
59
68
) {
60
69
$ this ->httpClient = $ http_client ;
61
70
$ this ->serializationJson = $ serialization_json ;
62
71
$ this ->config = $ config_factory ->get ('http_webhooks.outgoing_config ' );
72
+ $ this ->logger = $ logger ;
63
73
}
64
74
65
75
public function handle_event (EntityInterface $ entity , $ event ) {
66
- $ type = $ entity ->getEntityTypeId ();
67
- $ eventString = "entity: $ type : $ event " ;
68
- $ allowed_events = $ this ->config ->get ("http_webhooks.outgoing.events " );
76
+ $ entityType = $ entity ->getEntityTypeId ();
77
+ $ eventString = "entity: $ entityType : $ event " ;
78
+ $ allowedEvents = $ this ->config ->get ("http_webhooks.outgoing.events " );
69
79
70
80
// only post for entities and events allowed in the configuration
71
- if (in_array ($ eventString , $ allowed_events )) {
72
- $ this ->post ();
81
+ if (in_array ($ eventString , $ allowedEvents )) {
82
+ $ this ->post ($ entityType , $ event );
73
83
};
74
84
}
75
85
76
- public function post () {
86
+ private function post ($ entityType , $ event ) {
77
87
$ secret = $ this ->config ->get ('http_webhooks.outgoing.secret ' );
78
88
$ url = $ this ->config ->get ('http_webhooks.outgoing.url ' );
79
89
if (empty ($ secret ) || empty ($ url )) {
80
- // TODO: log a error message: these configuration are necessary,
90
+ $ this -> logger -> critical ( ' Cannot send the webhook since either the secret or the url is undefined. ' );
81
91
return ;
82
92
}
83
93
@@ -86,12 +96,17 @@ public function post() {
86
96
'json ' => ['secret ' => $ secret ]
87
97
]);
88
98
} catch (RequestException $ e ) {
89
- // TODO: log a error message: the request failed
99
+ $ variables = Error::decodeException ($ e );
100
+ if ($ e instanceof BadResponseException) {
101
+ $ this ->logger ->error ('Received error response after sending the webhook: %type: @message in %function (line %line of %file). ' , $ variables );
102
+ } else {
103
+ $ this ->logger ->error ('There was an error when trying to send the webhook: %type: @message in %function (line %line of %file). ' , $ variables );
104
+ }
90
105
return ;
91
106
}
92
- $ body = $ response ->getBody ();
93
- // TODO: log a success message with the response payload
94
-
107
+ $ this ->logger ->info ('Webhook sent and acknowledged after %entity_type %event event. ' , [
108
+ '%event ' => $ event ,
109
+ '%entity_type ' => $ entityType
110
+ ]);
95
111
}
96
-
97
112
}
0 commit comments