1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Application\Api\Field\Mutation; |
6
|
|
|
|
7
|
|
|
use Application\Api\Output\OpenDoorType; |
8
|
|
|
use Application\Enum\Door; |
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(Door::class)), |
28
|
1 |
|
], |
29
|
1 |
|
'resolve' => function ($root, array $args, SessionInterface $session): array { |
30
|
|
|
global $container; |
31
|
|
|
|
32
|
|
|
/** @var Door $door */ |
33
|
|
|
$door = $args['door']; |
34
|
|
|
if (!preg_match('/door([1-4])/', $door->value, $m)) { |
35
|
|
|
throw new Exception("La porte demandée n'existe pas"); |
36
|
|
|
} |
37
|
|
|
$doorIndex = $m[1]; |
38
|
|
|
|
39
|
|
|
$user = User::getCurrent(); |
40
|
|
|
if (!$user || !$user->getCanOpenDoor($door)) { |
41
|
|
|
throw new Exception("Tu n'as pas le droit d'ouvrir cette porte"); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
$apiConfig = $container->get('config')['doorsApi']; |
45
|
|
|
$attrs = [ |
46
|
|
|
'door' => $doorIndex, |
47
|
|
|
'token' => $apiConfig['token'], |
48
|
|
|
]; |
49
|
|
|
|
50
|
|
|
$request = new Request(); |
51
|
|
|
$request->getHeaders()->addHeaders(['Content-Type' => 'application/json']); |
52
|
|
|
$request->setUri($apiConfig['endpoint'] . '/open'); |
53
|
|
|
$request->setMethod(Request::METHOD_POST); |
54
|
|
|
$request->setContent(json_encode($attrs)); |
55
|
|
|
|
56
|
|
|
$client = new Client(); |
57
|
|
|
|
58
|
|
|
try { |
59
|
|
|
/** @var Response $response */ |
60
|
|
|
$response = $client->dispatch($request); |
61
|
|
|
} catch (Client\Exception\RuntimeException $e) { |
62
|
|
|
// No answer from the websocket |
63
|
|
|
_log()->error($e->getMessage(), $attrs); |
64
|
|
|
|
65
|
|
|
throw new Exception('Commande de porte temporairement inaccessible, essaie plus tard ou contacte un administrateur'); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
$content = json_decode($response->getContent(), true); |
69
|
|
|
|
70
|
|
|
if ($response->getStatusCode() === 200) { |
71
|
|
|
_log()->info(LogRepository::DOOR_OPENED . $doorIndex, $attrs); |
72
|
|
|
|
73
|
|
|
return $content; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
if (preg_match('/^5[0-9]{2}/', (string) $response->getStatusCode())) { |
77
|
|
|
$errorMsg = "Commande de porte inaccessible en raison d'une erreur serveur, essaie plus tard ou contacte un administrateur"; |
78
|
|
|
} else { |
79
|
|
|
$errorMsg = $content['message'] ?? 'Erreur de commande de porte, essaie plus tard ou contacte un administrateur'; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
// Log body if we have anything |
83
|
|
|
$body = trim($response->getBody()); |
84
|
|
|
if ($body) { |
85
|
|
|
_log()->error($response->getBody(), $attrs); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
throw new Exception($errorMsg); |
89
|
1 |
|
}, |
90
|
1 |
|
]; |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|