SmsController   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 15
dl 0
loc 34
rs 10
c 0
b 0
f 0
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A send() 0 15 1
A createJsonResponseFromSendMessageResult() 0 15 3
1
<?php
2
3
/**
4
 * Copyright 2014 SURFnet bv
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 *     http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 */
18
19
namespace Surfnet\StepupGateway\ApiBundle\Controller;
20
21
use Surfnet\StepupGateway\ApiBundle\Dto\Requester;
22
use Surfnet\StepupGateway\ApiBundle\Dto\SmsMessage;
23
use Surfnet\StepupGateway\ApiBundle\Service\SmsService;
24
use Surfnet\StepupGateway\ApiBundle\Sms\SmsMessageResultInterface;
25
use Surfnet\StepupGateway\GatewayBundle\Container\ContainerController;
26
use Symfony\Component\HttpFoundation\JsonResponse;
27
use Symfony\Component\Routing\Attribute\Route;
28
29
class SmsController extends ContainerController
30
{
31
    #[Route(
32
        path: '/api/send-sms',
33
        methods: ['POST'],
34
        condition: "request.headers.get('Content-Type') == 'application/json' and 
35
        request.headers.get('Accept') matches '/^application\\\\/json($|[;,])/'"
36
    )]
37
    public function send(
38
        SmsMessage $message,
39
        Requester $requester,
40
    ): JsonResponse {
41
        /** @var SmsService $smsService */
42
        $smsService = $this->get('surfnet_gateway_api.service.sms');
43
        $result = $smsService->send($message);
44
45
        return $this->createJsonResponseFromSendMessageResult($result, $requester);
46
    }
47
48
    private function createJsonResponseFromSendMessageResult(SmsMessageResultInterface $result, Requester $requester): JsonResponse
49
    {
50
        if ($result->isSuccess()) {
51
            return new JsonResponse(['status' => 'OK']);
52
        }
53
54
        $errorData = $result->getRawErrors();
55
        $errors = sprintf('%s (%d), SMS requested by identity "%s"', $errorData['description'], $errorData['code'], $requester->identity);
56
57
        if ($result->isMessageInvalid()) {
58
            return new JsonResponse(['errors' => $errors], 400);
59
        }
60
61
        // Invalid access key or server error
62
        return new JsonResponse(['errors' => $errors], 502);
63
    }
64
}
65