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)), |
|
|
|
|
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 { |
|
|
|
|
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
|
|
|
|