Completed
Push — develop ( 14a8ed...1e743a )
by greg
03:41
created

PostVote::createPost()   D

Complexity

Conditions 14
Paths 39

Size

Total Lines 83
Code Lines 55

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 83
rs 4.9516
c 0
b 0
f 0
cc 14
eloc 55
nc 39
nop 4

How to fix   Long Method    Complexity   

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
namespace PlaygroundGame\Service;
4
5
use Zend\Stdlib\ErrorHandler;
6
7
class PostVote extends Game
8
{
9
    protected $postvoteMapper;
10
    protected $postvoteformMapper;
11
    protected $postVotePostMapper;
12
    protected $postVoteVoteMapper;
13
    protected $postVoteCommentMapper;
14
    protected $postVotePostElementMapper;
15
16
    public function getGameEntity()
17
    {
18
        return new \PlaygroundGame\Entity\PostVote;
19
    }
20
21
    public function getPath($post)
22
    {
23
        $path = $this->getOptions()->getMediaPath() . DIRECTORY_SEPARATOR;
24
        $path .= 'game' . $post->getPostVote()->getId() . DIRECTORY_SEPARATOR;
25
        if (!is_dir($path)) {
26
            mkdir($path, 0777, true);
27
        }
28
        $path .= 'post'. $post->getId() . DIRECTORY_SEPARATOR;
29
        if (!is_dir($path)) {
30
            mkdir($path, 0777, true);
31
        }
32
33
        return $path;
34
    }
35
36 View Code Duplication
    public function getMediaUrl($post)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
37
    {
38
        $media_url = $this->getOptions()->getMediaUrl() . '/';
39
        $media_url .= 'game' . $post->getPostVote()->getId() . '/' . 'post'. $post->getId() . '/';
40
41
        return $media_url;
42
    }
43
44
    /**
45
     * @param boolean $entry
46
     */
47
    public function checkPost($entry)
48
    {
49
        $post = $this->getPostVotePostMapper()->findOneBy(array('entry' => $entry));
50
51
        if (! $post) {
52
            $post = new \PlaygroundGame\Entity\PostVotePost();
53
            $post->setPostvote($entry->getGame());
0 ignored issues
show
Bug introduced by
The method getGame cannot be called on $entry (of type boolean).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
54
            $post->setUser($entry->getUser());
0 ignored issues
show
Bug introduced by
The method getUser cannot be called on $entry (of type boolean).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
55
            $post->setEntry($entry);
56
            $post = $this->getPostVotePostMapper()->insert($post);
57
        }
58
59
        return $post;
60
    }
61
62
    public function uploadFileToPost($data, $game, $user)
63
    {
64
        $result = false;
65
        $entry = $this->findLastActiveEntry($game, $user);
66
67
        if (!$entry) {
68
            return '0';
69
        }
70
71
        $post = $this->checkPost($entry);
72
        $path = $this->getPath($post);
73
        $media_url = $this->getMediaUrl($post);
74
75
        $key = key($data);
76
        $uploadFile = $this->uploadFile($path, $data[$key]);
77
78
        if ($uploadFile) {
79
            $postElement = $this->getPostVotePostElementMapper()->findOneBy(array('post' => $post, 'name' => $key));
80
            if (! $postElement) {
81
                $postElement = new \PlaygroundGame\Entity\PostVotePostElement();
82
            }
83
            $postElement->setName($key);
84
            $postElement->setPosition(0);
85
            $postElement->setValue($media_url.$uploadFile);
86
            $postElement->setPost($post);
87
            $postElement = $this->getPostVotePostElementMapper()->insert($postElement);
0 ignored issues
show
Unused Code introduced by
$postElement is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
88
89
            $result = $media_url.$uploadFile;
90
        }
91
92
        return $result;
93
    }
94
95
    public function deleteFilePosted($data, $game, $user)
96
    {
97
        $postvotePostMapper = $this->getPostVotePostMapper();
98
        $postVotePostElementMapper = $this->getPostVotePostElementMapper();
99
100
        $entry = $this->findLastActiveEntry($game, $user);
101
102
        if (!$entry) {
103
            return 'falsefin0';
104
        }
105
106
        $post = $postvotePostMapper->findOneBy(array('entry' => $entry));
107
        $element = $postVotePostElementMapper->findOneBy(array('post' => $post->getId(), 'name' => $data['name']));
108
109
        if ($element) {
110
            $element = $postVotePostElementMapper->remove($element);
111
            if ($element) {
112
                return true;
113
            } else {
114
                return false;
115
            }
116
        } else {
117
            return false;
118
        }
119
    }
120
121
    /**
122
     *
123
     * @param  array $data
124
     * @return \PlaygroundGame\Entity\Game
125
     */
126
    public function createPost(array $data, $game, $user, $form)
127
    {
128
        $postvotePostMapper = $this->getPostVotePostMapper();
129
        $postVotePostElementMapper = $this->getPostVotePostElementMapper();
130
131
        $entry = $this->findLastActiveEntry($game, $user);
132
133
        if (!$entry) {
134
            return false;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return false; (false) is incompatible with the return type documented by PlaygroundGame\Service\PostVote::createPost of type PlaygroundGame\Entity\Game.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
135
        }
136
137
        $post = $this->checkPost($entry);
138
        $path = $this->getPath($post);
139
        $media_url = $this->getMediaUrl($post);
140
        $position=1;
141
142
        foreach ($data as $name => $value) {
143
            $postElement = $postVotePostElementMapper->findOneBy(array('post' => $post, 'name' => $name));
144
            if (! $postElement) {
145
                $postElement = new \PlaygroundGame\Entity\PostVotePostElement();
146
            }
147
            $postElement->setName($name);
148
            $postElement->setPosition($position);
149
150
            if (is_array($value) && isset($value['tmp_name'])) {
151
                // The file upload has been done in ajax but some weird bugs remain without it
152
153
                if (! $value['error']) {
154
                    ErrorHandler::start();
155
                    $value['name'] = $this->fileNewname($path, $value['name'], true);
156
                    move_uploaded_file($value['tmp_name'], $path . $value['name']);
157
158
                    if (getimagesize($path . $value['name'])) {
159
                        $image = $this->serviceLocator->get('playgroundcore_image_service');
160
                        $image->setImage($path . $value['name']);
161
162
                        if ($image->canCorrectOrientation()) {
163
                            $image->correctOrientation()->save();
164
                        }
165
                        $postElement->setValue($media_url . $value['name']);
166
                        
167
                        if (class_exists("Imagick")) {
168
                            $ext = pathinfo($value['name'], PATHINFO_EXTENSION);
169
                            $img = new \Imagick($path . $value['name']);
170
                            $img->cropThumbnailImage(100, 100);
171
                            $img->setImageCompression(\Imagick::COMPRESSION_JPEG);
172
                            $img->setImageCompressionQuality(75);
173
                            // Strip out unneeded meta data
174
                            $img->stripImage();
175
                            $img->writeImage($path . str_replace('.'.$ext, '-thumbnail.'.$ext, $value['name']));
176
                            ErrorHandler::stop(true);
177
                        }
178
                    }
179
                }
180
            } elseif (is_array($value) || $form->get($name) instanceof \Zend\Form\Element\Select) {
181
                $arValues = $form->get($name)->getValueOptions();
182
                $postElement->setValue($arValues[$value[0]]);
183
            } elseif (!empty($value)) {
184
                $postElement->setValue($value);
185
            }
186
            $postElement->setPost($post);
187
            $postVotePostElementMapper->insert($postElement);
188
            $position++;
189
        }
190
191
        $postvotePostMapper->update($post);
192
193
        // If a preview step is not proposed, I confirmPost on this step
194
        $steps = $game->getStepsArray();
195
        $previewKey = array_search('preview', $steps);
196
        if (!$previewKey) {
197
            $post = $this->confirmPost($game, $user);
198
        }
199
200
        $this->getEventManager()->trigger(__FUNCTION__ . '.post', $this, array(
201
            'user' => $user,
202
            'game' => $game,
203
            'post' => $post,
204
            'entry' => $entry
205
        ));
206
207
        return $post;
208
    }
209
210
    /**
211
     *
212
     * @return \PlaygroundGame\Entity\Game
213
     */
214
    public function confirmPost($game, $user)
215
    {
216
        $postvotePostMapper = $this->getPostVotePostMapper();
217
218
        $entryMapper = $this->getEntryMapper();
219
        $entry = $this->findLastActiveEntry($game, $user);
220
221
        if (!$entry) {
222
            return false;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return false; (false) is incompatible with the return type documented by PlaygroundGame\Service\PostVote::confirmPost of type PlaygroundGame\Entity\Game.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
223
        }
224
225
        $post = $postvotePostMapper->findOneBy(array('entry' => $entry));
226
227
        if (! $post) {
228
            return false;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return false; (false) is incompatible with the return type documented by PlaygroundGame\Service\PostVote::confirmPost of type PlaygroundGame\Entity\Game.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
229
        }
230
231
        // The post is confirmed by user. I update the status and close the associated entry
232
        // Post are validated by default, unless pre-moderation is enable for the game
233
        if ($game->getModerationType()) {
234
            $post->setStatus(1);
235
        } else {
236
            $post->setStatus(2);
237
        }
238
239
        $postvotePostMapper->update($post);
240
241
        $entry->setActive(0);
0 ignored issues
show
Bug introduced by
The method setActive cannot be called on $entry (of type boolean).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
242
        $entryMapper->update($entry);
243
244
        $this->getEventManager()->trigger(__FUNCTION__ . '.post', $this, array(
245
            'user' => $user,
246
            'game' => $game,
247
            'entry' => $entry,
248
            'post' => $post
249
        ));
250
251
        return $post;
252
    }
253
254
    /**
255
     *
256
     * This service is ready for all types of games
257
     *
258
     * @param  array                  $data
259
     * @return \PlaygroundGame\Entity\Game
260
     */
261 View Code Duplication
    public function createForm(array $data, $game, $form = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
262
    {
263
        $title ='';
264
        $description = '';
265
266
        if ($data['form_jsonified']) {
267
            $jsonPV = json_decode($data['form_jsonified']);
268
            foreach ($jsonPV as $element) {
269
                if ($element->form_properties) {
270
                    $attributes  = $element->form_properties[0];
271
                    $title       = $attributes->title;
272
                    $description = $attributes->description;
273
274
                    break;
275
                }
276
            }
277
        }
278
        if (!$form) {
279
            $form = new \PlaygroundGame\Entity\PostVoteForm();
280
        }
281
        $form->setPostvote($game);
282
        $form->setTitle($title);
283
        $form->setDescription($description);
284
        $form->setForm($data['form_jsonified']);
285
        $form->setFormTemplate($data['form_template']);
286
287
        $form = $this->getPostVoteFormMapper()->insert($form);
288
289
        return $form;
290
    }
291
292
    public function findArrayOfValidatedPosts($game, $user, $filter, $search = '')
293
    {
294
        $em = $this->serviceLocator->get('doctrine.entitymanager.orm_default');
295
        $qb = $em->createQueryBuilder();
296
        $and = $qb->expr()->andx();
297
        
298
        $and->add($qb->expr()->eq('p.status', 2));
299
300
        $and->add($qb->expr()->eq('g.id', ':game'));
301
        $qb->setParameter('game', $game);
302
        
303
        if ($search != '') {
304
            $and->add(
305
                $qb->expr()->orX(
306
                    $qb->expr()->like('u.username', $qb->expr()->literal('%:search%')),
307
                    $qb->expr()->like('u.firstname', $qb->expr()->literal('%:search%')),
308
                    $qb->expr()->like('u.lastname', $qb->expr()->literal('%:search%')),
309
                    $qb->expr()->like('e.value', $qb->expr()->literal('%:search%')),
310
                    $qb->expr()->isNull('g.publicationDate')
311
                )
312
            );
313
            $qb->setParameter('search', $search);
314
        }
315
        
316
        if ('push' == $filter) {
317
            $and->add(
318
                $qb->expr()->andX(
319
                    $qb->expr()->eq('p.pushed', 1)
320
                )
321
            );
322
        }
323
        
324
        $qb->select('p, COUNT(DISTINCT v) AS votesCount, COUNT(distinct av) AS voted')
325
            ->from('PlaygroundGame\Entity\PostVotePost', 'p')
326
            ->innerJoin('p.postvote', 'g')
327
            ->leftJoin('p.user', 'u')
328
            ->innerJoin('p.postElements', 'e')
329
            ->leftJoin('p.votes', 'v')
330
            ->leftJoin('p.votes', 'av', 'WITH', 'av.user = :user')
331
            ->where($and)
332
            ->groupBy('p.id');
333
 
334
        if ($user) {
335
            $qb->setParameter('user', $user);
336
        } else {
337
            $qb->setParameter('user', null);
338
        }
339
340
        switch ($filter) {
341
            case 'random':
342
                $qb->orderBy('e.value', 'ASC');
343
                break;
344
            case 'vote':
345
                $qb->orderBy('votesCount', 'DESC');
346
                break;
347
            case 'date':
348
                $qb->orderBy('p.createdAt', 'DESC');
349
                break;
350
            case 'push':
351
                $qb->orderBy('p.createdAt', 'DESC');
352
                break;
353
            case 'id':
354
                $qb->orderBy('p.createdAt', 'ASC');
355
                break;
356
            default:
357
                $qb->orderBy('p.createdAt', 'ASC');
358
                break;
359
        }
360
        
361
        $query = $qb->getQuery();
362
        
363
        $posts = $query->getResult();
364
        $arrayPosts = array();
365
        $i=0;
366
        foreach ($posts as $postRaw) {
367
            $data = array();
368
            $post = $postRaw[0];
369
            if ($post) {
370
                foreach ($post->getPostElements() as $element) {
371
                    $data[$element->getPosition()] = $element->getValue();
372
                }
373
                $arrayPosts[$i]['post']  = $post;
374
                $arrayPosts[$i]['data']  = $data;
375
                $arrayPosts[$i]['votes'] = count($post->getVotes());
376
                $arrayPosts[$i]['voted'] = $postRaw['voted'];
377
                $arrayPosts[$i]['id']    = $post->getId();
378
                $arrayPosts[$i]['user']  = $post->getUser();
379
                $arrayPosts[$i]['createdAt']  = $post->getCreatedAt();
380
                $i++;
381
            }
382
        }
383
384
        return $arrayPosts;
385
    }
386
387
    public function addVote($user, $ipAddress, $post)
388
    {
389
        $postvoteVoteMapper = $this->getPostVoteVoteMapper();
390
        $postId = $post->getId();
391
392
        if ($user) {
393
            $entryUser = count($postvoteVoteMapper->findBy(array('user' => $user, 'post' =>$postId)));
394
        } else {
395
            $entryUser =count($postvoteVoteMapper->findBy(array('ip' => $ipAddress, 'post' =>$postId)));
396
        }
397
        if ($entryUser && $entryUser > 0) {
398
            return false;
399
        } else {
400
            $vote = new \PlaygroundGame\Entity\PostVoteVote();
401
            $vote->setPost($post);
402
            $vote->setIp($ipAddress);
403
            $vote->setNote(1);
404
            $vote->setPostvote($post->getPostvote());
405
            if ($user) {
406
                $vote->setUser($user);
407
            }
408
409
            $postvoteVoteMapper->insert($vote);
410
            $game = $post->getPostvote();
411
            $this->getEventManager()->trigger(
412
                __FUNCTION__ .'.post',
413
                $this,
414
                array('user' => $user, 'game' => $game, 'post' => $post, 'vote' => $vote)
415
            );
416
417
            return true;
418
        }
419
    }
420
421
    public function addComment($user, $ipAddress, $post, $message = '')
422
    {
423
        $postvoteCommentMapper = $this->getPostVoteCommentMapper();
424
        $game = $post->getPostvote();
425
        $comment = new \PlaygroundGame\Entity\PostVoteComment();
426
        $comment->setPost($post);
427
        $comment->setIp($ipAddress);
428
        $message = strip_tags($message);
429
        $comment->setMessage($message);
430
        $comment->setPostvote($game);
431
        if ($user) {
432
            $comment->setUser($user);
433
        }
434
435
        $postvoteCommentMapper->insert($comment);
436
        
437
        $this->getEventManager()->trigger(
438
            __FUNCTION__ .'.post',
439
            $this,
440
            array('user' => $user, 'game' => $game, 'post' => $post, 'comment' => $comment)
441
        );
442
443
        return true;
444
    }
445
446
447
    public function addShare($post)
448
    {
449
        $postvotePostMapper = $this->getPostVotePostMapper();
450
        $post->setShares($post->getShares()+1);
451
        $postvotePostMapper->update($post);
452
453
        $this->getEventManager()->trigger(__FUNCTION__ . '.post', $this, array(
454
            'post' => $post
455
        ));
456
457
        return true;
458
    }
459
    /**
460
     * Get all comments for this game
461
     */
462
    public function getCommentsForPostvote($postvote)
463
    {
464
        $postvoteCommentMapper = $this->getPostVoteCommentMapper();
465
        $comments = $postvoteCommentMapper->findBy(array('postvote' => $postvote), array('createdAt' => 'DESC'));
466
467
        return $comments ;
468
    }
469
470
    public function removeComment($user, $ipAddress, $messageId)
0 ignored issues
show
Unused Code introduced by
The parameter $ipAddress is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
471
    {
472
        $postvoteCommentMapper = $this->getPostVoteCommentMapper();
473
        $comment = $postvoteCommentMapper->findOneBy(array('id' => $messageId));
474
        if ($comment->getUser()->getId() === $user->getId()) {
475
            $postvoteCommentMapper->remove($comment);
476
            $this->getEventManager()->trigger(
477
                'remove_comment_postvote.post',
478
                $this,
479
                array('user' => $user, 'comment' => $comment)
480
            );
481
482
            return true;
483
        }
484
485
        return false;
486
    }
487
488
    public function getEntriesHeader($game)
489
    {
490
        $header = parent::getEntriesHeader($game);
491
        if ($game->getForm()) {
492
            $form = json_decode($game->getForm()->getForm(), true);
493 View Code Duplication
            foreach ($form as $element) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
494
                foreach ($element as $k => $v) {
495
                    if ($k !== 'form_properties') {
496
                        $header[$v[0]['name']] = 1;
497
                    }
498
                }
499
            }
500
        }
501
502
        return $header;
503
    }
504
505 View Code Duplication
    public function getEntriesQuery($game)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
506
    {
507
        $em = $this->serviceLocator->get('doctrine.entitymanager.orm_default');
508
509
        $qb = $em->createQueryBuilder();
510
        $qb->select('
511
            p.id,
512
            u.username,
513
            u.title,
514
            u.firstname,
515
            u.lastname,
516
            u.email,
517
            u.optin,
518
            u.optinPartner,
519
            u.address,
520
            u.address2,
521
            u.postalCode,
522
            u.city,
523
            u.telephone,
524
            u.mobile,
525
            u.created_at,
526
            u.dob,
527
            e.winner,
528
            e.socialShares,
529
            e.playerData,
530
            e.updated_at,
531
            p.status,
532
            p
533
            ')
534
            ->from('PlaygroundGame\Entity\PostVotePost', 'p')
535
            ->innerJoin('p.entry', 'e')
536
            ->leftJoin('p.user', 'u')
537
            ->where($qb->expr()->eq('e.game', ':game'));
538
        
539
        $qb->setParameter('game', $game);
540
541
        return $qb->getQuery();
542
    }
543
544
    /**
545
    * getGameEntries : All entries of a game
546
    *
547
    * @return Array of PlaygroundGame\Entity\Game
548
    */
549
    public function getGameEntries($header, $entries, $game)
550
    {
551
        $results = array();
552
553
        foreach ($entries as $k => $entry) {
554
            $entryData = json_decode($entry['playerData'], true);
555
            $postElements = $entry[0]->getPostElements();
556
557
            foreach ($header as $key => $v) {
558
                if (isset($entryData[$key]) && $key !=='id') {
559
                    $results[$k][$key] = (is_array($entryData[$key]))?implode(', ', $entryData[$key]):$entryData[$key];
560
                } elseif (array_key_exists($key, $entry)) {
561
                    $results[$k][$key] = ($entry[$key] instanceof \DateTime)?$entry[$key]->format('Y-m-d'):$entry[$key];
562
                } else {
563
                    $results[$k][$key] = '';
564
                }
565
566
                foreach ($postElements as $e) {
567
                    if ($key === $e->getName()) {
568
                        $results[$k][$key] = (is_array($e->getValue()))?implode(', ', $e->getValue()):$e->getValue();
569
                        break;
570
                    }
571
                }
572
            }
573
        }
574
575
        return $results;
576
    }
577
578
    public function getPostVoteFormMapper()
579
    {
580
        if (null === $this->postvoteformMapper) {
581
            $this->postvoteformMapper = $this->serviceLocator->get('playgroundgame_postvoteform_mapper');
582
        }
583
584
        return $this->postvoteformMapper;
585
    }
586
587
    public function setPostVoteFormMapper($postvoteformMapper)
588
    {
589
        $this->postvoteformMapper = $postvoteformMapper;
590
591
        return $this;
592
    }
593
594
    public function getPostVotePostElementMapper()
595
    {
596
        if (null === $this->postVotePostElementMapper) {
597
            $this->postVotePostElementMapper = $this->serviceLocator->get(
598
                'playgroundgame_postvotepostelement_mapper'
599
            );
600
        }
601
602
        return $this->postVotePostElementMapper;
603
    }
604
605
    public function setPostVotePostElementMapper($postVotePostElementMapper)
606
    {
607
        $this->postVotePostElementMapper = $postVotePostElementMapper;
608
609
        return $this;
610
    }
611
612
    public function getPostVoteVoteMapper()
613
    {
614
        if (null === $this->postVoteVoteMapper) {
615
            $this->postVoteVoteMapper = $this->serviceLocator->get('playgroundgame_postvotevote_mapper');
616
        }
617
618
        return $this->postVoteVoteMapper;
619
    }
620
621
    public function setPostVoteVoteMapper($postVoteVoteMapper)
622
    {
623
        $this->postVoteVoteMapper = $postVoteVoteMapper;
624
625
        return $this;
626
    }
627
628
    public function getPostVoteCommentMapper()
629
    {
630
        if (null === $this->postVoteCommentMapper) {
631
            $this->postVoteCommentMapper = $this->serviceLocator->get('playgroundgame_postvotecomment_mapper');
632
        }
633
634
        return $this->postVoteCommentMapper;
635
    }
636
637
    public function setPostVoteCommentMapper($postVoteCommentMapper)
638
    {
639
        $this->postVoteCommentMapper = $postVoteCommentMapper;
640
641
        return $this;
642
    }
643
644
    public function getPostVotePostMapper()
645
    {
646
        if (null === $this->postVotePostMapper) {
647
            $this->postVotePostMapper = $this->serviceLocator->get('playgroundgame_postvotepost_mapper');
648
        }
649
650
        return $this->postVotePostMapper;
651
    }
652
653
    public function setPostVotePostMapper($postVotePostMapper)
654
    {
655
        $this->postVotePostMapper = $postVotePostMapper;
656
657
        return $this;
658
    }
659
660
    public function getPostVoteMapper()
661
    {
662
        if (null === $this->postvoteMapper) {
663
            $this->postvoteMapper = $this->serviceLocator->get('playgroundgame_postvote_mapper');
664
        }
665
666
        return $this->postvoteMapper;
667
    }
668
669
    /**
670
     * setQuizQuestionMapper
671
     *
672
     * @return PostVote
673
     */
674
    public function setPostVoteMapper($postvoteMapper)
675
    {
676
        $this->postvoteMapper = $postvoteMapper;
677
678
        return $this;
679
    }
680
}
681