|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <fabien@symfony.com> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\Component\Notifier\Bridge\Smsbox\Webhook; |
| 13 | + |
| 14 | +use Symfony\Component\HttpFoundation\Request; |
| 15 | +use Symfony\Component\HttpFoundation\RequestMatcher\MethodRequestMatcher; |
| 16 | +use Symfony\Component\HttpFoundation\RequestMatcherInterface; |
| 17 | +use Symfony\Component\HttpFoundation\ChainRequestMatcher; |
| 18 | +use Symfony\Component\RemoteEvent\Event\Sms\SmsEvent; |
| 19 | +use Symfony\Component\Webhook\Client\AbstractRequestParser; |
| 20 | +use Symfony\Component\Webhook\Exception\RejectWebhookException; |
| 21 | + |
| 22 | + |
| 23 | +final class SmsboxRequestParser extends AbstractRequestParser |
| 24 | +{ |
| 25 | + protected function getRequestMatcher(): RequestMatcherInterface |
| 26 | + { |
| 27 | + return new MethodRequestMatcher(['GET']); |
| 28 | + } |
| 29 | + |
| 30 | + protected function doParse(Request $request, #[\SensitiveParameter] string $secret): ?SmsEvent |
| 31 | + { |
| 32 | + $payload = $request->query->all(); |
| 33 | + |
| 34 | + if ( |
| 35 | + !isset($payload['numero']) |
| 36 | + || !isset($payload['reference']) |
| 37 | + || !isset($payload['accuse']) |
| 38 | + || !isset($payload['ts']) |
| 39 | + ) { |
| 40 | + throw new RejectWebhookException(406, 'Payload is malformed.'); |
| 41 | + } |
| 42 | + |
| 43 | + $name = match ($payload['accuse']) { |
| 44 | + // Documentation for SMSBOX dlr code https://www.smsbox.net/en/tools-development#doc-sms-accusees |
| 45 | + '-3' => SmsEvent::FAILED, |
| 46 | + '-1' => null, |
| 47 | + '0' => SmsEvent::DELIVERED, |
| 48 | + '1' => SmsEvent::FAILED, |
| 49 | + '2' => SmsEvent::FAILED, |
| 50 | + '3' => SmsEvent::FAILED, |
| 51 | + '4' => SmsEvent::FAILED, |
| 52 | + '5' => SmsEvent::FAILED, |
| 53 | + '6' => SmsEvent::FAILED, |
| 54 | + '7' => SmsEvent::FAILED, |
| 55 | + '8' => SmsEvent::FAILED, |
| 56 | + '9' => null, |
| 57 | + '10' => null, |
| 58 | + default => throw new RejectWebhookException(406, \sprintf('Unknown status: %s', $payload['accuse'])), |
| 59 | + }; |
| 60 | + |
| 61 | + $event = new SmsEvent($name, $payload['reference'], $payload); |
| 62 | + $event->setRecipientPhone($payload['numero']); |
| 63 | + |
| 64 | + return $event; |
| 65 | + } |
| 66 | +} |
0 commit comments