Created
May 6, 2020 21:53
-
-
Save nasrulhazim/91855bf452587bf40bfab89c7c7de4c2 to your computer and use it in GitHub Desktop.
GitHub Payload
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\WebhookClient\Payload; | |
use App\Contracts\WebhookPayload; | |
use Illuminate\Support\Str; | |
class GitHub implements \App\Contracts\WebhookPayload | |
{ | |
protected $header; | |
protected $payload; | |
public function setHeader($header): WebhookPayload | |
{ | |
$this->header = collect($header); | |
return $this; | |
} | |
public function getHeader() | |
{ | |
return $this->header; | |
} | |
public function setPayload($payload): WebhookPayload | |
{ | |
$this->payload = collect($payload); | |
return $this; | |
} | |
public function getPayload() | |
{ | |
return $this->payload; | |
} | |
public function getEvent(): string | |
{ | |
return $this->getHeader()->first( | |
function ($value, $key) { | |
return 'x-github-event' == $key; | |
} | |
)[0]; | |
} | |
public function getAction(): string | |
{ | |
return $this->getPayloadObject('action')[0]; | |
} | |
public function getSender(): string | |
{ | |
return $this->getPayloadObject('sender'); | |
} | |
public function getPayloadObject($object_key) | |
{ | |
return collect($this->getPayload()->first( | |
function ($value, $key) use ($object_key) { | |
return $key == $object_key; | |
} | |
)); | |
} | |
public function getEventObject() | |
{ | |
return $this->getPayloadObject($this->getEvent()); | |
} | |
public function getWebhookEventName(): string | |
{ | |
return $this->getEvent(); | |
} | |
public function getWebhookContent() | |
{ | |
return collect($this->payload->first(function ($value, $key) { | |
return $this->getEvent() == $key; | |
})); | |
} | |
public function getType(): string | |
{ | |
return Str::of($this->getWebhookEventName())->title(); | |
} | |
public function getTitle(): string | |
{ | |
return Str::of($this->getAction())->title() | |
. ' ' | |
. Str::of(str_replace('_', ' ', $this->getEvent()))->title() | |
. ' ' | |
. optional($this->getEventObject())['title']; | |
} | |
public function getUser() | |
{ | |
return $this->getEventObject()->first( | |
function ($value, $key) { | |
return 'user' == $key; | |
} | |
); | |
} | |
public function getText(): string | |
{ | |
$content = $this->getWebhookContent(); | |
return $this->getTitle() | |
. ' by ' | |
. '<a href="' . $this->getUser()['html_url'] . '" target="_blank">' . $this->getUser()['login'] . '</a>'; | |
} | |
public function getColor(): string | |
{ | |
return collect([ | |
'00e600', | |
])->random(); | |
} | |
public function getActionName(): string | |
{ | |
return __('View ' . str_replace('_', ' ', $this->getType())); | |
} | |
public function getActionUrl(): string | |
{ | |
return optional($this->getEventObject())['html_url'] ?? ''; | |
} | |
public function isSkip(): bool | |
{ | |
return 'push' == $this->getWebhookEventName(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment