generated from simple-as-fuck/php-package-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Symfony.php
44 lines (34 loc) · 1.5 KB
/
Symfony.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
<?php
declare(strict_types=1);
namespace SimpleAsFuck\ApiToolkit\Controller\Webhook;
use SimpleAsFuck\ApiToolkit\Factory\Symfony\ResponseFactory;
use SimpleAsFuck\ApiToolkit\Factory\Symfony\Validator;
use SimpleAsFuck\ApiToolkit\Service\Webhook\ParamsTransformer;
use SimpleAsFuck\ApiToolkit\Service\Webhook\Repository;
use SimpleAsFuck\ApiToolkit\Service\Webhook\WebhookTransformer;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\ConflictHttpException;
final class Symfony
{
public function __construct(
private readonly Repository $repository,
) {
}
public function addListener(Request $request): Response
{
$type = Validator::make($request)->query()->key('type')->string()->notEmpty()->notNull();
$params = Validator::make($request)->json()->object()->class(new ParamsTransformer())->notNull();
$webhook = $this->repository->save($type, $params);
if ($webhook === null) {
throw new ConflictHttpException('Webhook: \''.$params->listeningUrl().'\' with type: \''.$type.'\' already exist');
}
return ResponseFactory::makeJson($webhook, new WebhookTransformer());
}
public function removeListener(Request $request): Response
{
$webhookId = Validator::make($request)->query()->key('webhookId')->string()->notEmpty()->notNull();
$this->repository->delete($webhookId);
return ResponseFactory::makeJson(null);
}
}