Passed
Push — master ( 4ab488...bcfbc7 )
by Bálint
03:58
created

CreateWordPressMetadata::getEntityMapping()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 79
Code Lines 65

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 65
dl 0
loc 79
c 0
b 0
f 0
rs 8.7636
cc 2
nc 2
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
use POData\Providers\Metadata\Type\EdmPrimitiveType;
4
use POData\Common\InvalidOperationException;
5
require_once 'POData\Providers\Metadata\IDataServiceMetadataProvider.php';
6
use POData\Providers\Metadata\SimpleMetadataProvider;
7
8
//Begin Resource Classes
9
10
11
class Post
12
{
13
    //Key Edm.Int32
14
    public $PostID;
15
    //Edm.Int32
16
    public $Author;
17
    //Edm.DateTime
18
    public $Date;
19
    //Edm.DateTime
20
    public $DateGmt;
21
    //Edm.String
22
    public $Content;
23
    //Edm.String
24
    public $Title;
25
    //Edm.String
26
    public $Excerpt;
27
    //Edm.String
28
    public $Status;
29
    //Edm.String
30
    public $CommentStatus;
31
    //Edm.String
32
    public $PingStatus;
33
    //Edm.String
34
    public $Password;
35
    //Edm.String
36
    public $Name;
37
    //Edm.String
38
    public $ToPing;
39
    //Edm.String
40
    public $Pinged;
41
    //Edm.DateTime
42
    public $Modified;
43
    //Edm.DateTime
44
    public $ModifiedGmt;
45
    //Edm.String
46
    public $ContentFiltered;
47
    //Edm.Int32
48
    public $ParentID;
49
    //Edm.String
50
    public $Guid;
51
    //Edm.Int32
52
    public $MenuOrder;
53
    //Edm.String
54
    public $Type;
55
    //Edm.String
56
    public $MimeType;
57
    //Edm.Int32
58
    public $CommentCount;
59
    //Navigation Property User (ResourceReference)
60
    public $User;
61
    //Navigation Property tags (ResourceSetReference)
62
    public $Tags;
63
    //Navigation Property categories (ResourceSetReference)
64
    public $Categories;
65
    //Navigation Property comments (ResourceSetReference)
66
    public $Comments;
67
}
68
69
70
class Tag
71
{
72
    //Key Edm.Int32
73
    public $TagID;
74
    //Edm.String
75
    public $Name;
76
    //Edm.String
77
    public $Slug;
78
    //Edm.String
79
    public $Description;
80
    //Navigation Property Posts (ResourceSetReference)
81
    public $Posts;
82
}
83
84
85
class Category
86
{
87
    //Key Edm.Int32
88
    public $CategoryID;
89
    //Edm.String
90
    public $Name;
91
    //Edm.String
92
    public $Slug;
93
    //Edm.String
94
    public $Description;
95
    //Navigation Property Posts (ResourceSetReference)
96
    public $Posts;
97
}
98
99
100
class Comment
101
{
102
    //Key Edm.Int32
103
    public $CommentID;
104
    //Edm.Int32
105
    public $PostID;
106
    //Edm.String
107
    public $Author;
108
    //Edm.String
109
    public $AuthorEmail;
110
    //Edm.String
111
    public $AuthorUrl;
112
    //Edm.String
113
    public $AuthorIp;
114
    //Edm.DateTime
115
    public $Date;
116
    //Edm.DateTime
117
    public $DateGmt;
118
    //Edm.String
119
    public $Content;
120
    //Edm.Int32
121
    public $Karma;
122
    //Edm.String
123
    public $Approved;
124
    //Edm.String
125
    public $Agent;
126
    //Edm.String
127
    public $Type;
128
    //Edm.Int32
129
    public $ParentID;
130
    //Edm.Int32
131
    public $UserID;
132
    //Navigation Property User (ResourceReference)
133
    public $User;
134
    //Navigation Property Post (ResourceReference)
135
    public $Post;
136
}
137
138
139
class User
140
{
141
    //Key Edm.Int32
142
    public $UserID;
143
    //Edm.String
144
    public $Login;
145
    //Edm.String
146
    public $Nicename;
147
    //Edm.String
148
    public $Email;
149
    //Edm.String
150
    public $Url;
151
    //Edm.DateTime
152
    public $Registered;
153
    //Edm.Int16
154
    public $Status;
155
    //Edm.String
156
    public $DisplayName;
157
    //Navigation Property Posts (ResourceSetReference)
158
    public $Posts;
159
    //Navigation Property Comments (ResourceSetReference)
160
    public $Comments;
161
}
162
163
//End Resource Classes
164
165
166
167
class CreateWordPressMetadata
168
{
169
170
    /**
171
     * Array holding the mapping between the entity properties and coulmns.
172
     * 
173
     * @var array
174
     */
175
    private static $_entityMapping = array();
176
177
    /**
178
     * create metadata
179
     * 
180
     * @throws InvalidOperationException
181
     * 
182
     * @return SimpleMetadataProvider
183
     */
184
    public static function create()
185
    {
186
        $metadata = new SimpleMetadataProvider('WordPressEntities', 'WordPress');
187
    
188
        //Register the entity (resource) type 'Post'
189
        $postsEntityType = $metadata->addEntityType(new ReflectionClass('Post'), 'Post', 'WordPress');
190
        $metadata->addKeyProperty($postsEntityType, 'PostID', EdmPrimitiveType::INT32);
191
        $metadata->addPrimitiveProperty($postsEntityType, 'Author', EdmPrimitiveType::INT32);
192
        $metadata->addPrimitiveProperty($postsEntityType, 'Date', EdmPrimitiveType::DATETIME);
193
        $metadata->addPrimitiveProperty($postsEntityType, 'DateGmt', EdmPrimitiveType::DATETIME);
194
        $metadata->addPrimitiveProperty($postsEntityType, 'Content', EdmPrimitiveType::STRING);
195
        $metadata->addPrimitiveProperty($postsEntityType, 'Title', EdmPrimitiveType::STRING);
196
        $metadata->addPrimitiveProperty($postsEntityType, 'Excerpt', EdmPrimitiveType::STRING);
197
        $metadata->addPrimitiveProperty($postsEntityType, 'Status', EdmPrimitiveType::STRING);
198
        $metadata->addPrimitiveProperty($postsEntityType, 'CommentStatus', EdmPrimitiveType::STRING);
199
        $metadata->addPrimitiveProperty($postsEntityType, 'PingStatus', EdmPrimitiveType::STRING);
200
        $metadata->addPrimitiveProperty($postsEntityType, 'Password', EdmPrimitiveType::STRING);
201
        $metadata->addPrimitiveProperty($postsEntityType, 'Name', EdmPrimitiveType::STRING);
202
        $metadata->addPrimitiveProperty($postsEntityType, 'ToPing', EdmPrimitiveType::STRING);
203
        $metadata->addPrimitiveProperty($postsEntityType, 'Pinged', EdmPrimitiveType::STRING);
204
        $metadata->addETagProperty($postsEntityType, 'Modified', EdmPrimitiveType::DATETIME);
205
        $metadata->addPrimitiveProperty($postsEntityType, 'ModifiedGmt', EdmPrimitiveType::DATETIME);
206
        $metadata->addPrimitiveProperty($postsEntityType, 'ContentFiltered', EdmPrimitiveType::STRING);
207
        $metadata->addPrimitiveProperty($postsEntityType, 'ParentID', EdmPrimitiveType::INT32);
208
        $metadata->addPrimitiveProperty($postsEntityType, 'Guid', EdmPrimitiveType::STRING);
209
        $metadata->addPrimitiveProperty($postsEntityType, 'MenuOrder', EdmPrimitiveType::INT32);
210
        $metadata->addPrimitiveProperty($postsEntityType, 'Type', EdmPrimitiveType::STRING);
211
        $metadata->addPrimitiveProperty($postsEntityType, 'MimeType', EdmPrimitiveType::STRING);
212
        $metadata->addPrimitiveProperty($postsEntityType, 'CommentCount', EdmPrimitiveType::INT32);
213
    
214
        //Register the entity (resource) type 'Tag'
215
        $tagsEntityType = $metadata->addEntityType(new ReflectionClass('Tag'), 'Tag', 'WordPress');
216
        $metadata->addKeyProperty($tagsEntityType, 'TagID', EdmPrimitiveType::INT32);
217
        $metadata->addPrimitiveProperty($tagsEntityType, 'Name', EdmPrimitiveType::STRING);
218
        $metadata->addPrimitiveProperty($tagsEntityType, 'Slug', EdmPrimitiveType::STRING);
219
        $metadata->addPrimitiveProperty($tagsEntityType, 'Description', EdmPrimitiveType::STRING);
220
    
221
        //Register the entity (resource) type 'Category'
222
        $catsEntityType = $metadata->addEntityType(new ReflectionClass('Category'), 'Category', 'WordPress');
223
        $metadata->addKeyProperty($catsEntityType, 'CategoryID', EdmPrimitiveType::INT32);
224
        $metadata->addPrimitiveProperty($catsEntityType, 'Name', EdmPrimitiveType::STRING);
225
        $metadata->addPrimitiveProperty($catsEntityType, 'Slug', EdmPrimitiveType::STRING);
226
        $metadata->addPrimitiveProperty($catsEntityType, 'Description', EdmPrimitiveType::STRING);
227
    
228
        //Register the entity (resource) type 'Comment'
229
        $commentsEntityType = $metadata->addEntityType(new ReflectionClass('Comment'), 'Comment', 'WordPress');
230
        $metadata->addKeyProperty($commentsEntityType, 'CommentID', EdmPrimitiveType::INT32);
231
        $metadata->addPrimitiveProperty($commentsEntityType, 'PostID', EdmPrimitiveType::INT32);
232
        $metadata->addPrimitiveProperty($commentsEntityType, 'Author', EdmPrimitiveType::STRING);
233
        $metadata->addPrimitiveProperty($commentsEntityType, 'AuthorEmail', EdmPrimitiveType::STRING);
234
        $metadata->addPrimitiveProperty($commentsEntityType, 'AuthorUrl', EdmPrimitiveType::STRING);
235
        $metadata->addPrimitiveProperty($commentsEntityType, 'AuthorIp', EdmPrimitiveType::STRING);
236
        $metadata->addETagProperty($commentsEntityType, 'Date', EdmPrimitiveType::DATETIME);
237
        $metadata->addPrimitiveProperty($commentsEntityType, 'DateGmt', EdmPrimitiveType::DATETIME);
238
        $metadata->addPrimitiveProperty($commentsEntityType, 'Content', EdmPrimitiveType::STRING);
239
        $metadata->addPrimitiveProperty($commentsEntityType, 'Karma', EdmPrimitiveType::INT32);
240
        $metadata->addPrimitiveProperty($commentsEntityType, 'Approved', EdmPrimitiveType::STRING);
241
        $metadata->addPrimitiveProperty($commentsEntityType, 'Agent', EdmPrimitiveType::STRING);
242
        $metadata->addPrimitiveProperty($commentsEntityType, 'Type', EdmPrimitiveType::STRING);
243
        $metadata->addPrimitiveProperty($commentsEntityType, 'ParentID', EdmPrimitiveType::INT32);
244
        $metadata->addPrimitiveProperty($commentsEntityType, 'UserID', EdmPrimitiveType::INT32);
245
    
246
        //Register the entity (resource) type 'User'
247
        $usersEntityType = $metadata->addEntityType(new ReflectionClass('User'), 'User', 'WordPress');
248
        $metadata->addKeyProperty($usersEntityType, 'UserID', EdmPrimitiveType::INT32);
249
        $metadata->addPrimitiveProperty($usersEntityType, 'Login', EdmPrimitiveType::STRING);
250
        $metadata->addPrimitiveProperty($usersEntityType, 'Nicename', EdmPrimitiveType::STRING);
251
        $metadata->addPrimitiveProperty($usersEntityType, 'Email', EdmPrimitiveType::STRING);
252
        $metadata->addPrimitiveProperty($usersEntityType, 'Url', EdmPrimitiveType::STRING);
253
        $metadata->addETagProperty($usersEntityType, 'Registered', EdmPrimitiveType::DATETIME);
254
        $metadata->addPrimitiveProperty($usersEntityType, 'Status', EdmPrimitiveType::INT16);
255
        $metadata->addPrimitiveProperty($usersEntityType, 'DisplayName', EdmPrimitiveType::STRING);
256
    
257
        $postsResourceSet = $metadata->addResourceSet('Posts', $postsEntityType);
258
        $tagsResourceSet = $metadata->addResourceSet('Tags', $tagsEntityType);
259
        $catsResourceSet = $metadata->addResourceSet('Categories', $catsEntityType);
260
        $commentsResourceSet = $metadata->addResourceSet('Comments', $commentsEntityType);
261
        $usersResourceSet = $metadata->addResourceSet('Users', $usersEntityType);
262
        //associations of Post
263
        $metadata->addResourceReferenceProperty($postsEntityType, 'User', $usersResourceSet);
264
        $metadata->addResourceSetReferenceProperty($postsEntityType, 'Tags', $tagsResourceSet);
265
        $metadata->addResourceSetReferenceProperty($postsEntityType, 'Categories', $catsResourceSet);
266
        $metadata->addResourceSetReferenceProperty($postsEntityType, 'Comments', $commentsResourceSet);
267
        //associations of Tag
268
        $metadata->addResourceSetReferenceProperty($tagsEntityType, 'Posts', $postsResourceSet);
269
        //associations of Category
270
        $metadata->addResourceSetReferenceProperty($catsEntityType, 'Posts', $postsResourceSet);
271
        //associations of Comment
272
        $metadata->addResourceReferenceProperty($commentsEntityType, 'User', $usersResourceSet);
273
        $metadata->addResourceReferenceProperty($commentsEntityType, 'Post', $postsResourceSet);
274
        //associations of User
275
        $metadata->addResourceSetReferenceProperty($usersEntityType, 'Posts', $postsResourceSet);
276
        $metadata->addResourceSetReferenceProperty($usersEntityType, 'Comments', $commentsResourceSet);
277
    
278
        return $metadata;
279
    }
280
281
    /**
282
     * Gets the the mapping between db table columns and properties of entities.
283
     * 
284
     * @return array(string, array(string, string))
285
     */
286
    public static function getEntityMapping() {
287
        if (!is_null(self::$_entityMapping))
0 ignored issues
show
introduced by
The condition is_null(self::_entityMapping) is always false.
Loading history...
288
        {
289
        self::$_entityMapping = array(
290
            'Post' => array(
291
                '$MappedTable$' => 'wp_posts',
292
                'PostID' => 'ID',
293
                'Author' => 'post_author',
294
                'Date' => 'post_date',
295
                'DateGmt' => 'post_date_gmt',
296
                'Content' => 'post_content',
297
                'Title' => 'post_title',
298
                'Excerpt' => 'post_excerpt',
299
                'Status' => 'post_status',
300
                'CommentStatus' => 'comment_status',
301
                'PingStatus' => 'ping_status',
302
                'Password' => 'post_password',
303
                'Name' => 'post_name',
304
                'ToPing' => 'to_ping',
305
                'Pinged' => 'pinged',
306
                'ModifiedGmt' => 'post_modified_gmt',
307
                'ContentFiltered' => 'post_content_filtered',
308
                'ParentID' => 'post_parent',
309
                'Guid' => 'guid',
310
                'MenuOrder' => 'menu_order',
311
                'Type' => 'post_type',
312
                'MimeType' => 'post_mime_type',
313
                'CommentCount' => 'comment_count'
314
                ),
315
          
316
            'Tag' => array(
317
                '$MappedTable$' => 'wp_terms',
318
                'TagID' =>'t.term_id',
319
                'Name' =>'t.name',
320
                'Slug' =>'t.slug',
321
                'Description' =>'tt.description'
322
            ),
323
          
324
            'Category' => array(
325
                '$MappedTable$' => 'wp_terms',
326
                'CategoryID' =>'t.term_id',
327
                'Name' =>'t.name',
328
                'Slug' =>'t.slug',
329
                'Description' =>'tt.description'
330
            ),
331
          
332
            'Comment' => array(
333
                '$MappedTable$' => 'wp_comments',
334
                'CommentID', 'comment_id',
335
                'PostID', 'comment_post_id',
336
                'Author', 'comment_author',
337
                'AuthorEmail', 'comment_author',
338
                'AuthorUrl', 'comment_author_url',
339
                'AuthorIp', 'comment_author_email',
340
                'DateGmt', 'comment_date',
341
                'Content', 'comment_content',
342
                'Karma', 'comment_karma',
343
                'Approved', 'comment_approved',
344
                'Agent', 'comment_agent',
345
                'Type', 'comment_type',
346
                'ParentID', 'comment_parent',
347
                'UserID', 'user_id'
348
            ),
349
          
350
            'User' => array(
351
                '$MappedTable$' => 'wp_users',
352
                'UserID' => 'ID',
353
                'Login' => 'user_login',
354
                'Nicename' => 'user_nicename',
355
                'Email' => 'user_email',
356
                'Url' => 'user_url',
357
                'Registered' => 'user_registered',
358
                'Status' => 'user_status',
359
                'DisplayName' => 'display_name'
360
            )
361
            );
362
        }
363
      
364
        return self::$_entityMapping;
365
    }
366
}
367