Completed
Push — develop ( 8b48d6...0ad85f )
by greg
10:00
created

PostVote::addShare()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 12
rs 9.4285
cc 1
eloc 7
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.user = :user')
334
            ->where($and)
335
            ->groupBy('p.id');
336
 
337
        if($user){
338
            $qb->setParameter('user', $user);
339
        } else {
340
            $qb->setParameter('user', null);
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
            case 'id':
357
                $qb->orderBy('p.createdAt', 'ASC');
358
                break;
359
            default:
360
                $qb->orderBy('p.createdAt', 'ASC');
361
                break;
362
        }
363
        
364
        $query = $qb->getQuery();
365
        
366
        $posts = $query->getResult();
367
        $arrayPosts = array();
368
        $i=0;
369
        foreach ($posts as $postRaw) {
370
            $data = array();
371
            $post = $postRaw[0];
372
            if ($post) {
373
                foreach ($post->getPostElements() as $element) {
374
                    $data[$element->getPosition()] = $element->getValue();
375
                }
376
                $arrayPosts[$i]['post']  = $post;
377
                $arrayPosts[$i]['data']  = $data;
378
                $arrayPosts[$i]['votes'] = count($post->getVotes());
379
                $arrayPosts[$i]['voted'] = $postRaw['voted'];
380
                $arrayPosts[$i]['id']    = $post->getId();
381
                $arrayPosts[$i]['user']  = $post->getUser();
382
                $arrayPosts[$i]['createdAt']  = $post->getCreatedAt();
383
                $i++;
384
            }
385
        }
386
387
        return $arrayPosts;
388
    }
389
390
    public function addVote($user, $ipAddress, $post)
391
    {
392
        $postvoteVoteMapper = $this->getPostVoteVoteMapper();
393
        $postId = $post->getId();
394
395
        if ($user) {
396
            $entryUser = count($postvoteVoteMapper->findBy(array('user' => $user, 'post' =>$postId)));
397
        } else {
398
            $entryUser =count($postvoteVoteMapper->findBy(array('ip' => $ipAddress, 'post' =>$postId)));
399
        }
400
        if ($entryUser && $entryUser > 0) {
401
402
            return false;
403
        } else {
404
            $vote = new \PlaygroundGame\Entity\PostVoteVote();
405
            $vote->setPost($post);
406
            $vote->setIp($ipAddress);
407
            $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...
408
            $vote->setPostvote($post->getPostvote());
409
            if ($user) {
410
                $vote->setUser($user);
411
            }
412
413
            $postvoteVoteMapper->insert($vote);
414
            $game = $post->getPostvote();
415
            $this->getEventManager()->trigger(
416
                __FUNCTION__ .'.post',
417
                $this,
418
                array('user' => $user, 'game' => $game, 'post' => $post, 'vote' => $vote)
419
            );
420
421
            return true;
422
        }
423
    }
424
425
    public function addComment($user, $ipAddress, $post, $message = '')
426
    {
427
        $postvoteCommentMapper = $this->getPostVoteCommentMapper();
428
        $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...
429
        $game = $post->getPostvote();
430
        $comment = new \PlaygroundGame\Entity\PostVoteComment();
431
        $comment->setPost($post);
432
        $comment->setIp($ipAddress);
433
        $message = strip_tags($message);
434
        $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...
435
        $comment->setPostvote($game);
436
        if ($user) {
437
            $comment->setUser($user);
438
        }
439
440
        $postvoteCommentMapper->insert($comment);
441
        
442
        $this->getEventManager()->trigger(
443
            __FUNCTION__ .'.post',
444
            $this,
445
            array('user' => $user, 'game' => $game, 'post' => $post, 'comment' => $comment)
446
        );
447
448
        return true;
449
    }
450
451
452
    public function addShare($post)
453
    {
454
        $postvotePostMapper = $this->getPostVotePostMapper();
455
        $post->setShares($post->getShares()+1);
456
        $postvotePostMapper->update($post);
457
458
        $this->getEventManager()->trigger(__FUNCTION__ . '.post', $this, array(
459
            'post' => $post
460
        ));
461
462
        return true;
463
    }
464
    /**
465
     * Get all comments for this game
466
     */
467
    public function getCommentsForPostvote($postvote)
468
    {
469
        $postvoteCommentMapper = $this->getPostVoteCommentMapper();
470
        $comments = $postvoteCommentMapper->findBy(array('postvote' => $postvote), array('createdAt' => 'DESC'));
471
472
        return $comments ;
473
    }
474
475
    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...
476
    {
477
        $postvoteCommentMapper = $this->getPostVoteCommentMapper();
478
        $comment = $postvoteCommentMapper->findOneBy(array('id' => $messageId));
479
        if($comment->getUser()->getId() === $user->getId()){
480
            $postvoteCommentMapper->remove($comment);
481
            $this->getEventManager()->trigger(
482
                'remove_comment_postvote.post',
483
                $this,
484
                array('user' => $user,'comment' => $comment)
485
            );
486
487
            return true;
488
        }
489
490
        return false;
491
    }
492
493
    public function getEntriesHeader($game)
494
    {
495
        $header = parent::getEntriesHeader($game);
496
        if ($game->getForm()) {
497
            $form = json_decode($game->getForm()->getForm(), true);
498 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...
499
                foreach ($element as $k => $v) {
500
                    if ($k !== 'form_properties') {
501
                        $header[$v[0]['name']] = 1;
502
                    }
503
                }
504
            }
505
        }
506
507
        return $header;
508
    }
509
510 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...
511
    {
512
        $em = $this->getServiceManager()->get('doctrine.entitymanager.orm_default');
513
514
        $qb = $em->createQueryBuilder();
515
        $qb->select('
516
            p.id,
517
            u.username,
518
            u.title,
519
            u.firstname,
520
            u.lastname,
521
            u.email,
522
            u.optin,
523
            u.optinPartner,
524
            u.address,
525
            u.address2,
526
            u.postalCode,
527
            u.city,
528
            u.telephone,
529
            u.mobile,
530
            u.created_at,
531
            u.dob,
532
            e.winner,
533
            e.socialShares,
534
            e.playerData,
535
            e.updated_at,
536
            p.status,
537
            p
538
            ')
539
            ->from('PlaygroundGame\Entity\PostVotePost', 'p')
540
            ->innerJoin('p.entry', 'e')
541
            ->leftJoin('p.user', 'u')
542
            ->where($qb->expr()->eq('e.game', ':game'));
543
        
544
        $qb->setParameter('game', $game);
545
546
        return $qb->getQuery();
547
    }
548
549
    /**
550
    * getGameEntries : All entries of a game
551
    *
552
    * @return Array of PlaygroundGame\Entity\Game
553
    */
554
    public function getGameEntries($header, $entries, $game)
555
    {
556
        $results = array();
557
558
        foreach ($entries as $k => $entry) {
559
            $entryData = json_decode($entry['playerData'], true);
560
            $postElements = $entry[0]->getPostElements();
561
562
            foreach ($header as $key => $v) {
563
                if (isset($entryData[$key]) && $key !=='id') {
564
                    $results[$k][$key] = (is_array($entryData[$key]))?implode(', ', $entryData[$key]):$entryData[$key];
565
                } elseif (array_key_exists($key, $entry)) {
566
                    $results[$k][$key] = ($entry[$key] instanceof \DateTime)?$entry[$key]->format('Y-m-d'):$entry[$key];
567
                } else {
568
                    $results[$k][$key] = '';
569
                }
570
571
                foreach ($postElements as $e) {
572
                    if ($key === $e->getName()) {
573
                        $results[$k][$key] = (is_array($e->getValue()))?implode(', ', $e->getValue()):$e->getValue();
574
                        break;
575
                    }
576
                }
577
            }
578
        }
579
580
        return $results;
581
    }
582
583
    public function getPostVoteFormMapper()
584
    {
585
        if (null === $this->postvoteformMapper) {
586
            $this->postvoteformMapper = $this->getServiceManager()->get('playgroundgame_postvoteform_mapper');
587
        }
588
589
        return $this->postvoteformMapper;
590
    }
591
592
    public function setPostVoteFormMapper($postvoteformMapper)
593
    {
594
        $this->postvoteformMapper = $postvoteformMapper;
595
596
        return $this;
597
    }
598
599
    public function getPostVotePostElementMapper()
600
    {
601
        if (null === $this->postVotePostElementMapper) {
602
            $this->postVotePostElementMapper = $this->getServiceManager()->get(
603
                'playgroundgame_postvotepostelement_mapper'
604
            );
605
        }
606
607
        return $this->postVotePostElementMapper;
608
    }
609
610
    public function setPostVotePostElementMapper($postVotePostElementMapper)
611
    {
612
        $this->postVotePostElementMapper = $postVotePostElementMapper;
613
614
        return $this;
615
    }
616
617
    public function getPostVoteVoteMapper()
618
    {
619
        if (null === $this->postVoteVoteMapper) {
620
            $this->postVoteVoteMapper = $this->getServiceManager()->get('playgroundgame_postvotevote_mapper');
621
        }
622
623
        return $this->postVoteVoteMapper;
624
    }
625
626
    public function setPostVoteVoteMapper($postVoteVoteMapper)
627
    {
628
        $this->postVoteVoteMapper = $postVoteVoteMapper;
629
630
        return $this;
631
    }
632
633
    public function getPostVoteCommentMapper()
634
    {
635
        if (null === $this->postVoteCommentMapper) {
636
            $this->postVoteCommentMapper = $this->getServiceManager()->get('playgroundgame_postvotecomment_mapper');
637
        }
638
639
        return $this->postVoteCommentMapper;
640
    }
641
642
    public function setPostVoteCommentMapper($postVoteCommentMapper)
643
    {
644
        $this->postVoteCommentMapper = $postVoteCommentMapper;
645
646
        return $this;
647
    }
648
649
    public function getPostVotePostMapper()
650
    {
651
        if (null === $this->postVotePostMapper) {
652
            $this->postVotePostMapper = $this->getServiceManager()->get('playgroundgame_postvotepost_mapper');
653
        }
654
655
        return $this->postVotePostMapper;
656
    }
657
658
    public function setPostVotePostMapper($postVotePostMapper)
659
    {
660
        $this->postVotePostMapper = $postVotePostMapper;
661
662
        return $this;
663
    }
664
665
    public function getPostVoteMapper()
666
    {
667
        if (null === $this->postvoteMapper) {
668
            $this->postvoteMapper = $this->getServiceManager()->get('playgroundgame_postvote_mapper');
669
        }
670
671
        return $this->postvoteMapper;
672
    }
673
674
    /**
675
     * setQuizQuestionMapper
676
     *
677
     * @return PostVote
678
     */
679
    public function setPostVoteMapper($postvoteMapper)
680
    {
681
        $this->postvoteMapper = $postvoteMapper;
682
683
        return $this;
684
    }
685
}
686