Completed
Push — master ( 29f109...a9aba4 )
by greg
07:13 queued 03:33
created

PostVote::removeComment()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

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