RequestMethods   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Test Coverage

Coverage 37.04%

Importance

Changes 0
Metric Value
eloc 25
dl 0
loc 67
ccs 10
cts 27
cp 0.3704
rs 10
c 0
b 0
f 0
wmc 12

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getMethodName() 0 3 1
A getResult() 0 3 1
A getRequestString() 0 9 2
A export() 0 20 5
A setResult() 0 8 3
1
<?php
2
3
namespace leocata\M1\Abstracts;
4
5
use leocata\M1\Exceptions\MissingMandatoryField;
6
use leocata\M1\Interfaces\MethodDefinitions;
7
8
abstract class RequestMethods implements MethodDefinitions
9
{
10
    private $result;
11
12
    /**
13
     * @return mixed
14
     */
15
    final public function getResult()
16
    {
17
        return $this->result->result;
18
    }
19
20
    final public function setResult($data)
21
    {
22
        if (is_array($data->result)) {
23
            foreach ($data->result as $key => $value) {
24
                $this->result[$key] = $value;
25
            }
26
        } else {
27
            $this->result = $data;
28
        }
29
    }
30
31
    /**
32
     * @return string
33
     */
34
    final public function getRequestString()
35
    {
36
        $request = ['method' => $this->getMethodName()];
37
38
        if (!empty($params = $this->export())) {
39
            $request += ['params' => $params];
40
        }
41
42
        return \GuzzleHttp\json_encode($request);
43
    }
44
45
    /**
46
     * @throws MissingMandatoryField
47
     *
48
     * @return array
49
     */
50 2
    final public function export()
51
    {
52 2
        $finalArray = [];
53 2
        $mandatoryFields = $this->getMandatoryFields();
54
55 2
        $cleanObject = new $this();
56 2
        foreach ($cleanObject as $fieldId => $value) {
57 2
            if ($this->$fieldId === $cleanObject->$fieldId) {
58 2
                if (in_array($fieldId, $mandatoryFields, true)) {
59 2
                    throw new MissingMandatoryField(sprintf(
60 2
                        'The field "%s" is mandatory and empty, please correct',
61 2
                        $fieldId
62
                    ));
63
                }
64
            } else {
65
                $finalArray[$fieldId] = $this->$fieldId;
66
            }
67
        }
68
69
        return empty($finalArray) ? null : $finalArray;
70
    }
71
72
    final public function getMethodName(): string
73
    {
74
        return lcfirst((new \ReflectionClass($this))->getShortName());
75
    }
76
}
77