Completed
Push — master ( 2994aa...19d116 )
by Patrick
08:28
created

ModelFactory   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 161
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Test Coverage

Coverage 92.86%

Importance

Changes 0
Metric Value
wmc 23
lcom 1
cbo 9
dl 0
loc 161
ccs 39
cts 42
cp 0.9286
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
C make() 0 60 12
A isFileOrFolder() 0 4 1
A isFile() 0 4 1
A isFolder() 0 4 1
A isTemporaryLink() 0 4 2
A isList() 0 4 1
A isSearchResult() 0 4 1
A isFileRequestList() 0 4 1
A isFileRequest() 0 4 2
A isDeletedFileOrFolder() 0 4 1
1
<?php
2
3
namespace Dropbox\Models;
4
5
class ModelFactory
6
{
7
8
    /**
9
     * Make a Model Factory
10
     *
11
     * @param  array $data Model Data
12
     *
13
     * @return \Dropbox\Models\ModelInterface
14
     */
15 22
    public static function make(array $data = array())
16
    {
17
        //Some endpoints return metadata files or folder as sub-nodes of a `metadata` node
18
        //i.e. /files/move_v2
19 22
        if (isset($data['metadata']['.tag'])) {
20 2
            $data = $data['metadata'];
21
        }
22
23 22
        if (static::isFileOrFolder($data)) {
24 20
            $tag = $data['.tag'];
25
26
            //File
27 20
            if (static::isFile($tag)) {
28 13
                return new FileMetadata($data);
29
            }
30
31
            //Folder
32 11
            if (static::isFolder($tag)) {
33 10
                return new FolderMetadata($data);
34
            }
35
36
            //Deleted File/Folder
37 1
            if (static::isDeletedFileOrFolder($tag)) {
38 1
                return new DeletedMetadata($data);
39
            }
40
        }
41
42
        //Temporary Link
43 9
        if (static::isTemporaryLink($data)) {
44 1
            return new TemporaryLink($data);
45
        }
46
47
        //List
48 8
        if (static::isList($data)) {
49 4
            return new MetadataCollection($data);
50
        }
51
52
        //Search Results
53 4
        if (static::isSearchResult($data)) {
54 3
            return new SearchResults($data);
55
        }
56
57
        //File Requests List
58 1
        if (static::isFileRequestList($data)) {
59
            return new FileRequestCollection($data);
60
        }
61
62
        //File Request
63 1
        if (static::isFileRequest($data)) {
64
            return new FileRequest($data);
65
        }
66
67
        //Deleted File/Folder
68 1
        if (static::isDeletedFileOrFolder($data)) {
69
            return new DeletedMetadata($data);
70
        }
71
72
        //Base Model
73 1
        return new BaseModel($data);
74
    }
75
76
    /**
77
     * @param array $data
78
     *
79
     * @return bool
80
     */
81 22
    protected static function isFileOrFolder(array $data)
82
    {
83 22
        return isset($data['.tag']);
84
    }
85
86
    /**
87
     * @param string $tag
88
     *
89
     * @return bool
90
     */
91 20
    protected static function isFile($tag)
92
    {
93 20
        return $tag === 'file';
94
    }
95
96
    /**
97
     * @param string $tag
98
     *
99
     * @return bool
100
     */
101 11
    protected static function isFolder($tag)
102
    {
103 11
        return $tag === 'folder';
104
    }
105
106
    /**
107
     * @param array $data
108
     *
109
     * @return bool
110
     */
111 9
    protected static function isTemporaryLink(array $data)
112
    {
113 9
        return isset($data['metadata']) && isset($data['link']);
114
    }
115
116
    /**
117
     * @param array $data
118
     *
119
     * @return bool
120
     */
121 8
    protected static function isList(array $data)
122
    {
123 8
        return isset($data['entries']);
124
    }
125
126
    /**
127
     * @param array $data
128
     *
129
     * @return bool
130
     */
131 4
    protected static function isSearchResult(array $data)
132
    {
133 4
        return isset($data['matches']);
134
    }
135
136
    /**
137
     * @param string $tag
0 ignored issues
show
Bug introduced by
There is no parameter named $tag. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
138
     *
139
     * @return bool
140
     */
141 1
    protected static function isFileRequestList(array $data)
142
    {
143 1
        return isset($data['file_requests']);
144
    }
145
146
    /**
147
     * @param array $data
148
     *
149
     * @return bool
150
     */
151 1
    protected static function isFileRequest(array $data)
152
    {
153 1
        return isset($data['id']) && isset($data['is_open']);
154
    }
155
156
    /**
157
     * @param string $tag
158
     *
159
     * @return bool
160
     */
161 2
    protected static function isDeletedFileOrFolder($tag)
162
    {
163 2
        return 'deleted' === $tag;
164
    }
165
}
166