Completed
Push — develop ( 891625...cca290 )
by greg
03:03 queued 10s
created

PostVote::getGameEntries()   C

Complexity

Conditions 12
Paths 34

Size

Total Lines 33

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 33
rs 6.9666
c 0
b 0
f 0
cc 12
nc 34
nop 3

How to fix   Complexity   

Long Method

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

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

Commonly applied refactorings include:

1
<?php
2
3
namespace PlaygroundGame\Service;
4
5
use Zend\Stdlib\ErrorHandler;
6
7
class PostVote extends Game
8
{
9
    protected $postvoteMapper;
10
    protected $postvoteformMapper;
11
    protected $postVotePostMapper;
12
    protected $postVoteVoteMapper;
13
    protected $postVoteCommentMapper;
14
    protected $postVotePostElementMapper;
15
16
    public function getGameEntity()
17
    {
18
        return new \PlaygroundGame\Entity\PostVote;
19
    }
20
21 View Code Duplication
    public function getPath($post)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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

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

Loading history...
22
    {
23
        $path = $this->getOptions()->getMediaPath() . DIRECTORY_SEPARATOR;
24
        $path .= 'game' . $post->getPostVote()->getId() . DIRECTORY_SEPARATOR;
25
        if (!is_dir($path)) {
26
            mkdir($path, 0777, true);
27
        }
28
        $path .= 'post'. $post->getId() . DIRECTORY_SEPARATOR;
29
        if (!is_dir($path)) {
30
            mkdir($path, 0777, true);
31
        }
32
33
        return $path;
34
    }
35
36 View Code Duplication
    public function getMediaUrl($post)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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

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

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

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

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

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

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

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

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

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

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

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

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

Let’s take a look at an example:

class Author {
    private $name;

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

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

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

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

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

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

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

Loading history...
135
        }
136
137
        $post = $this->checkPost($entry);
138
        $path = $this->getPath($post);
139
        $media_url = $this->getMediaUrl($post);
140
        $position=1;
141
142
        foreach ($data as $name => $value) {
143
            $postElement = $postVotePostElementMapper->findOneBy(array('post' => $post, 'name' => $name));
144
            if (! $postElement) {
145
                $postElement = new \PlaygroundGame\Entity\PostVotePostElement();
146
            }
147
            $postElement->setName($name);
148
            $postElement->setPosition($position);
149
150
            if (is_array($value) && isset($value['tmp_name'])) {
151
                // The file upload has been done in ajax but some weird bugs remain without it
152
153
                if (! $value['error']) {
154
                    ErrorHandler::start();
155
                    $value['name'] = $this->fileNewname($path, $value['name'], true);
156
                    move_uploaded_file($value['tmp_name'], $path . $value['name']);
157
158
                    if (getimagesize($path . $value['name'])) {
159
                        $image = $this->serviceLocator->get('playgroundcore_image_service');
160
                        $image->setImage($path . $value['name']);
161
162
                        if ($image->canCorrectOrientation()) {
163
                            $image->correctOrientation()->save();
164
                        }
165
                        $postElement->setValue($media_url . $value['name']);
166
                        
167
                        if (class_exists("Imagick")) {
168
                            $ext = pathinfo($value['name'], PATHINFO_EXTENSION);
169
                            $img = new \Imagick($path . $value['name']);
170
                            $img->cropThumbnailImage(100, 100);
171
                            $img->setImageCompression(\Imagick::COMPRESSION_JPEG);
172
                            $img->setImageCompressionQuality(75);
173
                            // Strip out unneeded meta data
174
                            $img->stripImage();
175
                            $img->writeImage($path . str_replace('.'.$ext, '-thumbnail.'.$ext, $value['name']));
176
                            ErrorHandler::stop(true);
177
                        }
178
                    } else {
179
                        $postElement->setValue($media_url . $value['name']);
180
                    }
181
                }
182
            } elseif (is_array($value) || $form->get($name) instanceof \Zend\Form\Element\Select) {
183
                $arValues = $form->get($name)->getValueOptions();
184
                $postElement->setValue($arValues[$value[0]]);
185
            } elseif (!empty($value)) {
186
                $postElement->setValue($value);
187
            }
188
            $post->addPostElement($postElement);
189
            // $postElement->setPost($post);
190
            // $postVotePostElementMapper->insert($postElement);
191
            $position++;
192
        }
193
194
        $postvotePostMapper->update($post);
195
196
        // If a preview step is not proposed, I confirmPost on this step
197
        $steps = $game->getStepsArray();
198
        $previewKey = array_search('preview', $steps);
199
        if (!$previewKey) {
200
            $post = $this->confirmPost($game, $user);
201
        }
202
203
        $this->getEventManager()->trigger(
204
            __FUNCTION__ . '.post',
205
            $this,
206
            [
207
                'user' => $user,
208
                'game' => $game,
209
                'post' => $post,
210
                'entry' => $entry,
211
            ]
212
        );
213
214
        return $post;
215
    }
216
217
    /**
218
     *
219
     * @return \PlaygroundGame\Entity\Game
220
     */
221
    public function confirmPost($game, $user)
222
    {
223
        $postvotePostMapper = $this->getPostVotePostMapper();
224
225
        $entryMapper = $this->getEntryMapper();
226
        $entry = $this->findLastActiveEntry($game, $user);
227
228
        if (!$entry) {
229
            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...
230
        }
231
232
        $post = $postvotePostMapper->findOneBy(array('entry' => $entry));
233
234
        if (! $post) {
235
            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...
236
        }
237
238
        // The post is confirmed by user. I update the status and close the associated entry
239
        // Post are validated by default, unless pre-moderation is enable for the game
240
        if ($game->getModerationType()) {
241
            $post->setStatus(1);
242
        } else {
243
            $post->setStatus(2);
244
        }
245
246
        $post = $postvotePostMapper->update($post);
247
248
        $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...
249
        $entryMapper->update($entry);
250
251
        $this->getEventManager()->trigger(
252
            __FUNCTION__ . '.post',
253
            $this,
254
            [
255
                'user' => $user,
256
                'game' => $game,
257
                'entry' => $entry,
258
                'post' => $post,
259
            ]
260
        );
261
262
        return $post;
263
    }
264
265
    /**
266
     *
267
     * This service is ready for all types of games
268
     *
269
     * @param  array                  $data
270
     * @return \PlaygroundGame\Entity\Game
271
     */
272 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...
273
    {
274
        $title ='';
275
        $description = '';
276
277
        if ($data['form_jsonified']) {
278
            $jsonPV = json_decode($data['form_jsonified']);
279
            foreach ($jsonPV as $element) {
280
                if ($element->form_properties) {
281
                    $attributes  = $element->form_properties[0];
282
                    $title       = $attributes->title;
283
                    $description = $attributes->description;
284
285
                    break;
286
                }
287
            }
288
        }
289
        if (!$form) {
290
            $form = new \PlaygroundGame\Entity\PostVoteForm();
291
        }
292
        $form->setPostvote($game);
293
        $form->setTitle($title);
294
        $form->setDescription($description);
295
        $form->setForm($data['form_jsonified']);
296
        $form->setFormTemplate($data['form_template']);
297
298
        $form = $this->getPostVoteFormMapper()->insert($form);
299
300
        return $form;
301
    }
302
303
    public function findArrayOfValidatedPosts($game, $user, $filter, $search = '')
304
    {
305
        $em = $this->serviceLocator->get('doctrine.entitymanager.orm_default');
306
        $qb = $em->createQueryBuilder();
307
        $and = $qb->expr()->andx();
308
        
309
        $and->add($qb->expr()->eq('p.status', 2));
310
311
        $and->add($qb->expr()->eq('g.id', ':game'));
312
        $qb->setParameter('game', $game);
313
        
314
        if ($search != '') {
315
            $and->add(
316
                $qb->expr()->orX(
317
                    $qb->expr()->like('u.username', $qb->expr()->literal('%:search%')),
318
                    $qb->expr()->like('u.firstname', $qb->expr()->literal('%:search%')),
319
                    $qb->expr()->like('u.lastname', $qb->expr()->literal('%:search%')),
320
                    $qb->expr()->like('e.value', $qb->expr()->literal('%:search%')),
321
                    $qb->expr()->isNull('g.publicationDate')
322
                )
323
            );
324
            $qb->setParameter('search', $search);
325
        }
326
        
327
        if ('push' == $filter) {
328
            $and->add(
329
                $qb->expr()->andX(
330
                    $qb->expr()->eq('p.pushed', 1)
331
                )
332
            );
333
        }
334
        
335
        $qb->select('p, COUNT(DISTINCT v) AS votesCount, COUNT(distinct av) AS voted')
336
            ->from('PlaygroundGame\Entity\PostVotePost', 'p')
337
            ->innerJoin('p.postvote', 'g')
338
            ->leftJoin('p.user', 'u')
339
            ->innerJoin('p.postElements', 'e')
340
            ->leftJoin('p.votes', 'v')
341
            ->leftJoin('p.votes', 'av', 'WITH', 'av.user = :user')
342
            ->where($and)
343
            ->groupBy('p.id');
344
 
345
        if ($user) {
346
            $qb->setParameter('user', $user);
347
        } else {
348
            $qb->setParameter('user', null);
349
        }
350
351
        switch ($filter) {
352
            case 'random':
353
                $qb->orderBy('e.value', 'ASC');
354
                break;
355
            case 'vote':
356
                $qb->orderBy('votesCount', 'DESC');
357
                break;
358
            case 'date':
359
                $qb->orderBy('p.createdAt', 'DESC');
360
                break;
361
            case 'push':
362
                $qb->orderBy('p.createdAt', 'DESC');
363
                break;
364
            case 'id':
365
                $qb->orderBy('p.createdAt', 'ASC');
366
                break;
367
            default:
368
                $qb->orderBy('p.createdAt', 'ASC');
369
                break;
370
        }
371
        
372
        $query = $qb->getQuery();
373
        
374
        $posts = $query->getResult();
375
        $arrayPosts = array();
376
        $i=0;
377
        foreach ($posts as $postRaw) {
378
            $data = array();
379
            $post = $postRaw[0];
380
            if ($post) {
381
                foreach ($post->getPostElements() as $element) {
382
                    $data[$element->getPosition()] = $element->getValue();
383
                }
384
                $arrayPosts[$i]['post']  = $post;
385
                $arrayPosts[$i]['data']  = $data;
386
                $arrayPosts[$i]['votes'] = count($post->getVotes());
387
                $arrayPosts[$i]['voted'] = $postRaw['voted'];
388
                $arrayPosts[$i]['id']    = $post->getId();
389
                $arrayPosts[$i]['user']  = $post->getUser();
390
                $arrayPosts[$i]['createdAt']  = $post->getCreatedAt();
391
                $i++;
392
            }
393
        }
394
395
        return $arrayPosts;
396
    }
397
398 View Code Duplication
    public function toggleVote($user, $ipAddress, $post, $comment = 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...
399
    {
400
        $postvoteVoteMapper = $this->getPostVoteVoteMapper();
401
        $postId = $post->getId();
402
        $commentId = ($comment !== null) ? $comment->getId() : null;
403
        $vote = null;
0 ignored issues
show
Unused Code introduced by
$vote 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...
404
        $game = $post->getPostvote();
405
406
        if ($user) {
407
            if ($comment == null) {
408
                $entryUser = count($postvoteVoteMapper->findBy(array('user' => $user, 'post' => $postId)));
409
                $vote = $postvoteVoteMapper->findOneBy(array('user' => $user, 'post' => $postId));
410
            } else {
411
                $entryUser = count($postvoteVoteMapper->findBy(array('user' => $user, 'post' => $postId, 'postComment' => $commentId)));
412
                $vote = $postvoteVoteMapper->findOneBy(array('user' => $user, 'post' => $postId, 'postComment' => $commentId));
413
            }
414
        } else {
415
            if ($comment == null) {
416
                $entryUser = count($postvoteVoteMapper->findBy(array('ip' => $ipAddress, 'post' => $postId)));
417
                $vote = $postvoteVoteMapper->findOneBy(array('ip' => $ipAddress, 'post' => $postId));
418
            } else {
419
                $entryUser = count($postvoteVoteMapper->findBy(array('ip' => $ipAddress, 'post' => $postId, 'postComment' => $commentId)));
420
                $vote = $postvoteVoteMapper->findOneBy(array('ip' => $ipAddress, 'post' => $postId, 'postComment' => $commentId));
421
            }
422
        }
423
424
        if ($entryUser && $entryUser > 0) {
425
            $postvoteVoteMapper->remove($vote);
426
427
            return 0;
428
        } else {
429
            $vote = new \PlaygroundGame\Entity\PostVoteVote();
430
            $vote->setPost($post);
431
            $vote->setIp($ipAddress);
432
            $vote->setNote(1);
433
            // If the vote is for a comment
434
            if ($comment != null) {
435
                $vote->setPostComment($comment);
436
                $vote->setPostvote($post->getPostvote());
437
            // else if the vote is for the post itself
438
            } else {
439
                $vote->setPostvote($post->getPostvote(), true);
440
            }
441
442
            if ($user) {
443
                $vote->setUser($user);
444
            }
445
446
            $postvoteVoteMapper->insert($vote);
447
        }
448
449
        $this->getEventManager()->trigger(
450
            __FUNCTION__ .'.post',
451
            $this,
452
            array('user' => $user, 'game' => $game, 'post' => $post, 'vote' => $vote)
453
        );
454
455
        return 1;
456
    }
457
458
    public function removeVote($user, $ipAddress, $post)
459
    {
460
        $postvoteVoteMapper = $this->getPostVoteVoteMapper();
461
        $postId = $post->getId();
462
        $commentId = ($comment !== null) ? $comment->getId() : null;
0 ignored issues
show
Bug introduced by
The variable $comment does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
463
        $vote = null;
0 ignored issues
show
Unused Code introduced by
$vote 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...
464
        $game = $post->getPostvote();
465
466
        if ($user) {
467
            if ($comment == null) {
468
                $entryUser = count($postvoteVoteMapper->findBy(array('user' => $user, 'post' => $postId)));
0 ignored issues
show
Unused Code introduced by
$entryUser 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...
469
                $vote = $postvoteVoteMapper->findOneBy(array('user' => $user, 'post' => $postId));
470
            } else {
471
                $entryUser = count($postvoteVoteMapper->findBy(array('user' => $user, 'post' => $postId, 'postComment' => $commentId)));
0 ignored issues
show
Unused Code introduced by
$entryUser 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...
472
                $vote = $postvoteVoteMapper->findOneBy(array('user' => $user, 'post' => $postId, 'postComment' => $commentId));
473
            }
474
        } else {
475
            if ($comment == null) {
476
                $entryUser = count($postvoteVoteMapper->findBy(array('ip' => $ipAddress, 'post' => $postId)));
0 ignored issues
show
Unused Code introduced by
$entryUser 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...
477
                $vote = $postvoteVoteMapper->findOneBy(array('ip' => $ipAddress, 'post' => $postId));
478
            } else {
479
                $entryUser = count($postvoteVoteMapper->findBy(array('ip' => $ipAddress, 'post' => $postId, 'postComment' => $commentId)));
0 ignored issues
show
Unused Code introduced by
$entryUser 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...
480
                $vote = $postvoteVoteMapper->findOneBy(array('ip' => $ipAddress, 'post' => $postId, 'postComment' => $commentId));
481
            }
482
        }
483
484
        $this->getEventManager()->trigger(
485
            __FUNCTION__ .'.post',
486
            $this,
487
            array('user' => $user, 'game' => $game, 'post' => $post, 'vote' => $vote)
488
        );
489
490
        return true;
491
    }
492
493 View Code Duplication
    public function addVote($user, $ipAddress, $post)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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

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

Loading history...
494
    {
495
        $postvoteVoteMapper = $this->getPostVoteVoteMapper();
496
        $postId = $post->getId();
497
        $commentId = ($comment !== null) ? $comment->getId() : null;
0 ignored issues
show
Bug introduced by
The variable $comment does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
498
        $vote = null;
0 ignored issues
show
Unused Code introduced by
$vote 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...
499
        $game = $post->getPostvote();
0 ignored issues
show
Unused Code introduced by
$game 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...
500
501
        if ($user) {
502
            if ($comment == null) {
503
                $entryUser = count($postvoteVoteMapper->findBy(array('user' => $user, 'post' => $postId)));
504
                $vote = $postvoteVoteMapper->findOneBy(array('user' => $user, 'post' => $postId));
0 ignored issues
show
Unused Code introduced by
$vote 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...
505
            } else {
506
                $entryUser = count($postvoteVoteMapper->findBy(array('user' => $user, 'post' => $postId, 'postComment' => $commentId)));
507
                $vote = $postvoteVoteMapper->findOneBy(array('user' => $user, 'post' => $postId, 'postComment' => $commentId));
0 ignored issues
show
Unused Code introduced by
$vote 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...
508
            }
509
        } else {
510
            if ($comment == null) {
511
                $entryUser = count($postvoteVoteMapper->findBy(array('ip' => $ipAddress, 'post' => $postId)));
512
                $vote = $postvoteVoteMapper->findOneBy(array('ip' => $ipAddress, 'post' => $postId));
0 ignored issues
show
Unused Code introduced by
$vote 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...
513
            } else {
514
                $entryUser = count($postvoteVoteMapper->findBy(array('ip' => $ipAddress, 'post' => $postId, 'postComment' => $commentId)));
515
                $vote = $postvoteVoteMapper->findOneBy(array('ip' => $ipAddress, 'post' => $postId, 'postComment' => $commentId));
0 ignored issues
show
Unused Code introduced by
$vote 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...
516
            }
517
        }
518
519
        if ($entryUser && $entryUser > 0) {
520
            return false;
521
        } else {
522
            $vote = new \PlaygroundGame\Entity\PostVoteVote();
523
            $vote->setPost($post);
524
            $vote->setIp($ipAddress);
525
            $vote->setNote(1);
526
            // If the vote is for a comment
527
            if ($comment != null) {
528
                $vote->setPostComment($comment);
529
                $vote->setPostvote($post->getPostvote());
530
            // else if the vote is for the post itself
531
            } else {
532
                $vote->setPostvote($post->getPostvote(), true);
533
            }
534
            if ($user) {
535
                $vote->setUser($user);
536
            }
537
538
            $postvoteVoteMapper->insert($vote);
539
            $game = $post->getPostvote();
540
            $this->getEventManager()->trigger(
541
                __FUNCTION__ .'.post',
542
                $this,
543
                array('user' => $user, 'game' => $game, 'post' => $post, 'vote' => $vote)
544
            );
545
546
            return true;
547
        }
548
    }
549
550
    public function addComment($user, $ipAddress, $post, $message = '', $category = null)
551
    {
552
        $postvoteCommentMapper = $this->getPostVoteCommentMapper();
553
        $game = $post->getPostvote();
554
        $comment = new \PlaygroundGame\Entity\PostVoteComment();
555
        $comment->setPost($post);
556
        $comment->setIp($ipAddress);
557
        $message = strip_tags($message);
558
        $comment->setMessage($message);
559
        if ($category !== null) {
560
            $comment->setCategory($category);
561
        }
562
        $comment->setPostvote($game);
563
        if ($user) {
564
            $comment->setUser($user);
565
        }
566
567
        $postvoteCommentMapper->insert($comment);
568
        
569
        $this->getEventManager()->trigger(
570
            __FUNCTION__ .'.post',
571
            $this,
572
            array('user' => $user, 'game' => $game, 'post' => $post, 'comment' => $comment)
573
        );
574
575
        return true;
576
    }
577
578
579
    public function addShare($post)
580
    {
581
        $postvotePostMapper = $this->getPostVotePostMapper();
582
        $post->setShares($post->getShares()+1);
583
        $postvotePostMapper->update($post);
584
585
        $this->getEventManager()->trigger(__FUNCTION__ . '.post', $this, array(
586
            'post' => $post
587
        ));
588
589
        return true;
590
    }
591
    /**
592
     * Get all comments for this game
593
     */
594
    public function getCommentsForPostvote($postvote)
595
    {
596
        $postvoteCommentMapper = $this->getPostVoteCommentMapper();
597
        $comments = $postvoteCommentMapper->findBy(array('postvote' => $postvote), array('createdAt' => 'DESC'));
598
599
        return $comments ;
600
    }
601
602
    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...
603
    {
604
        $postvoteCommentMapper = $this->getPostVoteCommentMapper();
605
        $comment = $postvoteCommentMapper->findOneBy(array('id' => $messageId));
606
        if ($comment->getUser()->getId() === $user->getId()) {
607
            $postvoteCommentMapper->remove($comment);
608
            $this->getEventManager()->trigger(
609
                'remove_comment_postvote.post',
610
                $this,
611
                array('user' => $user, 'comment' => $comment)
612
            );
613
614
            return true;
615
        }
616
617
        return false;
618
    }
619
620
    public function getEntriesHeader($game)
621
    {
622
        $header = parent::getEntriesHeader($game);
623
        if ($game->getForm()) {
624
            $form = json_decode($game->getForm()->getForm(), true);
625 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...
626
                foreach ($element as $k => $v) {
627
                    if ($k !== 'form_properties') {
628
                        $header[$v[0]['name']] = 1;
629
                    }
630
                }
631
            }
632
        }
633
        if ($game->getVoteActive()) {
634
            $header['votes'] = 1;
635
        }
636
637
        return $header;
638
    }
639
640 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...
641
    {
642
        $em = $this->serviceLocator->get('doctrine.entitymanager.orm_default');
643
644
        $qb = $em->createQueryBuilder();
645
        $qb->select('
646
            p.id,
647
            u.username,
648
            u.title,
649
            u.firstname,
650
            u.lastname,
651
            u.displayName,
652
            u.email,
653
            u.optin,
654
            u.optinPartner,
655
            u.address,
656
            u.address2,
657
            u.postalCode,
658
            u.city,
659
            u.telephone,
660
            u.mobile,
661
            u.created_at,
662
            u.dob,
663
            e.winner,
664
            e.socialShares,
665
            e.playerData,
666
            e.updated_at,
667
            p.status,
668
            p
669
            ')
670
            ->from('PlaygroundGame\Entity\PostVotePost', 'p')
671
            ->innerJoin('p.entry', 'e')
672
            ->leftJoin('p.user', 'u')
673
            ->where($qb->expr()->eq('e.game', ':game'));
674
        
675
        $qb->setParameter('game', $game);
676
677
        return $qb->getQuery();
678
    }
679
680
    /**
681
    * getGameEntries : All entries of a game
682
    *
683
    * @return Array of PlaygroundGame\Entity\Game
684
    */
685
    public function getGameEntries($header, $entries, $game)
686
    {
687
        $results = array();
688
689
        foreach ($entries as $k => $entry) {
690
            $entryData = json_decode($entry['playerData'], true);
691
            $postElements = $entry[0]->getPostElements();
692
693
            foreach ($header as $key => $v) {
694
                if (isset($entryData[$key]) && $key !=='id') {
695
                    $results[$k][$key] = (is_array($entryData[$key]))?implode(', ', $entryData[$key]):$entryData[$key];
696
                } elseif (array_key_exists($key, $entry)) {
697
                    $results[$k][$key] = ($entry[$key] instanceof \DateTime)?$entry[$key]->format('Y-m-d'):$entry[$key];
698
                } else {
699
                    $results[$k][$key] = '';
700
                }
701
702
                foreach ($postElements as $e) {
703
                    if ($key === $e->getName()) {
704
                        $results[$k][$key] = (is_array($e->getValue()))?implode(', ', $e->getValue()):$e->getValue();
705
                        break;
706
                    }
707
                }
708
709
                if ($key === 'votes') {
710
                    $results[$k][$key] = count($entry[0]->getVotes());
711
                    break;
712
                }
713
            }
714
        }
715
716
        return $results;
717
    }
718
719
    public function getPostVoteFormMapper()
720
    {
721
        if (null === $this->postvoteformMapper) {
722
            $this->postvoteformMapper = $this->serviceLocator->get('playgroundgame_postvoteform_mapper');
723
        }
724
725
        return $this->postvoteformMapper;
726
    }
727
728
    public function setPostVoteFormMapper($postvoteformMapper)
729
    {
730
        $this->postvoteformMapper = $postvoteformMapper;
731
732
        return $this;
733
    }
734
735
    public function getPostVotePostElementMapper()
736
    {
737
        if (null === $this->postVotePostElementMapper) {
738
            $this->postVotePostElementMapper = $this->serviceLocator->get(
739
                'playgroundgame_postvotepostelement_mapper'
740
            );
741
        }
742
743
        return $this->postVotePostElementMapper;
744
    }
745
746
    public function setPostVotePostElementMapper($postVotePostElementMapper)
747
    {
748
        $this->postVotePostElementMapper = $postVotePostElementMapper;
749
750
        return $this;
751
    }
752
753
    public function getPostVoteVoteMapper()
754
    {
755
        if (null === $this->postVoteVoteMapper) {
756
            $this->postVoteVoteMapper = $this->serviceLocator->get('playgroundgame_postvotevote_mapper');
757
        }
758
759
        return $this->postVoteVoteMapper;
760
    }
761
762
    public function setPostVoteVoteMapper($postVoteVoteMapper)
763
    {
764
        $this->postVoteVoteMapper = $postVoteVoteMapper;
765
766
        return $this;
767
    }
768
769
    public function getPostVoteCommentMapper()
770
    {
771
        if (null === $this->postVoteCommentMapper) {
772
            $this->postVoteCommentMapper = $this->serviceLocator->get('playgroundgame_postvotecomment_mapper');
773
        }
774
775
        return $this->postVoteCommentMapper;
776
    }
777
778
    public function setPostVoteCommentMapper($postVoteCommentMapper)
779
    {
780
        $this->postVoteCommentMapper = $postVoteCommentMapper;
781
782
        return $this;
783
    }
784
785
    public function getPostVotePostMapper()
786
    {
787
        if (null === $this->postVotePostMapper) {
788
            $this->postVotePostMapper = $this->serviceLocator->get('playgroundgame_postvotepost_mapper');
789
        }
790
791
        return $this->postVotePostMapper;
792
    }
793
794
    public function setPostVotePostMapper($postVotePostMapper)
795
    {
796
        $this->postVotePostMapper = $postVotePostMapper;
797
798
        return $this;
799
    }
800
801
    public function getPostVoteMapper()
802
    {
803
        if (null === $this->postvoteMapper) {
804
            $this->postvoteMapper = $this->serviceLocator->get('playgroundgame_postvote_mapper');
805
        }
806
807
        return $this->postvoteMapper;
808
    }
809
810
    /**
811
     * setQuizQuestionMapper
812
     *
813
     * @return PostVote
814
     */
815
    public function setPostVoteMapper($postvoteMapper)
816
    {
817
        $this->postvoteMapper = $postvoteMapper;
818
819
        return $this;
820
    }
821
}
822