GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( c428a4...8cb3bd )
by Jared
12:57
created

FirebaseModel   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 198
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 23
lcom 1
cbo 3
dl 0
loc 198
c 0
b 0
f 0
rs 10

13 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getNodeName() 0 4 2
A setNodeName() 0 4 1
A getMaps() 0 4 1
A setMaps() 0 4 1
A getData() 0 8 1
A create() 0 10 1
A save() 0 14 2
A delete() 0 11 2
A findByKey() 0 12 3
A findAll() 0 17 4
A map() 0 5 1
A mapAttributes() 0 14 3
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: jaredchu
5
 * Date: 1/14/17
6
 * Time: 2:25 PM
7
 */
8
9
namespace JC\Firebase\Models;
10
11
use JC\Firebase\JCFirebase;
12
use JsonMapper;
13
14
/**
15
 * Class FirebaseModel
16
 * @package JCFirebase
17
 */
18
class FirebaseModel
19
{
20
21
    /**
22
     * @var string
23
     */
24
    public static $nodeName = '';
25
26
    /**
27
     * @var array
28
     */
29
    public static $maps = [];
30
31
    /**
32
     * @var string
33
     */
34
    public $key;
35
36
    /**
37
     * @var JCFirebase
38
     */
39
    public $firebase;
40
41
    /**
42
     * FirebaseModel constructor.
43
     *
44
     * @param \JC\Firebase\JCFirebase $firebase
45
     */
46
    public function __construct(JCFirebase $firebase = null)
47
    {
48
        $this->firebase = $firebase;
49
    }
50
51
    /**
52
     * @return string
53
     */
54
    public static function getNodeName()
55
    {
56
        return static::$nodeName ?: strtolower((new \ReflectionClass(get_called_class()))->getShortName());
57
    }
58
59
    /**
60
     * @param $nodeName
61
     */
62
    public static function setNodeName($nodeName)
63
    {
64
        static::$nodeName = $nodeName;
65
    }
66
67
    /**
68
     * @return array
69
     */
70
    public static function getMaps()
71
    {
72
        return static::$maps;
73
    }
74
75
    /**
76
     * @param array $maps
77
     */
78
    public static function setMaps($maps)
79
    {
80
        static::$maps = $maps;
81
    }
82
83
    /**
84
     * @return array
85
     */
86
    public function getData()
87
    {
88
        $object = clone $this;
89
        unset($object->firebase);
90
        unset($object->key);
91
92
        return static::mapAttributes(get_object_vars($object));
93
    }
94
95
    /**
96
     * @return bool
97
     */
98
    public function create()
99
    {
100
        $response = $this->firebase->post(static::getNodeName(), array(
101
            'data' => $this->getData()
102
        ));
103
104
        $this->key = json_decode($response->body())->name;
105
106
        return $response->success();
107
    }
108
109
110
    /**
111
     * @return bool
112
     */
113
    public function save()
114
    {
115
        if (!empty($this->key)) {
116
            $response = $this->firebase->put(static::getNodeName() . '/' . $this->key, array(
117
                'data' => $this->getData()
118
            ));
119
120
            $success = $response->success();
121
        } else {
122
            $success = $this->create();
123
        }
124
125
        return $success;
126
    }
127
128
    /**
129
     * @return bool
130
     */
131
    public function delete()
132
    {
133
        $success = false;
134
        if (!empty($this->key)) {
135
            $response = $this->firebase->delete(static::getNodeName() . '/' . $this->key);
136
137
            $success = $response->success();
138
        }
139
140
        return $success;
141
    }
142
143
    /**
144
     * @param $key
145
     * @param JCFirebase $firebase
146
     *
147
     * @return object
148
     */
149
    public static function findByKey($key, JCFirebase $firebase)
150
    {
151
        $response = $firebase->get(static::getNodeName() . '/' . $key);
152
        $object = null;
153
        if ($response->success() && $response->body() != 'null') {
154
            $object = static::map($response->json(), new static());
155
            $object->key = $key;
156
            $object->firebase = $firebase;
157
        }
158
159
        return $object;
160
    }
161
162
    /**
163
     * @param JCFirebase $firebase
164
     *
165
     * @return array(FirebaseModel)
0 ignored issues
show
Documentation introduced by
The doc-type array(FirebaseModel) could not be parsed: Expected "|" or "end of type", but got "(" at position 5. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
166
     */
167
    public static function findAll(JCFirebase $firebase)
168
    {
169
        $response = $firebase->get(static::getNodeName());
170
        $objects = array();
171
172
        $jsonObject = json_decode($response->body(), true);
173
        if ($response->success() && count($jsonObject)) {
174
            do {
175
                $object = static::map((object)current($jsonObject), new static());
176
                $object->key = key($jsonObject);
177
                $object->firebase = $firebase;
178
                $objects[] = $object;
179
            } while (next($jsonObject));
180
        }
181
182
        return $objects;
183
    }
184
185
    /**
186
     * @param $object
187
     * @param $instance
188
     * @return object
189
     */
190
    protected static function map($object, $instance)
191
    {
192
        $mapper = new JsonMapper();
193
        return $mapper->map((object)static::mapAttributes(get_object_vars($object), false), $instance);
194
    }
195
196
    /**
197
     * @param array $objectVars
198
     * @param bool $fromLocal
199
     * @return array
200
     */
201
    protected static function mapAttributes(array $objectVars, $fromLocal = true)
202
    {
203
        foreach (static::getMaps() as $localAttr => $DBAttr) {
204
            if ($fromLocal) {
205
                $objectVars[$DBAttr] = $objectVars[$localAttr];
206
                unset($objectVars[$localAttr]);
207
            } else {
208
                $objectVars[$localAttr] = $objectVars[$DBAttr];
209
                unset($objectVars[$DBAttr]);
210
            }
211
        }
212
213
        return $objectVars;
214
    }
215
}