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\Output\OpenDoorType; |
9
|
|
|
use Application\Model\User; |
10
|
|
|
use Application\Repository\LogRepository; |
11
|
|
|
use Ecodev\Felix\Api\Exception; |
12
|
|
|
use Ecodev\Felix\Api\Field\FieldInterface; |
13
|
|
|
use GraphQL\Type\Definition\Type; |
14
|
|
|
use Laminas\Http\Client; |
15
|
|
|
use Laminas\Http\Request; |
16
|
|
|
use Laminas\Http\Response; |
17
|
|
|
use Mezzio\Session\SessionInterface; |
18
|
|
|
|
19
|
|
|
abstract class OpenDoor implements FieldInterface |
20
|
|
|
{ |
21
|
1 |
|
public static function build(): iterable |
22
|
|
|
{ |
23
|
1 |
|
yield 'openDoor' => fn () => [ |
24
|
1 |
|
'type' => Type::nonNull(_types()->get(OpenDoorType::class)), |
25
|
1 |
|
'description' => 'Open a door at the premises', |
26
|
1 |
|
'args' => [ |
27
|
1 |
|
'door' => Type::nonNull(_types()->get(DoorType::class)), |
28
|
1 |
|
], |
29
|
1 |
|
'resolve' => function ($root, array $args, SessionInterface $session): array { |
30
|
|
|
global $container; |
31
|
|
|
|
32
|
|
|
if (!preg_match('/door([1-4])/', (string) $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("Tu n'as pas le droit d'ouvrir cette porte"); |
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
|
|
|
/** @var Response $response */ |
58
|
|
|
$response = $client->dispatch($request); |
59
|
|
|
} catch (\Laminas\Http\Client\Exception\RuntimeException $e) { |
60
|
|
|
// No answer from the websocket |
61
|
|
|
_log()->err($e->getMessage(), $attrs); |
62
|
|
|
|
63
|
|
|
throw new Exception('Commande de porte temporairement inaccessible, essaie plus tard ou contacte un administrateur'); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
$content = json_decode($response->getContent(), true); |
67
|
|
|
|
68
|
|
|
if ($response->getStatusCode() === 200) { |
69
|
|
|
_log()->info(LogRepository::DOOR_OPENED . $doorIndex, $attrs); |
70
|
|
|
|
71
|
|
|
return $content; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
if (preg_match('/^5[0-9]{2}/', (string) $response->getStatusCode())) { |
75
|
|
|
$errorMsg = "Commande de porte inaccessible en raison d'une erreur serveur, essaie plus tard ou contacte un administrateur"; |
76
|
|
|
} else { |
77
|
|
|
$errorMsg = $content['message'] ?? 'Erreur de commande de porte, essaie plus tard ou contacte un administrateur'; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
// Log body if we have anything |
81
|
|
|
$body = trim($response->getBody()); |
82
|
|
|
if ($body) { |
83
|
|
|
_log()->err($response->getBody(), $attrs); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
throw new Exception($errorMsg); |
87
|
1 |
|
}, |
88
|
1 |
|
]; |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|