Completed
Pull Request — master (#27)
by Julien
03:13 queued 21s
created

ModelHydrator::convertId()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 1
Metric Value
c 1
b 1
f 1
dl 0
loc 15
rs 9.4285
cc 3
eloc 8
nc 3
nop 2
1
<?php
2
3
namespace Mapado\RestClientSdk\Model;
4
5
use Mapado\RestClientSdk\Collection\HydraCollection;
6
use Mapado\RestClientSdk\Collection\HydraPaginatedCollection;
7
use Mapado\RestClientSdk\SdkClient;
8
9
/**
10
* Class ModelHydrator
11
*
12
* @author Julien Deniau <[email protected]>
13
*/
14
class ModelHydrator
15
{
16
    /**
17
     * sdk
18
     *
19
     * @var SdkClient
20
     * @access private
21
     */
22
    protected $sdk;
23
24
    /**
25
     * __construct
26
     *
27
     * @param RestClient
28
     * @access public
29
     */
30
    public function __construct(SdkClient $sdk)
31
    {
32
        $this->sdk = $sdk;
33
    }
34
35
    /**
36
      * convertId
37
      *
38
      * @param string $id
39
      * @param string $modelName
40
      * @access public
41
      * @return string
42
      */
43
    public function convertId($id, $modelName)
44
    {
45
        // add slash if needed to have a valid hydra id
46
        if (!strstr($id, '/')) {
47
            $mapping = $this->sdk->getMapping();
48
            $key = $mapping->getKeyFromModel($modelName);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $key is correct as $mapping->getKeyFromModel($modelName) (which targets Mapado\RestClientSdk\Mapping::getKeyFromModel()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
49
            $id = $key . '/' . $id;
50
51
            if ($prefix = $mapping->getIdPrefix()) {
52
                $id = $prefix . '/' . $id;
53
            }
54
        }
55
56
        return $id;
57
    }
58
59
    /**
60
     * hydrate
61
     *
62
     * @param array $data
63
     * @param string $modelName
64
     * @access public
65
     * @return object
66
     */
67
    public function hydrate($data, $modelName)
68
    {
69
        $mapping = $this->sdk->getMapping();
70
        $key = $mapping->getKeyFromModel($modelName);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $key is correct as $mapping->getKeyFromModel($modelName) (which targets Mapado\RestClientSdk\Mapping::getKeyFromModel()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
71
        $modelName = $mapping->getModelName($key);
72
73
        return $this->deserialize($data, $modelName);
74
    }
75
76
    /**
77
     * hydrateList
78
     *
79
     * @param array $data
80
     * @param string $modelName
81
     * @access public
82
     * @return array
83
     */
84
    public function hydrateList($data, $modelName)
85
    {
86
        if (is_array($data) && !empty($data['hydra:member'])) {
87
            return $this->deserializeAll($data, $modelName);
88
        }
89
90
        return new HydraCollection();
91
    }
92
93
    public function deserializeAll($data, $modelName)
94
    {
95
        $data['hydra:member'] = array_map(
96
            function ($member) use ($modelName) {
97
                return $this->deserialize($member, $modelName);
98
            },
99
            $data['hydra:member']
100
        );
101
102
        $hydratedList = new HydraCollection($data);
103
104
        if (!empty($data['@type'])) {
105
            if ($data['@type'] === 'hydra:PagedCollection') {
106
                $hydratedList = new HydraPaginatedCollection($data);
107
            }
108
        }
109
110
        return $hydratedList;
111
    }
112
113
    /**
114
     * deserialize
115
     *
116
     * @param array $data
117
     * @param string $modelName
118
     * @access private
119
     * @return object
120
     */
121
    private function deserialize($data, $modelName)
122
    {
123
        if (empty($data)) {
124
            return null;
125
        }
126
127
        if (!is_array($data)) {
128
            return null;
129
        }
130
131
        return $this->sdk->getSerializer()->deserialize($data, $modelName);
132
    }
133
}
134