Passed
Push — master ( b917eb...fefbb9 )
by Aleksandr
02:16
created

RequestAbstract   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 105
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 15
c 0
b 0
f 0
dl 0
loc 105
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A formClassNamespace() 0 3 1
A formOutputPath() 0 3 1
A formExtends() 0 3 1
A arrayAsPhpVar() 0 13 4
B methods() 0 53 7
A formClassName() 0 3 1
1
<?php
2
3
namespace carono\turbotext\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\turbotext\RequestAbstract';
15
    }
16
17
    /**
18
     * @return string
19
     */
20
    protected function formClassName()
21
    {
22
        return ucfirst($this->params['name']) . 'Request';
23
    }
24
25
    /**
26
     * @return string
27
     */
28
    protected function formClassNamespace()
29
    {
30
        return 'carono\turbotext\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
        foreach ($this->params['methods'] as $methodData) {
47
            $methodName = formMethodName($methodData['name']);
48
            $method = $this->phpClass->addMethod($methodName);
49
            $method->addComment(trim($methodData['description']));
50
            $params = [];
51
            $params['action'] = $methodData['name'];
52
            if (count($methodData['params']) > 4) {
53
                $config = new ConfigAbstract();
54
                $config->renderToFile($methodData);
55
                $className = '\\' . $config->formClassNamespace() . '\\' . $config->formClassName();
56
                $method->addParameter('config');
57
                $method->addComment("@param $className|array \$config");
58
59
                $body = <<<PHP
60
foreach ((\$config instanceof \carono\\turbotext\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
                    $method->addComment("@param {$type} \${$param['name']} {$param['description']}");
71
                    $params[$param['name']] = '$' . $param['name'];
72
                    if ($param['required']) {
73
                        $method->addParameter($param['name']);
74
                    } else {
75
                        $method->addParameter($param['name'], null);
76
                    }
77
                }
78
                $paramsStr = self::arrayAsPhpVar($params);
79
                $method->addBody("\$params = $paramsStr;");
80
            }
81
            $response = new ResponseAbstract();
82
            if (isset($methodData['returns'][0]['result'])) {
83
                $response->renderToFile($methodData['returns'][0]);
84
                $className = $response->formClassNamespace() . '\\' . $response->formClassName();
85
            } elseif ($methodData['returns']) {
86
                $methodData['returns']['name'] = $methodData['name'];
87
                $response->properties['asd'] = 213;
88
                $response->renderToFile($methodData['returns']);
89
                $className = $response->formClassNamespace() . '\\' . $response->formClassName();
90
            } else {
91
                $className = 'carono\turbotext\Response';
92
            }
93
            $method->addComment("@return \\$className|string|\stdClass|\SimpleXMLElement");
94
            $method->addBody("return \$this->getClient()->getContent('api', \$params, '$className');");
95
        }
96
        return false;
97
    }
98
99
    protected static function arrayAsPhpVar($array)
100
    {
101
        $export = [];
102
        foreach ($array as $key => $value) {
103
            $export[] = "'$key' => " . (strpos($value, '$') === false ? "'$value'" : $value);
104
        }
105
        $export = join(",\n\t", $export);
106
        if ($array) {
107
            $result = "[\n\t$export\n]";
108
        } else {
109
            $result = "[]";
110
        }
111
        return $result;
112
    }
113
}