Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Webhooks.php
Go to the documentation of this file.
1 <?php
7 
8 use Magento\Mtf\Block\Block;
9 use Magento\Mtf\Client\ElementInterface;
10 use Magento\Mtf\Client\Locator;
11 
15 class Webhooks extends Block
16 {
22  private $webhookEventOptionsMap = [
23  'CASE_CREATION' => 'Case Creation',
24  'CASE_REVIEW' => 'Case Review',
25  'GUARANTEE_COMPLETION' => 'Guarantee Completion'
26  ];
27 
33  private $webhookAddedElement = '//table[@id="webhooks"]//tr[./td/span/text()="%s" and ./td/span/text()="%s"]';
34 
40  private $webhookUrl = '[id="webhookUrl"]';
41 
47  private $webhookTeamOption = './/select[@id="webhookTeams"]//option[text()="%s"]';
48 
54  private $webhookEventOption = 'select[id="webhookEvent"] option[value="%s"]';
55 
61  private $webhookAddButton = '[id="addWebhook"] [type=submit]';
62 
68  private $webhookDeleteButton = '[class*="webhook-delete"]';
69 
75  private $webhookDeleteConfirmButton = '[class="appriseOuter"] button[value="ok"]';
76 
83  public function create($team)
84  {
85  $handlerUrl = $this->getHandlerUrl();
86 
87  foreach ($this->webhookEventOptionsMap as $webhookEventCode => $webhookEventName) {
88  if ($this->getWebhook($team, $webhookEventName)) {
89  continue;
90  }
91 
92  $this->addWebhook($handlerUrl, $webhookEventCode, $team);
93  }
94  }
95 
102  public function cleanup($team)
103  {
104  foreach ($this->webhookEventOptionsMap as $webhookEventName) {
105  if ($webhook = $this->getWebhook($team, $webhookEventName)) {
106  $this->deleteWebhook($webhook);
107  }
108  }
109  }
110 
118  private function getWebhook($team, $webhookEventName)
119  {
120  $webhook = $this->_rootElement->find(
121  sprintf($this->webhookAddedElement, $team, $webhookEventName),
122  Locator::SELECTOR_XPATH
123  );
124 
125  return $webhook->isPresent() ? $webhook : null;
126  }
127 
134  private function deleteWebhook(ElementInterface $webhook)
135  {
136  $webhook->find($this->webhookDeleteButton)->click();
137  $this->_rootElement->find($this->webhookDeleteConfirmButton)->click();
138  }
139 
148  private function addWebhook(
149  $handlerUrl,
150  $webhookEventCode,
151  $team
152  ) {
153  $this->setEvent($webhookEventCode);
154  $this->setTeam($team);
155  $this->setUrl($handlerUrl);
156  $this->submit();
157  }
158 
165  private function setEvent($webhookEventCode)
166  {
167  $this->_rootElement->find(
168  sprintf($this->webhookEventOption, $webhookEventCode)
169  )->click();
170  }
171 
178  private function setTeam($team)
179  {
180  $this->_rootElement->find(
181  sprintf($this->webhookTeamOption, $team),
182  Locator::SELECTOR_XPATH
183  )->click();
184  }
185 
192  private function setUrl($handlerUrl)
193  {
194  $this->_rootElement->find($this->webhookUrl)->setValue($handlerUrl);
195  }
196 
202  private function submit()
203  {
204  $this->_rootElement->find($this->webhookAddButton)->click();
205  }
206 
212  private function getHandlerUrl()
213  {
214  return $_ENV['app_frontend_url'] . 'signifyd/webhooks/handler';
215  }
216 }