Completed
Push — master ( fe2a3e...0bf495 )
by greg
07:03 queued 03:32
created

PostVote::setPostVoteFormMapper()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
3
namespace PlaygroundGame\Service;
4
5
use Zend\ServiceManager\ServiceManagerAwareInterface;
6
use Zend\Stdlib\ErrorHandler;
7
8
class PostVote extends Game implements ServiceManagerAwareInterface
9
{
10
    protected $postvoteMapper;
11
    protected $postvoteformMapper;
12
    protected $postVotePostMapper;
13
    protected $postVoteVoteMapper;
14
    protected $postVoteCommentMapper;
15
    protected $postVotePostElementMapper;
16
17
    public function getGameEntity()
18
    {
19
        return new \PlaygroundGame\Entity\PostVote;
20
    }
21
22
    public function getPath($post)
23
    {
24
        $path = $this->getOptions()->getMediaPath() . DIRECTORY_SEPARATOR;
25
        $path .= 'game' . $post->getPostVote()->getId() . DIRECTORY_SEPARATOR;
26
        if (!is_dir($path)) {
27
            mkdir($path, 0777, true);
28
        }
29
        $path .= 'post'. $post->getId() . DIRECTORY_SEPARATOR;
30
        if (!is_dir($path)) {
31
            mkdir($path, 0777, true);
32
        }
33
34
        return $path;
35
    }
36
37
    public function getMediaUrl($post)
38
    {
39
        $media_url = $this->getOptions()->getMediaUrl() . '/';
40
        $media_url .= 'game' . $post->getPostVote()->getId() . '/' . 'post'. $post->getId() . '/';
41
42
        return $media_url;
43
    }
44
45
    /**
46
     * @param boolean $entry
47
     */
48
    public function checkPost($entry)
49
    {
50
        $post = $this->getPostVotePostMapper()->findOneBy(array('entry' => $entry));
51
52
        if (! $post) {
53
            $post = new \PlaygroundGame\Entity\PostVotePost();
54
            $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...
55
            $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...
56
            $post->setEntry($entry);
57
            $post = $this->getPostVotePostMapper()->insert($post);
58
        }
59
60
        return $post;
61
    }
62
63
    public function uploadFileToPost($data, $game, $user)
64
    {
65
        $result = false;
66
        $entry = $this->findLastActiveEntry($game, $user);
67
68
        if (!$entry) {
69
            return '0';
70
        }
71
72
        $post = $this->checkPost($entry);
73
        $path = $this->getPath($post);
74
        $media_url = $this->getMediaUrl($post);
75
76
        $key = key($data);
77
        $uploadFile = $this->uploadFile($path, $data[$key]);
78
79
        if ($uploadFile) {
80
            $postElement = $this->getPostVotePostElementMapper()->findOneBy(array('post' => $post, 'name' => $key));
81
            if (! $postElement) {
82
                $postElement = new \PlaygroundGame\Entity\PostVotePostElement();
83
            }
84
            $postElement->setName($key);
85
            $postElement->setPosition(0);
86
            $postElement->setValue($media_url.$uploadFile);
87
            $postElement->setPost($post);
88
            $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...
89
90
            $result = $media_url.$uploadFile;
91
        }
92
93
        return $result;
94
    }
95
96
    public function deleteFilePosted($data, $game, $user)
97
    {
98
        $postvotePostMapper = $this->getPostVotePostMapper();
99
        $postVotePostElementMapper = $this->getPostVotePostElementMapper();
100
101
        $entry = $this->findLastActiveEntry($game, $user);
102
103
        if (!$entry) {
104
            return 'falsefin0';
105
        }
106
107
        $post = $postvotePostMapper->findOneBy(array('entry' => $entry));
108
        $element = $postVotePostElementMapper->findOneBy(array('post' => $post->getId(), 'name' => $data['name']));
109
110
        if ($element) {
111
            $element = $postVotePostElementMapper->remove($element);
112
            if ($element) {
113
                return true;
114
            } else {
115
                return false;
116
            }
117
        } else {
118
            return false;
119
        }
120
    }
121
122
    /**
123
     *
124
     * @param  array $data
125
     * @return \PlaygroundGame\Entity\Game
126
     */
127
    public function createPost(array $data, $game, $user, $form)
128
    {
129
        $postvotePostMapper = $this->getPostVotePostMapper();
130
        $postVotePostElementMapper = $this->getPostVotePostElementMapper();
131
132
        $entry = $this->findLastActiveEntry($game, $user);
133
134
        if (!$entry) {
135
            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...
136
        }
137
138
        $post = $this->checkPost($entry);
139
        $path = $this->getPath($post);
140
        $media_url = $this->getMediaUrl($post);
141
        $position=1;
142
143
        foreach ($data as $name => $value) {
144
            $postElement = $postVotePostElementMapper->findOneBy(array('post' => $post, 'name' => $name));
145
            if (! $postElement) {
146
                $postElement = new \PlaygroundGame\Entity\PostVotePostElement();
147
            }
148
            $postElement->setName($name);
149
            $postElement->setPosition($position);
150
151
            if (is_array($value) && isset($value['tmp_name'])) {
152
                // The file upload has been done in ajax but some weird bugs remain without it
153
154
                if (! $value['error']) {
155
                    ErrorHandler::start();
156
                    $value['name'] = $this->fileNewname($path, $value['name'], true);
157
                    move_uploaded_file($value['tmp_name'], $path . $value['name']);
158
                    $image = $this->getServiceManager()->get('playgroundcore_image_service');
159
                    $image->setImage($path . $value['name']);
160
161
                    if ($image->canCorrectOrientation()) {
162
                        $image->correctOrientation()->save();
163
                    }
164
                    $postElement->setValue($media_url . $value['name']);
165
                    
166
                    if (class_exists("Imagick")) {
167
                        $ext = pathinfo($value['name'], PATHINFO_EXTENSION);
168
                        $img = new \Imagick($path . $value['name']);
169
                        $img->cropThumbnailImage(100, 100);
170
                        $img->setImageCompression(\Imagick::COMPRESSION_JPEG);
171
                        $img->setImageCompressionQuality(75);
172
                        // Strip out unneeded meta data
173
                        $img->stripImage();
174
                        $img->writeImage($path . str_replace('.'.$ext, '-thumbnail.'.$ext, $value['name']));
175
                        ErrorHandler::stop(true);
176
                    }
177
                }
178
            } elseif (is_array($value) || $form->get($name) instanceof \Zend\Form\Element\Select) {
179
                $arValues = $form->get($name)->getValueOptions();
180
                $postElement->setValue($arValues[$value[0]]);
181
            } elseif (!empty($value)) {
182
                $postElement->setValue($value);
183
            }
184
            $postElement->setPost($post);
185
            $postVotePostElementMapper->insert($postElement);
186
            $position++;
187
        }
188
189
        $postvotePostMapper->update($post);
190
191
        // If a preview step is not proposed, I confirmPost on this step
192
        $steps = $game->getStepsArray();
193
        $previewKey = array_search('preview', $steps);
194
        if (!$previewKey) {
195
            $post = $this->confirmPost($game, $user);
196
        }
197
198
        $this->getEventManager()->trigger(__FUNCTION__ . '.post', $this, array(
199
            'user' => $user,
200
            'game' => $game,
201
            'post' => $post,
202
            'entry' => $entry
203
        ));
204
205
        return $post;
206
    }
207
208
    /**
209
     *
210
     * @return \PlaygroundGame\Entity\Game
211
     */
212
    public function confirmPost($game, $user)
213
    {
214
        $postvotePostMapper = $this->getPostVotePostMapper();
215
216
        $entryMapper = $this->getEntryMapper();
217
        $entry = $this->findLastActiveEntry($game, $user);
218
219
        if (!$entry) {
220
            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...
221
        }
222
223
        $post = $postvotePostMapper->findOneBy(array('entry' => $entry));
224
225
        if (! $post) {
226
            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...
227
        }
228
229
        // The post is confirmed by user. I update the status and close the associated entry
230
        // Post are validated by default, unless pre-moderation is enable for the game
231
        if ($game->getModerationType()) {
232
            $post->setStatus(1);
233
        } else {
234
            $post->setStatus(2);
235
        }
236
237
        $postvotePostMapper->update($post);
238
239
        $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...
240
        $entryMapper->update($entry);
241
242
        $this->getEventManager()->trigger(__FUNCTION__ . '.post', $this, array(
243
            'user' => $user,
244
            'game' => $game,
245
            'entry' => $entry,
246
            'post' => $post
247
        ));
248
249
        if ($user) {
250
            // send mail for participation
251
            $this->sendGameMail($game, $user, $post, 'postvote');
252
        }
253
254
        return $post;
255
    }
256
257
    /**
258
     *
259
     * This service is ready for all types of games
260
     *
261
     * @param  array                  $data
262
     * @return \PlaygroundGame\Entity\Game
263
     */
264 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...
265
    {
266
        $title ='';
267
        $description = '';
268
269
        if ($data['form_jsonified']) {
270
            $jsonPV = json_decode($data['form_jsonified']);
271
            foreach ($jsonPV as $element) {
272
                if ($element->form_properties) {
273
                    $attributes  = $element->form_properties[0];
274
                    $title       = $attributes->title;
275
                    $description = $attributes->description;
276
277
                    break;
278
                }
279
            }
280
        }
281
        if (!$form) {
282
            $form = new \PlaygroundGame\Entity\PostVoteForm();
283
        }
284
        $form->setPostvote($game);
285
        $form->setTitle($title);
286
        $form->setDescription($description);
287
        $form->setForm($data['form_jsonified']);
288
        $form->setFormTemplate($data['form_template']);
289
290
        $form = $this->getPostVoteFormMapper()->insert($form);
291
292
        return $form;
293
    }
294
295
    public function findArrayOfValidatedPosts($game, $user, $filter, $search = '')
296
    {
297
        $em = $this->getServiceManager()->get('doctrine.entitymanager.orm_default');
298
        $qb = $em->createQueryBuilder();
299
        $and = $qb->expr()->andx();
300
        
301
        $and->add($qb->expr()->eq('p.status', 2));
302
303
        $and->add($qb->expr()->eq('g.id', ':game'));
304
        $qb->setParameter('game', $game);
305
        
306
        if ($search != '') {
307
            $and->add(
308
                $qb->expr()->orX(
309
                    $qb->expr()->like('u.username', $qb->expr()->literal('%:search%')),
310
                    $qb->expr()->like('u.firstname', $qb->expr()->literal('%:search%')),
311
                    $qb->expr()->like('u.lastname', $qb->expr()->literal('%:search%')),
312
                    $qb->expr()->like('e.value', $qb->expr()->literal('%:search%')),
313
                    $qb->expr()->isNull('g.publicationDate')
314
                )
315
            );
316
            $qb->setParameter('search', $search);
317
        }
318
        
319
        if ('push' == $filter) {
320
            $and->add(
321
                $qb->expr()->andX(
322
                    $qb->expr()->eq('p.pushed', 1)
323
                )
324
            );
325
        }
326
        
327
        $qb->select('p, COUNT(DISTINCT v) AS votesCount, COUNT(distinct av) AS voted')
328
            ->from('PlaygroundGame\Entity\PostVotePost', 'p')
329
            ->innerJoin('p.postvote', 'g')
330
            ->leftJoin('p.user', 'u')
331
            ->innerJoin('p.postElements', 'e')
332
            ->leftJoin('p.votes', 'v')
333
            ->leftJoin('p.votes', 'av', 'WITH', 'av.userId = :userId')
334
            ->where($and)
335
            ->groupBy('p.id');
336
 
337
        if($user){
338
            $qb->setParameter('userId', $user->getId());
339
        } else {
340
            $qb->setParameter('userId', 0);
341
        }
342
343
        switch ($filter) {
344
            case 'random':
345
                $qb->orderBy('e.value', 'ASC');
346
                break;
347
            case 'vote':
348
                $qb->orderBy('votesCount', 'DESC');
349
                break;
350
            case 'date':
351
                $qb->orderBy('p.createdAt', 'DESC');
352
                break;
353
            case 'push':
354
                $qb->orderBy('p.createdAt', 'DESC');
355
                break;
356
        }
357
        
358
        $query = $qb->getQuery();
359
        
360
        $posts = $query->getResult();
361
        $arrayPosts = array();
362
        $i=0;
363
        foreach ($posts as $postRaw) {
364
            $data = array();
365
            $post = $postRaw[0];
366
            if ($post) {
367
                foreach ($post->getPostElements() as $element) {
368
                    $data[$element->getPosition()] = $element->getValue();
369
                }
370
                $arrayPosts[$i]['post']  = $post;
371
                $arrayPosts[$i]['data']  = $data;
372
                $arrayPosts[$i]['votes'] = count($post->getVotes());
373
                $arrayPosts[$i]['voted'] = $postRaw['voted'];
374
                $arrayPosts[$i]['id']    = $post->getId();
375
                $arrayPosts[$i]['user']  = $post->getUser();
376
                $arrayPosts[$i]['createdAt']  = $post->getCreatedAt();
377
                $i++;
378
            }
379
        }
380
381
        return $arrayPosts;
382
    }
383
384
    public function addVote($user, $ipAddress, $post)
385
    {
386
        $postvoteVoteMapper = $this->getPostVoteVoteMapper();
387
        $postId = $post->getId();
388
389
        if ($user) {
390
            $userId = $user->getId();
391
            $entryUser = count($postvoteVoteMapper->findBy(array('userId' => $userId, 'post' =>$postId)));
392
        } else {
393
            $entryUser =count($postvoteVoteMapper->findBy(array('ip' => $ipAddress, 'post' =>$postId)));
394
        }
395
        if ($entryUser && $entryUser > 0) {
396
397
            return false;
398
        } else {
399
            $vote = new \PlaygroundGame\Entity\PostVoteVote();
400
            $vote->setPost($post);
401
            $vote->setIp($ipAddress);
402
            $vote->setNote(1);
0 ignored issues
show
Documentation introduced by
1 is of type integer, but the function expects a object<PlaygroundGame\Entity\unknown_type>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
403
            if ($user) {
404
                $vote->setUserId($user->getId());
405
            }
406
407
            $postvoteVoteMapper->insert($vote);
408
            $game = $post->getPostvote();
409
            $this->getEventManager()->trigger(
410
                'vote_postvote.post',
411
                $this,
412
                array('user' => $user, 'game' => $game, 'post' => $post, 'vote' => $vote)
413
            );
414
415
            return true;
416
        }
417
    }
418
419
    public function addComment($user, $ipAddress, $post, $message = '')
420
    {
421
        $postvoteCommentMapper = $this->getPostVoteCommentMapper();
422
        $postId = $post->getId();
0 ignored issues
show
Unused Code introduced by
$postId 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...
423
        $game = $post->getPostvote();
424
        $comment = new \PlaygroundGame\Entity\PostVoteComment();
425
        $comment->setPost($post);
426
        $comment->setIp($ipAddress);
427
        $comment->setMessage($message);
0 ignored issues
show
Documentation introduced by
$message is of type string, but the function expects a object<PlaygroundGame\Entity\unknown_type>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
428
        $comment->setPostvote($game);
429
        if ($user) {
430
            $comment->setUser($user);
431
        }
432
433
        $postvoteCommentMapper->insert($comment);
434
        
435
        $this->getEventManager()->trigger(
436
            'comment_postvote.post',
437
            $this,
438
            array('user' => $user, 'game' => $game, 'post' => $post, 'comment' => $comment)
439
        );
440
441
        return true;
442
    }
443
444
    /**
445
     * Get all comments for this game
446
     */
447
    public function getCommentsForPostvote($postvote)
448
    {
449
        $postvoteCommentMapper = $this->getPostVoteCommentMapper();
450
        $comments = $postvoteCommentMapper->findBy(array('postvote' => $postvote));
451
452
        return $comments ;
453
    }
454
455
    /**
456
     * Get all comments for this post
457
     */
458
    public function getCommentsForPost($post)
459
    {
460
        $postvoteCommentMapper = $this->getPostVoteCommentMapper();
461
        $comments = $postvoteCommentMapper->findBy(array('post' => $post));
462
463
        return $comments ;
464
    }
465
466
    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...
467
    {
468
        $postvoteCommentMapper = $this->getPostVoteCommentMapper();
469
        $comment = $postvoteCommentMapper->findOneBy(array('id' => $messageId));
470
        if($comment->getUser()->getId() === $user->getId()){
471
            $postvoteCommentMapper->remove($comment);
472
            $this->getEventManager()->trigger(
473
                'remove_comment_postvote.post',
474
                $this,
475
                array('user' => $user,'comment' => $comment)
476
            );
477
478
            return true;
479
        }
480
481
        return false;
482
    }
483
484
    public function getEntriesHeader($game)
485
    {
486
        $header = parent::getEntriesHeader($game);
487
        if ($game->getForm()) {
488
            $form = json_decode($game->getForm()->getForm(), true);
489 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...
490
                foreach ($element as $k => $v) {
491
                    if ($k !== 'form_properties') {
492
                        $header[$v[0]['name']] = 1;
493
                    }
494
                }
495
            }
496
        }
497
498
        return $header;
499
    }
500
501 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...
502
    {
503
        $em = $this->getServiceManager()->get('doctrine.entitymanager.orm_default');
504
505
        $qb = $em->createQueryBuilder();
506
        $qb->select('
507
            p.id,
508
            u.username,
509
            u.title,
510
            u.firstname,
511
            u.lastname,
512
            u.email,
513
            u.optin,
514
            u.optinPartner,
515
            u.address,
516
            u.address2,
517
            u.postalCode,
518
            u.city,
519
            u.telephone,
520
            u.mobile,
521
            u.created_at,
522
            u.dob,
523
            e.winner,
524
            e.socialShares,
525
            e.playerData,
526
            e.updated_at,
527
            p.status,
528
            p
529
            ')
530
            ->from('PlaygroundGame\Entity\PostVotePost', 'p')
531
            ->innerJoin('p.entry', 'e')
532
            ->leftJoin('p.user', 'u')
533
            ->where($qb->expr()->eq('e.game', ':game'));
534
        
535
        $qb->setParameter('game', $game);
536
537
        return $qb->getQuery();
538
    }
539
540
    /**
541
    * getGameEntries : All entries of a game
542
    *
543
    * @return Array of PlaygroundGame\Entity\Game
544
    */
545
    public function getGameEntries($header, $entries, $game)
546
    {
547
        $results = array();
548
549
        foreach ($entries as $k => $entry) {
550
            $entryData = json_decode($entry['playerData'], true);
551
            $postElements = $entry[0]->getPostElements();
552
553
            foreach ($header as $key => $v) {
554
                if (isset($entryData[$key]) && $key !=='id') {
555
                    $results[$k][$key] = (is_array($entryData[$key]))?implode(', ', $entryData[$key]):$entryData[$key];
556
                } elseif (array_key_exists($key, $entry)) {
557
                    $results[$k][$key] = ($entry[$key] instanceof \DateTime)?$entry[$key]->format('Y-m-d'):$entry[$key];
558
                } else {
559
                    $results[$k][$key] = '';
560
                }
561
562
                foreach ($postElements as $e) {
563
                    if ($key === $e->getName()) {
564
                        $results[$k][$key] = (is_array($e->getValue()))?implode(', ', $e->getValue()):$e->getValue();
565
                        break;
566
                    }
567
                }
568
            }
569
        }
570
571
        return $results;
572
    }
573
574
    public function getPostVoteFormMapper()
575
    {
576
        if (null === $this->postvoteformMapper) {
577
            $this->postvoteformMapper = $this->getServiceManager()->get('playgroundgame_postvoteform_mapper');
578
        }
579
580
        return $this->postvoteformMapper;
581
    }
582
583
    public function setPostVoteFormMapper($postvoteformMapper)
584
    {
585
        $this->postvoteformMapper = $postvoteformMapper;
586
587
        return $this;
588
    }
589
590
    public function getPostVotePostElementMapper()
591
    {
592
        if (null === $this->postVotePostElementMapper) {
593
            $this->postVotePostElementMapper = $this->getServiceManager()->get(
594
                'playgroundgame_postvotepostelement_mapper'
595
            );
596
        }
597
598
        return $this->postVotePostElementMapper;
599
    }
600
601
    public function setPostVotePostElementMapper($postVotePostElementMapper)
602
    {
603
        $this->postVotePostElementMapper = $postVotePostElementMapper;
604
605
        return $this;
606
    }
607
608
    public function getPostVoteVoteMapper()
609
    {
610
        if (null === $this->postVoteVoteMapper) {
611
            $this->postVoteVoteMapper = $this->getServiceManager()->get('playgroundgame_postvotevote_mapper');
612
        }
613
614
        return $this->postVoteVoteMapper;
615
    }
616
617
    public function setPostVoteVoteMapper($postVoteVoteMapper)
618
    {
619
        $this->postVoteVoteMapper = $postVoteVoteMapper;
620
621
        return $this;
622
    }
623
624
    public function getPostVoteCommentMapper()
625
    {
626
        if (null === $this->postVoteCommentMapper) {
627
            $this->postVoteCommentMapper = $this->getServiceManager()->get('playgroundgame_postvotecomment_mapper');
628
        }
629
630
        return $this->postVoteCommentMapper;
631
    }
632
633
    public function setPostVoteCommentMapper($postVoteCommentMapper)
634
    {
635
        $this->postVoteCommentMapper = $postVoteCommentMapper;
636
637
        return $this;
638
    }
639
640
    public function getPostVotePostMapper()
641
    {
642
        if (null === $this->postVotePostMapper) {
643
            $this->postVotePostMapper = $this->getServiceManager()->get('playgroundgame_postvotepost_mapper');
644
        }
645
646
        return $this->postVotePostMapper;
647
    }
648
649
    public function setPostVotePostMapper($postVotePostMapper)
650
    {
651
        $this->postVotePostMapper = $postVotePostMapper;
652
653
        return $this;
654
    }
655
656
    public function getPostVoteMapper()
657
    {
658
        if (null === $this->postvoteMapper) {
659
            $this->postvoteMapper = $this->getServiceManager()->get('playgroundgame_postvote_mapper');
660
        }
661
662
        return $this->postvoteMapper;
663
    }
664
665
    /**
666
     * setQuizQuestionMapper
667
     *
668
     * @return PostVote
669
     */
670
    public function setPostVoteMapper($postvoteMapper)
671
    {
672
        $this->postvoteMapper = $postvoteMapper;
673
674
        return $this;
675
    }
676
}
677