Completed
Pull Request — master (#97)
by
unknown
03:44
created

ModelFactory   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 126
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 17
lcom 1
cbo 7
dl 0
loc 126
ccs 32
cts 32
cp 1
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
D make() 0 45 9
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 isDeletedFileOrFolder() 0 4 1
1
<?php
2
3
namespace Kunnu\Dropbox\Models;
4
5
class ModelFactory
6
{
7
8
    /**
9
     * Make a Model Factory
10
     *
11
     * @param  array $data Model Data
12
     *
13
     * @return \Kunnu\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
        //Base Model
58 1
        return new BaseModel($data);
59
    }
60
61
    /**
62
     * @param array $data
63
     *
64
     * @return bool
65
     */
66 22
    protected static function isFileOrFolder(array $data)
67
    {
68 22
        return isset($data['.tag']);
69
    }
70
71
    /**
72
     * @param string $tag
73
     *
74
     * @return bool
75
     */
76 20
    protected static function isFile($tag)
77
    {
78 20
        return $tag === 'file';
79
    }
80
81
    /**
82
     * @param string $tag
83
     *
84
     * @return bool
85
     */
86 11
    protected static function isFolder($tag)
87
    {
88 11
        return $tag === 'folder';
89
    }
90
91
    /**
92
     * @param array $data
93
     *
94
     * @return bool
95
     */
96 9
    protected static function isTemporaryLink(array $data)
97
    {
98 9
        return isset($data['metadata']) && isset($data['link']);
99
    }
100
101
    /**
102
     * @param array $data
103
     *
104
     * @return bool
105
     */
106 8
    protected static function isList(array $data)
107
    {
108 8
        return isset($data['entries']);
109
    }
110
111
    /**
112
     * @param array $data
113
     *
114
     * @return bool
115
     */
116 4
    protected static function isSearchResult(array $data)
117
    {
118 4
        return isset($data['matches']);
119
    }
120
121
    /**
122
     * @param string $tag
123
     *
124
     * @return bool
125
     */
126 1
    protected static function isDeletedFileOrFolder($tag)
127
    {
128 1
        return 'deleted' === $tag;
129
    }
130
}
131