11<?php
22
33namespace Drupal \http_webhooks ;
4- use GuzzleHttp \ ClientInterface ;
4+
55use Drupal \Component \Serialization \SerializationInterface ;
66use Drupal \Core \Config \ConfigFactoryInterface ;
7+ use Drupal \Core \Entity \EntityInterface ;
8+ use GuzzleHttp \ClientInterface ;
9+ use GuzzleHttp \Exception \RequestException ;
10+ use GuzzleHttp \Psr7 ;
11+ use GuzzleHttp \Psr7 \Request ;
712
813/**
914 * Class OutgoingWebhook.
1015 */
1116class OutgoingWebhook {
17+ const EVENT_CREATE = "create " ;
18+ const EVENT_UPDATE = "update " ;
19+ const EVENT_DELETE = "delete " ;
20+
21+ const VALID_EVENTS = [
22+ 'entity:user:create ' => ['type ' => 'user ' , 'event ' => 'create ' ],
23+ 'entity:user:update ' => ['type ' => 'user ' , 'event ' => 'update ' ],
24+ 'entity:user:delete ' => ['type ' => 'user ' , 'event ' => 'delete ' ],
25+ 'entity:node:create ' => ['type ' => 'node ' , 'event ' => 'create ' ],
26+ 'entity:node:update ' => ['type ' => 'node ' , 'event ' => 'update ' ],
27+ 'entity:node:delete ' => ['type ' => 'node ' , 'event ' => 'delete ' ],
28+ 'entity:comment:create ' => ['type ' => 'comment ' , 'event ' => 'create ' ],
29+ 'entity:comment:update ' => ['type ' => 'comment ' , 'event ' => 'update ' ],
30+ 'entity:comment:delete ' => ['type ' => 'comment ' , 'event ' => 'delete ' ],
31+ ];
1232
1333 /**
1434 * GuzzleHttp\ClientInterface definition.
@@ -25,9 +45,9 @@ class OutgoingWebhook {
2545 protected $ serializationJson ;
2646
2747 /**
28- * @var \Drupal\Core\Config\ConfigFactoryInterface
48+ * @var \Drupal\Core\Config\ImmutableConfig
2949 */
30- protected $ configFactory ;
50+ protected $ config ;
3151
3252 /**
3353 * Constructs a new OutgoingWebhook object.
@@ -39,8 +59,39 @@ public function __construct(
3959 ) {
4060 $ this ->httpClient = $ http_client ;
4161 $ this ->serializationJson = $ serialization_json ;
42- $ this ->configFactory = $ config_factory ;
62+ $ this ->config = $ config_factory ->get ('http_webhooks.outgoing_config ' );
63+ }
64+
65+ 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 " );
69+
70+ // only post for entities and events allowed in the configuration
71+ if (in_array ($ eventString , $ allowed_events )) {
72+ $ this ->post ();
73+ };
4374 }
75+
76+ public function post () {
77+ $ secret = $ this ->config ->get ('http_webhooks.outgoing.secret ' );
78+ $ url = $ this ->config ->get ('http_webhooks.outgoing.url ' );
79+ if (empty ($ secret ) || empty ($ url )) {
80+ // TODO: log a error message: these configuration are necessary,
81+ return ;
82+ }
83+
84+ try {
85+ $ response = $ this ->httpClient ->request ('POST ' , $ url , [
86+ 'json ' => ['secret ' => $ secret ]
87+ ]);
88+ } catch (RequestException $ e ) {
89+ // TODO: log a error message: the request failed
90+ return ;
91+ }
92+ $ body = $ response ->getBody ();
93+ // TODO: log a success message with the response payload
94+
4495 }
4596
4697}
0 commit comments