Failed Conditions
Push — master ( a8fd5c...ef1a3d )
by Adrien
11:53
created

OpenDoor   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Test Coverage

Coverage 13.89%

Importance

Changes 0
Metric Value
eloc 42
c 0
b 0
f 0
dl 0
loc 68
ccs 5
cts 36
cp 0.1389
rs 10
wmc 8

1 Method

Rating   Name   Duplication   Size   Complexity  
B build() 0 66 8
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Application\Api\Field\Mutation;
6
7
use Application\Api\Enum\DoorType;
8
use Application\Api\Exception;
9
use Application\Api\Field\FieldInterface;
10
use Application\Api\Output\OpenDoorType;
11
use Application\Model\User;
12
use Application\Repository\LogRepository;
13
use GraphQL\Type\Definition\Type;
14
use Zend\Expressive\Session\SessionInterface;
15
use Zend\Http\Client;
16
use Zend\Http\Request;
17
18
abstract class OpenDoor implements FieldInterface
19
{
20 1
    public static function build(): array
21
    {
22
        return [
23 1
            'name' => 'openDoor',
24 1
            'type' => Type::nonNull(_types()->get(OpenDoorType::class)),
1 ignored issue
show
Bug introduced by
_types()->get(Applicatio...ut\OpenDoorType::class) of type GraphQL\Type\Definition\Type is incompatible with the type GraphQL\Type\Definition\NullableType expected by parameter $wrappedType of GraphQL\Type\Definition\Type::nonNull(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

24
            'type' => Type::nonNull(/** @scrutinizer ignore-type */ _types()->get(OpenDoorType::class)),
Loading history...
25 1
            'description' => 'Open a door at the premises',
26
            'args' => [
27 1
                'door' => Type::nonNull(_types()->get(DoorType::class)),
28
            ],
29
            'resolve' => function ($root, array $args, SessionInterface $session): array {
1 ignored issue
show
Unused Code introduced by
The parameter $session is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

29
            'resolve' => function ($root, array $args, /** @scrutinizer ignore-unused */ SessionInterface $session): array {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
30
                global $container;
31
32
                if (!preg_match('/door([1-4])/', $args['door'], $m)) {
33
                    throw new Exception("La porte demandée n'existe pas");
34
                }
35
                $doorIndex = $m[1];
36
37
                $user = User::getCurrent();
38
                if (!$user || !$user->getCanOpenDoor($args['door'])) {
39
                    throw new Exception("Vous n'avez pas le droit d'ouvrir la porte, assurez-vous d'être connecté au Wi-Fi du local Ichtus");
40
                }
41
42
                $apiConfig = $container->get('config')['doorsApi'];
43
                $attrs = [
44
                    'door' => $doorIndex,
45
                    'token' => $apiConfig['token'],
46
                ];
47
48
                $request = new Request();
49
                $request->getHeaders()->addHeaders(['Content-Type' => 'application/json']);
50
                $request->setUri($apiConfig['endpoint'] . '/open');
51
                $request->setMethod(Request::METHOD_POST);
52
                $request->setContent(json_encode($attrs));
53
54
                $client = new Client();
55
56
                try {
57
                    $response = $client->dispatch($request);
58
                } catch (\Zend\Http\Client\Exception\RuntimeException $e) {
59
                    // No answer from the websocket
60
                    _log()->err($e->getMessage(), $attrs);
61
62
                    throw new Exception('Commande de porte temporairement inaccessible, veuillez essayez plus tard ou contacter un administrateur');
63
                }
64
65
                $content = json_decode($response->getContent(), true);
66
67
                if ($response->getStatusCode() === 200) {
68
                    _log()->info(LogRepository::DOOR_OPENED . $doorIndex, $attrs);
69
70
                    return $content;
71
                }
72
73
                if (preg_match('/^5[0-9]{2}/', (string) $response->getStatusCode())) {
74
                    $errorMsg = "Commande de porte inaccessible en raison d'une erreur serveur, veuillez essayez plus tard ou contacter un administrateur";
75
                } else {
76
                    $errorMsg = $content['message'] ?? 'Erreur de commande de porte, veuillez essayez plus tard ou contact un administrateur';
77
                }
78
79
                // Log body if we have anything
80
                $body = trim($response->getBody());
81
                if ($body) {
82
                    _log()->err($response->getBody(), $attrs);
83
                }
84
85
                throw new Exception($errorMsg);
86 1
            },
87
        ];
88
    }
89
}
90