-
Notifications
You must be signed in to change notification settings - Fork 110
/
AbstractWebhook.php
79 lines (65 loc) · 2.22 KB
/
AbstractWebhook.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
<?php
namespace Playbloom\Satisfy\Webhook;
use Playbloom\Satisfy\Event\BuildEvent;
use Playbloom\Satisfy\Model\RepositoryInterface;
use Playbloom\Satisfy\Service\Manager;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
use Symfony\Component\HttpKernel\Exception\ServiceUnavailableHttpException;
abstract class AbstractWebhook
{
/** @var EventDispatcherInterface */
protected $dispatcher;
/** @var Manager */
protected $manager;
/** @var string|null */
protected $secret;
public function __construct(Manager $manager, EventDispatcherInterface $dispatcher)
{
$this->manager = $manager;
$this->dispatcher = $dispatcher;
}
/**
* @throws BadRequestHttpException
* @throws ServiceUnavailableHttpException
*/
public function getResponse(Request $request): Response
{
try {
$this->validate($request);
$repository = $this->getRepository($request);
$status = $this->handle($repository);
} catch (\InvalidArgumentException $exception) {
throw new BadRequestHttpException($exception->getMessage(), $exception);
} catch (\Throwable $exception) {
throw new ServiceUnavailableHttpException();
}
return new Response($status);
}
public function handle(RepositoryInterface $repository): ?int
{
$event = new BuildEvent($repository);
$this->dispatcher->dispatch(BuildEvent::EVENT_NAME, $event);
return $event->getStatus();
}
/**
* @throws \InvalidArgumentException
*/
abstract protected function validate(Request $request): void;
/**
* @throws \InvalidArgumentException
*/
abstract protected function getRepository(Request $request): RepositoryInterface;
protected function findRepository(array $urls): ?RepositoryInterface
{
foreach ($urls as $url) {
$repository = $this->manager->findByUrl($url);
if ($repository) {
return $repository;
}
}
return null;
}
}