Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
22 / 22
100.00% covered (success)
100.00%
1 / 1
CRAP
100.00% covered (success)
100.00%
1 / 1
ResponseTranslator
100.00% covered (success)
100.00%
22 / 22
100.00% covered (success)
100.00%
1 / 1
13
100.00% covered (success)
100.00%
1 / 1
 translate
100.00% covered (success)
100.00%
22 / 22
100.00% covered (success)
100.00%
1 / 1
13
1<?php
2
3#declare(strict_types=1);
4
5/**
6 * CNIC\HEXONET
7 * Copyright © CentralNic Group PLC
8 */
9
10namespace CNIC\HEXONET;
11
12use CNIC\HEXONET\ResponseTemplateManager as RTM;
13
14/**
15 * HEXONET ResponseTranslator
16 *
17 * @package CNIC\HEXONET
18 */
19class ResponseTranslator
20{
21    /**
22     * hidden class var of API description regex mappings for translation
23     * @var array
24     */
25    private static $descriptionRegexMap = [
26        "Authorization failed; Operation forbidden by ACL" => "Authorization failed; Used Command `{COMMAND}` not white-listed by your Access Control List",
27        "Request is not available; DOMAIN TRANSFER IS PROHIBITED BY STATUS (clientTransferProhibited)/WRONG AUTH" => "This Domain is locked and the given Authorization Code is wrong. Initiating a Transfer is therefore impossible.",
28        "Request is not available; DOMAIN TRANSFER IS PROHIBITED BY STATUS (clientTransferProhibited)" => "This Domain is locked. Initiating a Transfer is therefore impossible.",
29        "Request is not available; DOMAIN TRANSFER IS PROHIBITED BY STATUS (requested)" => "Registration of this Domain Name has not yet completed. Initiating a Transfer is therefore impossible.",
30        "Request is not available; DOMAIN TRANSFER IS PROHIBITED BY STATUS (requestedcreate)" => "Registration of this Domain Name has not yet completed. Initiating a Transfer is therefore impossible.",
31        "Request is not available; DOMAIN TRANSFER IS PROHIBITED BY STATUS (requesteddelete)" => "Deletion of this Domain Name has been requested. Initiating a Transfer is therefore impossible.",
32        "Request is not available; DOMAIN TRANSFER IS PROHIBITED BY STATUS (pendingdelete)" => "Deletion of this Domain Name is pending. Initiating a Transfer is therefore impossible.",
33        "Request is not available; DOMAIN TRANSFER IS PROHIBITED BY WRONG AUTH" => "The given Authorization Code is wrong. Initiating a Transfer is therefore impossible.",
34        "Request is not available; DOMAIN TRANSFER IS PROHIBITED BY AGE OF THE DOMAIN" => "This Domain Name is within 60 days of initial registration. Initiating a Transfer is therefore impossible."
35    ];
36
37    /**
38     * translate a raw api response
39     * @param String $raw API raw response
40     * @param Array $cmd requested API command
41     * @param Array $ph list of place holder vars
42     * @return String
43     */
44    public static function translate($raw, $cmd, $ph = [])
45    {
46        $newraw = empty($raw) ? "empty" : $raw;
47        // Hint: Empty API Response (replace {CONNECTION_URL} later)
48
49        // Explicit call for a static template
50        if (RTM::hasTemplate($newraw)) {
51            // don't use getTemplate as it leads to endless loop as of again
52            // creating a response instance
53            $newraw = RTM::$templates[$newraw];
54        }
55
56        // Missing CODE or DESCRIPTION in API Response
57        if (
58            (!preg_match("/description[\s]*=/i", $newraw) // missing description
59                || preg_match("/description[\s]*=\r\n/i", $newraw) // empty description
60                || !preg_match("/code[\s]*=/i", $newraw) // missing code
61            )
62            && RTM::hasTemplate("invalid")
63        ) {
64            $newraw = RTM::$templates["invalid"];
65        }
66
67        // generic API response description rewrite
68        foreach (self::$descriptionRegexMap as $regex => $val) {
69            // match the response for given description
70            // NOTE: we match if the description starts with the given description
71            // it would also match if it is followed by additional text
72            $qregex = "/description=" . preg_quote($regex, "/") . "([^\r\n]+)?/i";
73            if (preg_match($qregex, $newraw)) {
74                // replace command place holder with API command name used
75                if (isset($cmd["COMMAND"])) {
76                    $val = str_replace("{COMMAND}", $cmd["COMMAND"], $val);
77                }
78                // switch to better readable response if matching
79                $tmp = preg_replace($qregex, "description=" . $val, $newraw);
80                if (strcmp($tmp, $newraw) !== 0) {
81                    $newraw = $tmp;
82                    break;
83                }
84            }
85        }
86
87        // generic replacing of place holder vars
88        if (preg_match("/\{[^}]+\}/", $newraw)) {
89            foreach ($ph as $key => $val) {
90                $newraw = preg_replace("/\{" . preg_quote($key) . "\}/", $val, $newraw);
91            }
92            $newraw = preg_replace("/\{[^}]+\}/", "", $newraw);
93        }
94        return $newraw;
95    }
96}