|
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
|
|
|
} |
|
|
|
|
|
|
65
|
1 |
|
}, |
|
66
|
|
|
]; |
|
67
|
|
|
} |
|
68
|
|
|
} |
|
69
|
|
|
|
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: