Conditions | 8 |
Paths | 1 |
Total Lines | 66 |
Code Lines | 41 |
Lines | 0 |
Ratio | 0 % |
Tests | 5 |
CRAP Score | 48.8639 |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
20 | 1 | public static function build(): array |
|
21 | { |
||
22 | return [ |
||
23 | 1 | 'name' => 'openDoor', |
|
24 | 1 | 'type' => Type::nonNull(_types()->get(OpenDoorType::class)), |
|
1 ignored issue
–
show
|
|||
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 { |
||
1 ignored issue
–
show
|
|||
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 | }, |
|
90 |