Failed Conditions
Push — master ( 7ddd91...986579 )
by
unknown
06:07
created

OpenDoor   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Test Coverage

Coverage 16.66%

Importance

Changes 0
Metric Value
eloc 34
dl 0
loc 48
ccs 5
cts 30
cp 0.1666
rs 10
c 0
b 0
f 0
wmc 7

1 Method

Rating   Name   Duplication   Size   Complexity  
B build() 0 46 7
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\Model\User;
11
use GraphQL\Type\Definition\Type;
12
use Zend\Expressive\Session\SessionInterface;
13
use Zend\Http\Client;
14
use Zend\Http\Request;
15
16
abstract class OpenDoor implements FieldInterface
17
{
18 1
    public static function build(): array
19
    {
20
        return [
21 1
            'name' => 'openDoor',
22 1
            'type' => Type::nonNull(Type::string()),
23 1
            'description' => 'Open a door at the premises',
24
            'args' => [
25 1
                'door' => Type::nonNull(_types()->get(DoorType::class)),
26
            ],
27
            'resolve' => function ($root, array $args, SessionInterface $session): string {
28
                global $container;
29
30
                if (!preg_match('/door([0-9]+)/', $args['door'], $m)) {
31
                    throw new Exception("La porte demandée n'existe pas");
32
                }
33
                $doorIndex = $m[1];
34
35
                $user = User::getCurrent();
36
                if ($user && $user->isActive()) {
37
                    if (!$user->{'isDoor' . $doorIndex}()) {
38
                        throw new Exception("La porte demandée ne peut être ouverte par l'utilisateur");
39
                    }
40
                    $apiConfig = $container->get('config')['doorsApi'];
41
                    $request = new Request();
42
                    $request->getHeaders()->addHeaders(['Content-Type' => 'application/json']);
43
                    $request->setUri($apiConfig['endpoint'] . '/open');
44
                    $request->setMethod(Request::METHOD_POST);
45
                    $request->setContent(json_encode([
46
                        'door' => $doorIndex,
47
                        'token' => $apiConfig['token'],
48
                    ]));
49
50
                    $client = new Client();
51
52
                    try {
53
                        $response = $client->dispatch($request);
54
                    } catch (\Zend\Http\Client\Exception\RuntimeException $e) {
55
                        throw new Exception('Commande de porte inaccessible: ' . $e->getMessage());
56
                    }
57
                    $content = json_decode($response->getContent());
58
                    if ($response->getStatusCode() === 200) {
59
                        return $content->message;
60
                    }
61
                    $errorMsg = $content->message ?? 'Erreur de commande de porte: ' . $response->getStatusCode() . ' ' . $response->getReasonPhrase();
62
63
                    throw new Exception($errorMsg);
64
                }
0 ignored issues
show
Bug Best Practice introduced by
The function implicitly returns null when the if condition on line 36 is false. This is incompatible with the type-hinted return string. Consider adding a return statement or allowing null as return value.

For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example:

interface ReturnsInt {
    public function returnsIntHinted(): int;
}

class MyClass implements ReturnsInt {
    public function returnsIntHinted(): int
    {
        if (foo()) {
            return 123;
        }
        // here: null is implicitly returned
    }
}
Loading history...
65 1
            },
66
        ];
67
    }
68
}
69