55use Drupal \Component \Serialization \SerializationInterface ;
66use Drupal \Core \Config \ConfigFactoryInterface ;
77use Drupal \Core \Entity \EntityInterface ;
8+ use Drupal \Core \Utility \Error ;
89use GuzzleHttp \ClientInterface ;
10+ use GuzzleHttp \Exception \BadResponseException ;
911use GuzzleHttp \Exception \RequestException ;
1012use GuzzleHttp \Psr7 ;
1113use GuzzleHttp \Psr7 \Request ;
14+ use Psr \Log \LoggerInterface ;
1215
1316/**
1417 * Class OutgoingWebhook.
@@ -49,35 +52,42 @@ class OutgoingWebhook {
4952 */
5053 protected $ config ;
5154
55+ /**
56+ * @var \Psr\Log\LoggerInterface
57+ */
58+ protected $ logger ;
59+
5260 /**
5361 * Constructs a new OutgoingWebhook object.
5462 */
5563 public function __construct (
5664 ClientInterface $ http_client ,
5765 SerializationInterface $ serialization_json ,
58- ConfigFactoryInterface $ config_factory
66+ ConfigFactoryInterface $ config_factory ,
67+ LoggerInterface $ logger
5968 ) {
6069 $ this ->httpClient = $ http_client ;
6170 $ this ->serializationJson = $ serialization_json ;
6271 $ this ->config = $ config_factory ->get ('http_webhooks.outgoing_config ' );
72+ $ this ->logger = $ logger ;
6373 }
6474
6575 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 " );
6979
7080 // 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 );
7383 };
7484 }
7585
76- public function post () {
86+ private function post ($ entityType , $ event ) {
7787 $ secret = $ this ->config ->get ('http_webhooks.outgoing.secret ' );
7888 $ url = $ this ->config ->get ('http_webhooks.outgoing.url ' );
7989 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. ' );
8191 return ;
8292 }
8393
@@ -86,12 +96,17 @@ public function post() {
8696 'json ' => ['secret ' => $ secret ]
8797 ]);
8898 } 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+ }
90105 return ;
91106 }
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+ ]);
95111 }
96-
97112}
0 commit comments