generated from simple-as-fuck/php-package-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AddListener.php
37 lines (30 loc) · 1.34 KB
/
AddListener.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
<?php
declare(strict_types=1);
namespace SimpleAsFuck\ApiToolkit\Controller\Webhook;
use Kayex\HttpCodes;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
use SimpleAsFuck\ApiToolkit\Factory\Server\ResponseFactory;
use SimpleAsFuck\ApiToolkit\Factory\Server\Validator;
use SimpleAsFuck\ApiToolkit\Model\Server\ApiException;
use SimpleAsFuck\ApiToolkit\Service\Webhook\ParamsTransformer;
use SimpleAsFuck\ApiToolkit\Service\Webhook\Repository;
use SimpleAsFuck\ApiToolkit\Service\Webhook\WebhookTransformer;
final class AddListener implements RequestHandlerInterface
{
public function __construct(
private readonly Repository $repository,
) {
}
public function handle(ServerRequestInterface $request): ResponseInterface
{
$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 ApiException('Webhook: \''.$params->listeningUrl().'\' with type: \''.$type.'\' already exist', HttpCodes::HTTP_CONFLICT);
}
return ResponseFactory::makeJson($webhook, new WebhookTransformer());
}
}