JsonHandler::objectToArray()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
nc 4
nop 1
dl 0
loc 22
rs 9.568
c 0
b 0
f 0
ccs 13
cts 13
cp 1
crap 4
1
<?php
2
namespace agoalofalife\bpm\Handlers;
3
4
5
use agoalofalife\bpm\Contracts\Collection;
6
use agoalofalife\bpm\Contracts\Handler;
7
8
/**
9
 * @property string buildJson
10
 */
11
class JsonHandler implements Handler, Collection
12
{
13
    use XmlConverter;
14
15
    protected $response;
16
    protected $buildJson;
17
18
    private $jsonPrefix     = 'd';
19
    private $jsonPrefixWord = 'results';
20
    private $validText      = [];
21
    private $jsonPrefixAllCollection = 'EntitySets';
22
23 1
    public function getAccept()
24
    {
25 1
        return 'application/json;odata=verbose;';
26
    }
27
28 1
    public function getContentType()
29
    {
30 1
        return 'application/json;odata=verbose;';
31
    }
32
33 6
    public function parse($parse)
34
    {
35 6
        if ($this->checkIntegrity($parse) === false)
36 6
        {
37 1
            return [];
38
        }
39
40 5
        $this->response = $parse;
41 5
        $this->validText = $this->chooseJsonPrefix($parse);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->chooseJsonPrefix($parse) of type * is incompatible with the declared type array of property $validText.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
42 5
        return $this;
43
    }
44
45
    /**
46
     * @param $parse string json
47
     * @return mixed
48
     */
49 5
    private function chooseJsonPrefix($parse)
50
    {
51 5
        $decode = json_decode($parse);
52 5
        if ( isset($decode->{$this->jsonPrefix}->{$this->jsonPrefixWord}) )
53 5
        {
54 4
            return $decode->{$this->jsonPrefix}->{$this->jsonPrefixWord};
55
        } else {
56 1
            return $decode->{$this->jsonPrefix}->{$this->jsonPrefixAllCollection};
57
        }
58
    }
59
60
    /**
61
     * @param $response
62
     * @return bool
63
     */
64 8
    public function checkIntegrity($response)
65
    {
66 8
       return isset( json_decode($response)->{$this->jsonPrefix} );
67
    }
68
69
    /**
70
     * @return array
71
     */
72 1
    public function toArray()
73
    {
74 1
        return $this->objectToArray($this->validText);
75
    }
76
77
    /**
78
     * @return string json
79
     */
80 1
    public function toJson()
81
    {
82 1
        return json_encode($this->validText);
83
    }
84
85 1
    public function getData()
86
    {
87 1
        return $this->validText;
88
    }
89
90 1
    public function create(array $data)
91
    {
92 1
        if ( empty($data)){
93 1
            return $this->buildJson = '{}';
94
        }
95 1
        return $this->buildJson = json_encode($data);
96
    }
97
98 1
    private function objectToArray($data)
99
    {
100 1
        $result = array();
101
102 1
        foreach ($data as $key => $value) {
103 1
            if (gettype($value) == 'object')
104 1
            {
105 1
                $result[$key] = $this->objectToArray($value);
106 1
            } else{
107 1
               if (gettype($value) != 'object')
108 1
               {
109 1
                   $result[$key] = $value;
110 1
               }
111
               // TODO not smog to find a similar case
112
             /*  else{
0 ignored issues
show
Unused Code Comprehensibility introduced by
62% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
113
                   $result[$key] = get_object_vars($value);
114
               }*/
115
116
            }
117 1
        }
118 1
        return $result;
119
    }
120
    
121
}