Completed
Push — master ( 40aeb3...89a717 )
by greg
10:53 queued 08:07
created

PostVote::getEntriesHeader()   B

Complexity

Conditions 6
Paths 4

Size

Total Lines 22

Duplication

Lines 7
Ratio 31.82 %

Importance

Changes 0
Metric Value
dl 7
loc 22
rs 8.9457
c 0
b 0
f 0
cc 6
nc 4
nop 1
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
    protected $postVoteShareMapper;
16
    protected $postVoteViewMapper;
17
18
    public function getGameEntity()
19
    {
20
        return new \PlaygroundGame\Entity\PostVote;
21
    }
22
23
    public function getPath($post)
24
    {
25
        $path = $this->getOptions()->getMediaPath() . DIRECTORY_SEPARATOR;
26
        $path .= 'game' . $post->getPostVote()->getId() . DIRECTORY_SEPARATOR;
27
        if (!is_dir($path)) {
28
            mkdir($path, 0777, true);
29
        }
30
        $path .= 'post'. $post->getId() . DIRECTORY_SEPARATOR;
31
        if (!is_dir($path)) {
32
            mkdir($path, 0777, true);
33
        }
34
35
        return $path;
36
    }
37
38 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...
39
    {
40
        $media_url = $this->getOptions()->getMediaUrl() . '/';
41
        $media_url .= 'game' . $post->getPostVote()->getId() . '/' . 'post'. $post->getId() . '/';
42
43
        return $media_url;
44
    }
45
46
    /**
47
     * @param boolean $entry
48
     */
49
    public function checkPost($entry)
50
    {
51
        $post = $this->getPostVotePostMapper()->findOneBy(array('entry' => $entry));
52
53
        if (! $post) {
54
            $post = new \PlaygroundGame\Entity\PostVotePost();
55
            $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...
56
            $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...
57
            $post->setEntry($entry);
58
            $post = $this->getPostVotePostMapper()->insert($post);
59
        }
60
61
        return $post;
62
    }
63
64
    public function uploadFileToPost($data, $game, $user)
65
    {
66
        $result = false;
67
        $entry = $this->findLastActiveEntry($game, $user);
68
69
        if (!$entry) {
70
            return '0';
71
        }
72
73
        $post = $this->checkPost($entry);
74
        $path = $this->getPath($post);
75
        $media_url = $this->getMediaUrl($post);
76
77
        $key = key($data);
78
        $uploadFile = $this->uploadFile($path, $data[$key]);
79
80
        if ($uploadFile) {
81
            $postElement = $this->getPostVotePostElementMapper()->findOneBy(array('post' => $post, 'name' => $key));
82
            if (! $postElement) {
83
                $postElement = new \PlaygroundGame\Entity\PostVotePostElement();
84
            }
85
            $postElement->setName($key);
86
            $postElement->setPosition(0);
87
            $postElement->setValue($media_url.$uploadFile);
88
            $postElement->setPost($post);
89
            $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...
90
91
            $result = $media_url.$uploadFile;
92
        }
93
94
        return $result;
95
    }
96
97
    public function deleteFilePosted($data, $game, $user)
98
    {
99
        $postvotePostMapper = $this->getPostVotePostMapper();
100
        $postVotePostElementMapper = $this->getPostVotePostElementMapper();
101
102
        $entry = $this->findLastActiveEntry($game, $user);
103
104
        if (!$entry) {
105
            return 'falsefin0';
106
        }
107
108
        $post = $postvotePostMapper->findOneBy(array('entry' => $entry));
109
        $element = $postVotePostElementMapper->findOneBy(array('post' => $post->getId(), 'name' => $data['name']));
110
111
        if ($element) {
112
            $element = $postVotePostElementMapper->remove($element);
113
            if ($element) {
114
                return true;
115
            } else {
116
                return false;
117
            }
118
        } else {
119
            return false;
120
        }
121
    }
122
123
    /**
124
     *
125
     * @param  array $data
126
     * @return \PlaygroundGame\Entity\Game
127
     */
128
    public function createPost(array $data, $game, $user, $form)
129
    {
130
        $postvotePostMapper = $this->getPostVotePostMapper();
131
        $postVotePostElementMapper = $this->getPostVotePostElementMapper();
132
133
        $entry = $this->findLastActiveEntry($game, $user);
134
135
        if (!$entry) {
136
            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...
137
        }
138
139
        $post = $this->checkPost($entry);
140
        $path = $this->getPath($post);
141
        $media_url = $this->getMediaUrl($post);
142
        $position=1;
143
144
        foreach ($data as $name => $value) {
145
            $postElement = $postVotePostElementMapper->findOneBy(array('post' => $post, 'name' => $name));
146
            if (! $postElement) {
147
                $postElement = new \PlaygroundGame\Entity\PostVotePostElement();
148
            }
149
            $postElement->setName($name);
150
            foreach($form as $e) {
151
                if ($e->getName() == $name) {
152
                    $postElement->setLabel($e->getLabel());
153
                    break;
154
                }
155
            }
156
            $postElement->setPosition($position);
157
158
            if (is_array($value) && isset($value['tmp_name'])) {
159
                // The file upload has been done in ajax but some weird bugs remain without it
160
161
                if (! $value['error']) {
162
                    ErrorHandler::start();
163
                    $value['name'] = $this->fileNewname($path, $value['name'], true);
164
                    move_uploaded_file($value['tmp_name'], $path . $value['name']);
165
166
                    if (getimagesize($path . $value['name'])) {
167
                        $image = $this->serviceLocator->get('playgroundcore_image_service');
168
                        $image->setImage($path . $value['name']);
169
170
                        if ($image->canCorrectOrientation()) {
171
                            $image->correctOrientation()->save();
172
                        }
173
                        $postElement->setValue($media_url . $value['name']);
174
                        
175
                        if (class_exists("Imagick")) {
176
                            $ext = pathinfo($value['name'], PATHINFO_EXTENSION);
177
                            $img = new \Imagick($path . $value['name']);
178
                            $img->cropThumbnailImage(100, 100);
179
                            $img->setImageCompression(\Imagick::COMPRESSION_JPEG);
180
                            $img->setImageCompressionQuality(75);
181
                            // Strip out unneeded meta data
182
                            $img->stripImage();
183
                            $img->writeImage($path . str_replace('.'.$ext, '-thumbnail.'.$ext, $value['name']));
184
                            ErrorHandler::stop(true);
185
                        }
186
                    } else {
187
                        $postElement->setValue($media_url . $value['name']);
188
                    }
189
                }
190
            } elseif (is_array($value) || $form->get($name) instanceof \Zend\Form\Element\Select) {
191
                $arValues = $form->get($name)->getValueOptions();
192
                $postElement->setValue($arValues[$value[0]]);
193
            } elseif (!empty($value)) {
194
                $postElement->setValue($value);
195
            }
196
            $post->addPostElement($postElement);
197
            // $postElement->setPost($post);
198
            // $postVotePostElementMapper->insert($postElement);
199
            $position++;
200
        }
201
202
        $postvotePostMapper->update($post);
203
204
        // If a preview step is not proposed, I confirmPost on this step
205
        $steps = $game->getStepsArray();
206
        $previewKey = array_search('preview', $steps);
207
        if (!$previewKey) {
208
            $post = $this->confirmPost($game, $user);
209
        }
210
211
        $this->getEventManager()->trigger(
212
            __FUNCTION__ . '.post',
213
            $this,
214
            [
215
                'user' => $user,
216
                'game' => $game,
217
                'post' => $post,
218
                'entry' => $entry,
219
            ]
220
        );
221
222
        return $post;
223
    }
224
225
    /**
226
     *
227
     * @return \PlaygroundGame\Entity\Game
228
     */
229
    public function confirmPost($game, $user)
230
    {
231
        $postvotePostMapper = $this->getPostVotePostMapper();
232
233
        $entryMapper = $this->getEntryMapper();
234
        $entry = $this->findLastActiveEntry($game, $user);
235
236
        if (!$entry) {
237
            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...
238
        }
239
240
        $post = $postvotePostMapper->findOneBy(array('entry' => $entry));
241
242
        if (! $post) {
243
            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...
244
        }
245
246
        // The post is confirmed by user. I update the status and close the associated entry
247
        // Post are validated by default, unless pre-moderation is enable for the game
248
        if ($game->getModerationType()) {
249
            $post->setStatus(1);
250
        } else {
251
            $post->setStatus(2);
252
        }
253
254
        $post = $postvotePostMapper->update($post);
255
256
        $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...
257
        $entryMapper->update($entry);
258
259
        $this->getEventManager()->trigger(
260
            __FUNCTION__ . '.post',
261
            $this,
262
            [
263
                'user' => $user,
264
                'game' => $game,
265
                'entry' => $entry,
266
                'post' => $post,
267
            ]
268
        );
269
270
        return $post;
271
    }
272
273
    /**
274
     *
275
     * This service is ready for all types of games
276
     *
277
     * @param  array                  $data
278
     * @return \PlaygroundGame\Entity\Game
279
     */
280 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...
281
    {
282
        $title ='';
283
        $description = '';
284
285
        if ($data['form_jsonified']) {
286
            $jsonPV = json_decode($data['form_jsonified']);
287
            foreach ($jsonPV as $element) {
288
                if ($element->form_properties) {
289
                    $attributes  = $element->form_properties[0];
290
                    $title       = $attributes->title;
291
                    $description = $attributes->description;
292
293
                    break;
294
                }
295
            }
296
        }
297
        if (!$form) {
298
            $form = new \PlaygroundGame\Entity\PostVoteForm();
299
        }
300
        $form->setPostvote($game);
301
        $form->setTitle($title);
302
        $form->setDescription($description);
303
        $form->setForm($data['form_jsonified']);
304
        $form->setFormTemplate($data['form_template']);
305
306
        $form = $this->getPostVoteFormMapper()->insert($form);
307
308
        return $form;
309
    }
310
311
    public function findArrayOfValidatedPosts($game, $user, $filter, $search = '')
312
    {
313
        $em = $this->serviceLocator->get('doctrine.entitymanager.orm_default');
314
        $qb = $em->createQueryBuilder();
315
        $and = $qb->expr()->andx();
316
        
317
        $and->add($qb->expr()->eq('p.status', 2));
318
319
        $and->add($qb->expr()->eq('g.id', ':game'));
320
        $qb->setParameter('game', $game);
321
        
322
        if ($search != '') {
323
            $and->add(
324
                $qb->expr()->orX(
325
                    $qb->expr()->like('u.username', $qb->expr()->literal('%:search%')),
326
                    $qb->expr()->like('u.firstname', $qb->expr()->literal('%:search%')),
327
                    $qb->expr()->like('u.lastname', $qb->expr()->literal('%:search%')),
328
                    $qb->expr()->like('e.value', $qb->expr()->literal('%:search%')),
329
                    $qb->expr()->isNull('g.publicationDate')
330
                )
331
            );
332
            $qb->setParameter('search', $search);
333
        }
334
        
335
        if ('push' == $filter) {
336
            $and->add(
337
                $qb->expr()->andX(
338
                    $qb->expr()->eq('p.pushed', 1)
339
                )
340
            );
341
        }
342
        
343
        $qb->select('p, COUNT(DISTINCT v) AS votesCount, COUNT(distinct av) AS voted')
344
            ->from('PlaygroundGame\Entity\PostVotePost', 'p')
345
            ->innerJoin('p.postvote', 'g')
346
            ->leftJoin('p.user', 'u')
347
            ->innerJoin('p.postElements', 'e')
348
            ->leftJoin('p.votes', 'v')
349
            ->leftJoin('p.votes', 'av', 'WITH', 'av.user = :user')
350
            ->where($and)
351
            ->groupBy('p.id');
352
 
353
        if ($user) {
354
            $qb->setParameter('user', $user);
355
        } else {
356
            $qb->setParameter('user', null);
357
        }
358
359
        switch ($filter) {
360
            case 'random':
361
                $qb->orderBy('e.value', 'ASC');
362
                break;
363
            case 'vote':
364
                $qb->orderBy('votesCount', 'DESC');
365
                break;
366
            case 'date':
367
                $qb->orderBy('p.createdAt', 'DESC');
368
                break;
369
            case 'push':
370
                $qb->orderBy('p.createdAt', 'DESC');
371
                break;
372
            case 'id':
373
                $qb->orderBy('p.createdAt', 'ASC');
374
                break;
375
            default:
376
                $qb->orderBy('p.createdAt', 'ASC');
377
                break;
378
        }
379
        
380
        $query = $qb->getQuery();
381
        
382
        $posts = $query->getResult();
383
        $arrayPosts = array();
384
        $i=0;
385
        foreach ($posts as $postRaw) {
386
            $data = array();
387
            $post = $postRaw[0];
388
            if ($post) {
389
                foreach ($post->getPostElements() as $element) {
390
                    $data[$element->getPosition()] = $element->getValue();
391
                }
392
                $arrayPosts[$i]['post']  = $post;
393
                $arrayPosts[$i]['data']  = $data;
394
                $arrayPosts[$i]['votes'] = count($post->getVotes());
395
                $arrayPosts[$i]['voted'] = $postRaw['voted'];
396
                $arrayPosts[$i]['id']    = $post->getId();
397
                $arrayPosts[$i]['user']  = $post->getUser();
398
                $arrayPosts[$i]['createdAt']  = $post->getCreatedAt();
399
                $i++;
400
            }
401
        }
402
403
        return $arrayPosts;
404
    }
405
406 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...
407
    {
408
        $postvoteVoteMapper = $this->getPostVoteVoteMapper();
409
        $postId = $post->getId();
410
        $commentId = ($comment !== null) ? $comment->getId() : null;
411
        $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...
412
        $game = $post->getPostvote();
413
414
        if ($user) {
415
            if ($comment == null) {
416
                $entryUser = count($postvoteVoteMapper->findBy(array('user' => $user, 'post' => $postId)));
417
                $vote = $postvoteVoteMapper->findOneBy(array('user' => $user, 'post' => $postId));
418
            } else {
419
                $entryUser = count($postvoteVoteMapper->findBy(array('user' => $user, 'post' => $postId, 'postComment' => $commentId)));
420
                $vote = $postvoteVoteMapper->findOneBy(array('user' => $user, 'post' => $postId, 'postComment' => $commentId));
421
            }
422
        } else {
423
            if ($comment == null) {
424
                $entryUser = count($postvoteVoteMapper->findBy(array('ip' => $ipAddress, 'post' => $postId)));
425
                $vote = $postvoteVoteMapper->findOneBy(array('ip' => $ipAddress, 'post' => $postId));
426
            } else {
427
                $entryUser = count($postvoteVoteMapper->findBy(array('ip' => $ipAddress, 'post' => $postId, 'postComment' => $commentId)));
428
                $vote = $postvoteVoteMapper->findOneBy(array('ip' => $ipAddress, 'post' => $postId, 'postComment' => $commentId));
429
            }
430
        }
431
432
        if ($entryUser && $entryUser > 0) {
433
            $postvoteVoteMapper->remove($vote);
434
435
            return 0;
436
        } else {
437
            $vote = new \PlaygroundGame\Entity\PostVoteVote();
438
            $vote->setPost($post);
439
            $vote->setIp($ipAddress);
440
            $vote->setNote(1);
441
            // If the vote is for a comment
442
            if ($comment != null) {
443
                $vote->setPostComment($comment);
444
                $vote->setPostvote($post->getPostvote());
445
            // else if the vote is for the post itself
446
            } else {
447
                $vote->setPostvote($post->getPostvote(), true);
448
            }
449
450
            if ($user) {
451
                $vote->setUser($user);
452
            }
453
454
            $postvoteVoteMapper->insert($vote);
455
        }
456
457
        $this->getEventManager()->trigger(
458
            __FUNCTION__ .'.post',
459
            $this,
460
            array('user' => $user, 'game' => $game, 'post' => $post, 'vote' => $vote)
461
        );
462
463
        return 1;
464
    }
465
466
    public function removeVote($user, $ipAddress, $post)
467
    {
468
        $postvoteVoteMapper = $this->getPostVoteVoteMapper();
469
        $postId = $post->getId();
470
        $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...
471
        $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...
472
        $game = $post->getPostvote();
473
474
        if ($user) {
475
            if ($comment == null) {
476
                $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...
477
                $vote = $postvoteVoteMapper->findOneBy(array('user' => $user, 'post' => $postId));
478
            } else {
479
                $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...
480
                $vote = $postvoteVoteMapper->findOneBy(array('user' => $user, 'post' => $postId, 'postComment' => $commentId));
481
            }
482
        } else {
483
            if ($comment == null) {
484
                $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...
485
                $vote = $postvoteVoteMapper->findOneBy(array('ip' => $ipAddress, 'post' => $postId));
486
            } else {
487
                $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...
488
                $vote = $postvoteVoteMapper->findOneBy(array('ip' => $ipAddress, 'post' => $postId, 'postComment' => $commentId));
489
            }
490
        }
491
492
        $this->getEventManager()->trigger(
493
            __FUNCTION__ .'.post',
494
            $this,
495
            array('user' => $user, 'game' => $game, 'post' => $post, 'vote' => $vote)
496
        );
497
498
        return true;
499
    }
500
501 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...
502
    {
503
        $postvoteVoteMapper = $this->getPostVoteVoteMapper();
504
        $postId = $post->getId();
505
        $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...
506
        $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...
507
        $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...
508
509
        if ($user) {
510
            if ($comment == null) {
511
                $entryUser = count($postvoteVoteMapper->findBy(array('user' => $user, 'post' => $postId)));
512
                $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...
513
            } else {
514
                $entryUser = count($postvoteVoteMapper->findBy(array('user' => $user, 'post' => $postId, 'postComment' => $commentId)));
515
                $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...
516
            }
517
        } else {
518
            if ($comment == null) {
519
                $entryUser = count($postvoteVoteMapper->findBy(array('ip' => $ipAddress, 'post' => $postId)));
520
                $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...
521
            } else {
522
                $entryUser = count($postvoteVoteMapper->findBy(array('ip' => $ipAddress, 'post' => $postId, 'postComment' => $commentId)));
523
                $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...
524
            }
525
        }
526
527
        if ($entryUser && $entryUser > 0) {
528
            return false;
529
        } else {
530
            $vote = new \PlaygroundGame\Entity\PostVoteVote();
531
            $vote->setPost($post);
532
            $vote->setIp($ipAddress);
533
            $vote->setNote(1);
534
            // If the vote is for a comment
535
            if ($comment != null) {
536
                $vote->setPostComment($comment);
537
                $vote->setPostvote($post->getPostvote());
538
            // else if the vote is for the post itself
539
            } else {
540
                $vote->setPostvote($post->getPostvote(), true);
541
            }
542
            if ($user) {
543
                $vote->setUser($user);
544
            }
545
546
            $postvoteVoteMapper->insert($vote);
547
            $game = $post->getPostvote();
548
            $this->getEventManager()->trigger(
549
                __FUNCTION__ .'.post',
550
                $this,
551
                array('user' => $user, 'game' => $game, 'post' => $post, 'vote' => $vote)
552
            );
553
554
            return true;
555
        }
556
    }
557
558
    public function addComment($user, $ipAddress, $post, $message = '', $category = null)
559
    {
560
        $postvoteCommentMapper = $this->getPostVoteCommentMapper();
561
        $game = $post->getPostvote();
562
        $comment = new \PlaygroundGame\Entity\PostVoteComment();
563
        $comment->setPost($post);
564
        $comment->setIp($ipAddress);
565
        $message = strip_tags($message);
566
        $comment->setMessage($message);
567
        if ($category !== null) {
568
            $comment->setCategory($category);
569
        }
570
        $comment->setPostvote($game);
571
        if ($user) {
572
            $comment->setUser($user);
573
        }
574
575
        $postvoteCommentMapper->insert($comment);
576
        
577
        $this->getEventManager()->trigger(
578
            __FUNCTION__ .'.post',
579
            $this,
580
            array('user' => $user, 'game' => $game, 'post' => $post, 'comment' => $comment)
581
        );
582
583
        return true;
584
    }
585
586 View Code Duplication
    public function addView($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...
587
    {
588
        $postvoteViewMapper = $this->getPostVoteViewMapper();
589
        $postView = new \PlaygroundGame\Entity\PostVoteView();
590
        $postView->setPost($post)
591
            ->setPostvote($post->getPostvote())
592
            ->setUser($user)
593
            ->setIp($ipAddress);
594
        $postvoteViewMapper->insert($postView);
595
596
        $this->getEventManager()->trigger(
597
            __FUNCTION__ . '.post',
598
            $this,
599
            array(
600
                'post' => $post
601
            )
602
        );
603
604
        return true;
605
    }
606
607 View Code Duplication
    public function addShare($post, $user)
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...
608
    {
609
        $postvoteShareMapper = $this->getPostVoteShareMapper();
610
        $postShare = new \PlaygroundGame\Entity\PostVoteShare();
611
        $postShare->setPost($post)
612
            ->setPostvote($post->getPostvote())
613
            ->setUser($user)
614
            ->setOrigin('mail');
615
        $postvoteShareMapper->insert($postShare);
616
617
        $this->getEventManager()->trigger(
618
            __FUNCTION__ . '.post',
619
            $this,
620
            array(
621
                'post' => $post
622
            )
623
        );
624
625
        return true;
626
    }
627
628
    /**
629
     * Get all comments for this game
630
     */
631
    public function getCommentsForPostvote($postvote)
632
    {
633
        $postvoteCommentMapper = $this->getPostVoteCommentMapper();
634
        $comments = $postvoteCommentMapper->findBy(array('postvote' => $postvote), array('createdAt' => 'DESC'));
635
636
        return $comments ;
637
    }
638
639
    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...
640
    {
641
        $postvoteCommentMapper = $this->getPostVoteCommentMapper();
642
        $comment = $postvoteCommentMapper->findOneBy(array('id' => $messageId));
643
        if ($comment->getUser()->getId() === $user->getId()) {
644
            $postvoteCommentMapper->remove($comment);
645
            $this->getEventManager()->trigger(
646
                'remove_comment_postvote.post',
647
                $this,
648
                array('user' => $user, 'comment' => $comment)
649
            );
650
651
            return true;
652
        }
653
654
        return false;
655
    }
656
657
    public function getEntriesHeader($game)
658
    {
659
        $header = parent::getEntriesHeader($game);
660
        if ($game->getForm()) {
661
            $form = json_decode($game->getForm()->getForm(), true);
662 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...
663
                foreach ($element as $k => $v) {
664
                    if ($k !== 'form_properties') {
665
                        $header[$v[0]['name']] = 1;
666
                    }
667
                }
668
            }
669
        }
670
        if ($game->getVoteActive()) {
671
            $header['votes'] = 1;
672
        }
673
674
        $header['views'] = 1;
675
        $header['shares'] = 1;
676
677
        return $header;
678
    }
679
680 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...
681
    {
682
        $em = $this->serviceLocator->get('doctrine.entitymanager.orm_default');
683
684
        $qb = $em->createQueryBuilder();
685
        $qb->select('
686
            p.id,
687
            u.username,
688
            u.title,
689
            u.firstname,
690
            u.lastname,
691
            u.displayName,
692
            u.email,
693
            u.optin,
694
            u.optinPartner,
695
            u.address,
696
            u.address2,
697
            u.postalCode,
698
            u.city,
699
            u.telephone,
700
            u.mobile,
701
            u.created_at,
702
            u.dob,
703
            e.winner,
704
            e.socialShares,
705
            e.playerData,
706
            e.updated_at,
707
            p.status,
708
            p
709
            ')
710
            ->from('PlaygroundGame\Entity\PostVotePost', 'p')
711
            ->innerJoin('p.entry', 'e')
712
            ->leftJoin('p.user', 'u')
713
            ->where($qb->expr()->eq('e.game', ':game'));
714
        
715
        $qb->setParameter('game', $game);
716
717
        return $qb->getQuery();
718
    }
719
720
    /**
721
    * getGameEntries : All entries of a game
722
    *
723
    * @return Array of PlaygroundGame\Entity\Game
724
    */
725
    public function getGameEntries($header, $entries, $game)
726
    {
727
        $results = array();
728
729
        foreach ($entries as $k => $entry) {
730
            $entryData = json_decode($entry['playerData'], true);
731
            $postElements = $entry[0]->getPostElements();
732
733
            foreach ($header as $key => $v) {
734
                if (isset($entryData[$key]) && $key !=='id') {
735
                    $results[$k][$key] = (is_array($entryData[$key]))?implode(', ', $entryData[$key]):$entryData[$key];
736
                } elseif (array_key_exists($key, $entry)) {
737
                    $results[$k][$key] = ($entry[$key] instanceof \DateTime)?$entry[$key]->format('Y-m-d'):$entry[$key];
738
                } else {
739
                    $results[$k][$key] = '';
740
                }
741
742
                foreach ($postElements as $e) {
743
                    if ($key === $e->getName()) {
744
                        $results[$k][$key] = (is_array($e->getValue()))?implode(', ', $e->getValue()):$e->getValue();
745
                        break;
746
                    }
747
                }
748
749
                if ($key === 'votes') {
750
                    $results[$k][$key] = count($entry[0]->getVotes());
751
                }
752
                if ($key === 'views') {
753
                    $results[$k][$key] = count($entry[0]->getViews());
754
                }
755
                if ($key === 'shares') {
756
                    $results[$k][$key] = count($entry[0]->getShares());
757
                }
758
            }
759
        }
760
761
        return $results;
762
    }
763
764
    public function getPostVoteFormMapper()
765
    {
766
        if (null === $this->postvoteformMapper) {
767
            $this->postvoteformMapper = $this->serviceLocator->get('playgroundgame_postvoteform_mapper');
768
        }
769
770
        return $this->postvoteformMapper;
771
    }
772
773
    public function setPostVoteFormMapper($postvoteformMapper)
774
    {
775
        $this->postvoteformMapper = $postvoteformMapper;
776
777
        return $this;
778
    }
779
780
    public function getPostVotePostElementMapper()
781
    {
782
        if (null === $this->postVotePostElementMapper) {
783
            $this->postVotePostElementMapper = $this->serviceLocator->get(
784
                'playgroundgame_postvotepostelement_mapper'
785
            );
786
        }
787
788
        return $this->postVotePostElementMapper;
789
    }
790
791
    public function setPostVotePostElementMapper($postVotePostElementMapper)
792
    {
793
        $this->postVotePostElementMapper = $postVotePostElementMapper;
794
795
        return $this;
796
    }
797
798
    public function getPostVoteVoteMapper()
799
    {
800
        if (null === $this->postVoteVoteMapper) {
801
            $this->postVoteVoteMapper = $this->serviceLocator->get('playgroundgame_postvotevote_mapper');
802
        }
803
804
        return $this->postVoteVoteMapper;
805
    }
806
807
    public function setPostVoteVoteMapper($postVoteVoteMapper)
808
    {
809
        $this->postVoteVoteMapper = $postVoteVoteMapper;
810
811
        return $this;
812
    }
813
814
    public function getPostVoteCommentMapper()
815
    {
816
        if (null === $this->postVoteCommentMapper) {
817
            $this->postVoteCommentMapper = $this->serviceLocator->get('playgroundgame_postvotecomment_mapper');
818
        }
819
820
        return $this->postVoteCommentMapper;
821
    }
822
823
    public function setPostVoteCommentMapper($postVoteCommentMapper)
824
    {
825
        $this->postVoteCommentMapper = $postVoteCommentMapper;
826
827
        return $this;
828
    }
829
830
    public function getPostVotePostMapper()
831
    {
832
        if (null === $this->postVotePostMapper) {
833
            $this->postVotePostMapper = $this->serviceLocator->get('playgroundgame_postvotepost_mapper');
834
        }
835
836
        return $this->postVotePostMapper;
837
    }
838
839
    public function getPostVoteShareMapper()
840
    {
841
        if (null === $this->postVoteShareMapper) {
842
            $this->postVoteShareMapper = $this->serviceLocator->get('playgroundgame_postvoteshare_mapper');
843
        }
844
845
        return $this->postVoteShareMapper;
846
    }
847
848
    public function getPostVoteViewMapper()
849
    {
850
        if (null === $this->postVoteViewMapper) {
851
            $this->postVoteViewMapper = $this->serviceLocator->get('playgroundgame_postvoteview_mapper');
852
        }
853
854
        return $this->postVoteViewMapper;
855
    }
856
857
    public function setPostVotePostMapper($postVotePostMapper)
858
    {
859
        $this->postVotePostMapper = $postVotePostMapper;
860
861
        return $this;
862
    }
863
864
    public function getPostVoteMapper()
865
    {
866
        if (null === $this->postvoteMapper) {
867
            $this->postvoteMapper = $this->serviceLocator->get('playgroundgame_postvote_mapper');
868
        }
869
870
        return $this->postvoteMapper;
871
    }
872
873
    /**
874
     * setQuizQuestionMapper
875
     *
876
     * @return PostVote
877
     */
878
    public function setPostVoteMapper($postvoteMapper)
879
    {
880
        $this->postvoteMapper = $postvoteMapper;
881
882
        return $this;
883
    }
884
}
885