Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
20 / 20 |
|
100.00% |
7 / 7 |
CRAP | |
100.00% |
1 / 1 |
| ResponseTemplateManager | |
100.00% |
20 / 20 |
|
100.00% |
7 / 7 |
12 | |
100.00% |
1 / 1 |
| generateTemplate | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| addTemplate | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
| getTemplate | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
| getTemplates | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
| hasTemplate | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| isTemplateMatchHash | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
| isTemplateMatchPlain | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | #declare(strict_types=1); |
| 4 | |
| 5 | /** |
| 6 | * CNIC\HEXONET |
| 7 | * Copyright © CentralNic Group PLC |
| 8 | */ |
| 9 | |
| 10 | namespace CNIC\HEXONET; |
| 11 | |
| 12 | use CNIC\HEXONET\ResponseParser as RP; |
| 13 | |
| 14 | /** |
| 15 | * HEXONET ResponseTemplateManager |
| 16 | * |
| 17 | * @package CNIC\HEXONET |
| 18 | */ |
| 19 | |
| 20 | final class ResponseTemplateManager |
| 21 | { |
| 22 | /** |
| 23 | * template container |
| 24 | * @var array |
| 25 | */ |
| 26 | public static $templates = [ |
| 27 | "404" => "[RESPONSE]\r\nCODE=421\r\nDESCRIPTION=Page not found\r\nEOF\r\n", |
| 28 | "500" => "[RESPONSE]\r\nCODE=500\r\nDESCRIPTION=Internal server error\r\nEOF\r\n", |
| 29 | "empty" => "[RESPONSE]\r\nCODE=423\r\nDESCRIPTION=Empty API response. Probably unreachable API end point {CONNECTION_URL}\r\nEOF\r\n", |
| 30 | "error" => "[RESPONSE]\r\nCODE=421\r\nDESCRIPTION=Command failed due to server error. Client should try again\r\nEOF\r\n", |
| 31 | "expired" => "[RESPONSE]\r\nCODE=530\r\nDESCRIPTION=SESSION NOT FOUND\r\nEOF\r\n", |
| 32 | "httperror" => "[RESPONSE]\r\nCODE=421\r\nDESCRIPTION=Command failed due to HTTP communication error\r\nEOF\r\n", |
| 33 | "invalid" => "[RESPONSE]\r\nCODE=423\r\nDESCRIPTION=Invalid API response. Contact Support\r\nEOF\r\n", |
| 34 | "nocurl" => "[RESPONSE]\r\nCODE=423\r\nDESCRIPTION=API access error: curl_init failed\r\nEOF\r\n", |
| 35 | "notfound" => "[RESPONSE]\r\nCODE=500\r\nDESCRIPTION=Response Template not found\r\nEOF\r\n", |
| 36 | "unauthorized" => "[RESPONSE]\r\nCODE=530\r\nDESCRIPTION=Unauthorized\r\nEOF\r\n" |
| 37 | ]; |
| 38 | |
| 39 | /** |
| 40 | * Generate API response template string for given code and description |
| 41 | * @param string $code API response code |
| 42 | * @param string $description API response description |
| 43 | * @return string generate response template string |
| 44 | */ |
| 45 | public static function generateTemplate($code, $description) |
| 46 | { |
| 47 | return "[RESPONSE]\r\nCODE=" . $code . "\r\nDESCRIPTION=" . $description . "\r\nEOF\r\n"; |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Add response template to template container |
| 52 | * @param string $id template id |
| 53 | * @param string $plain API plain response or API response code (when providing $descr) |
| 54 | * @param string|null $descr API response description |
| 55 | * @return self |
| 56 | */ |
| 57 | public static function addTemplate($id, $plain, $descr = null) |
| 58 | { |
| 59 | if (is_null($descr)) { |
| 60 | self::$templates[$id] = $plain; |
| 61 | } else { |
| 62 | self::$templates[$id] = self::generateTemplate($plain, $descr); |
| 63 | } |
| 64 | return new self(); |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Get response template instance from template container |
| 69 | * @param string $id template id |
| 70 | * @return Response template instance |
| 71 | */ |
| 72 | public static function getTemplate($id) |
| 73 | { |
| 74 | if (self::hasTemplate($id)) { |
| 75 | return new Response($id); |
| 76 | } |
| 77 | return new Response("notfound"); |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Return all available response templates |
| 82 | * @return array all available response instances |
| 83 | */ |
| 84 | public static function getTemplates() |
| 85 | { |
| 86 | $tpls = []; |
| 87 | foreach (self::$templates as $key => $raw) { |
| 88 | $tpls[$key] = new Response($raw); |
| 89 | } |
| 90 | return $tpls; |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Check if given template exists in template container |
| 95 | * @param string $id template id |
| 96 | * @return bool boolean result |
| 97 | */ |
| 98 | public static function hasTemplate($id) |
| 99 | { |
| 100 | return array_key_exists($id, self::$templates); |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Check if given API response hash matches a given template by code and description |
| 105 | * @param array $tpl api response hash |
| 106 | * @param string $id template id |
| 107 | * @return bool boolean result |
| 108 | */ |
| 109 | public static function isTemplateMatchHash($tpl, $id) |
| 110 | { |
| 111 | $h = self::getTemplate($id)->getHash(); |
| 112 | return ( |
| 113 | ($h["CODE"] === $tpl["CODE"]) && |
| 114 | ($h["DESCRIPTION"] === $tpl["DESCRIPTION"]) |
| 115 | ); |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Check if given API plain response matches a given template by code and description |
| 120 | * @param string $plain API plain response |
| 121 | * @param string $id template id |
| 122 | * @return bool boolean result |
| 123 | */ |
| 124 | public static function isTemplateMatchPlain($plain, $id) |
| 125 | { |
| 126 | $h = self::getTemplate($id)->getHash(); |
| 127 | $tpl = RP::parse($plain); |
| 128 | return ( |
| 129 | ($h["CODE"] === $tpl["CODE"]) && |
| 130 | ($h["DESCRIPTION"] === $tpl["DESCRIPTION"]) |
| 131 | ); |
| 132 | } |
| 133 | } |