RequestAbstract   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 102
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 55
dl 0
loc 102
rs 10
c 0
b 0
f 0
wmc 14

6 Methods

Rating   Name   Duplication   Size   Complexity  
A formOutputPath() 0 3 1
A formClassName() 0 3 1
A arrayAsPhpVar() 0 13 4
A formClassNamespace() 0 3 1
A formExtends() 0 3 1
B methods() 0 50 6
1
<?php
2
3
namespace carono\etxtru\codegen;
4
5
use carono\codegen\ClassGenerator;
6
7
class RequestAbstract extends ClassGenerator
8
{
9
    /**
10
     * @return string
11
     */
12
    protected function formExtends()
13
    {
14
        return 'carono\etxtru\RequestAbstract';
15
    }
16
17
    /**
18
     * @return string
19
     */
20
    protected function formClassName()
21
    {
22
        return ucfirst(formClassName($this->params['name'])) . 'Request';
23
    }
24
25
    /**
26
     * @return string
27
     */
28
    protected function formClassNamespace()
29
    {
30
        return 'carono\etxtru\request';
31
    }
32
33
    /**
34
     * @return string
35
     */
36
    protected function formOutputPath()
37
    {
38
        return dirname(__DIR__) . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . 'request' . DIRECTORY_SEPARATOR . $this->formClassName() . '.php';
39
    }
40
41
    /**
42
     * @return bool
43
     */
44
    public function methods()
45
    {
46
47
        foreach ($this->params['methods'] as $methodData) {
48
49
            $methodName = formMethodName($methodData['name']);
50
            $method = $this->phpClass->addMethod($methodName);
51
            $method->addComment(stripAndWordWrap($methodData['description']));
52
            $params = [];
53
            if (count($methodData['params']) > 4) {
54
                $config = new ConfigAbstract();
55
                $config->renderToFile(array_merge(['api' => $this->params['name']], $methodData));
56
                $className = '\\' . $config->formClassNamespace() . '\\' . $config->formClassName();
57
                $method->addParameter('config');
58
                $method->addComment("@param $className|array \$config");
59
                $body = <<<PHP
60
foreach (\$config instanceof \carono\\etxtru\ConfigAbstract ? \$config->toArray() : \$config as \$key => \$value) {
61
    \$params[\$key] = \$value;
62
}
63
PHP;
64
                $paramsStr = self::arrayAsPhpVar($params);
65
                $method->addBody("\$params = $paramsStr;");
66
                $method->addBody($body);
67
            } else {
68
                foreach ($methodData['params'] as $param) {
69
                    $type = formParamType($param['type']);
70
                    $description = trim(stripAndWordWrap($param['description']));
71
                    $method->addComment("@param {$type} \${$param['name']} {$description}");
72
                    $params[$param['name']] = '$' . $param['name'];
73
                    if ($param['required']) {
74
                        $method->addParameter($param['name']);
75
                    } else {
76
                        $method->addParameter($param['name'], null);
77
                    }
78
                }
79
                $paramsStr = self::arrayAsPhpVar($params);
80
                $method->addBody("\$params = $paramsStr;");
81
            }
82
            $response = new ResponseAbstract();
83
            if (isset($methodData['returns'][0])) {
84
                $response->renderToFile(['name' => $methodName, 'returns' => $methodData['returns']]);
85
                $className = $response->formClassNamespace() . '\\' . $response->formClassName();
86
            } else {
87
                $className = 'carono\etxtru\Response';
88
            }
89
            $method->addComment("@return \\$className|\stdClass");
90
            $api = $this->params['name'] . '.' . $methodData['name'];
91
            $method->addBody("return \$this->getClient()->getContent('$api', \$params, '$className');");
92
        }
93
        return false;
94
    }
95
96
    protected static function arrayAsPhpVar($array)
97
    {
98
        $export = [];
99
        foreach ($array as $key => $value) {
100
            $export[] = "'$key' => " . (strpos($value, '$') === false ? "'$value'" : $value);
101
        }
102
        $export = implode(",\n\t", $export);
103
        if ($array) {
104
            $result = "[\n\t$export\n]";
105
        } else {
106
            $result = '[]';
107
        }
108
        return $result;
109
    }
110
}