Completed
Push — master ( e95261...40aeb3 )
by greg
04:30 queued 01:59
created

PostVote::addView()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20

Duplication

Lines 20
Ratio 100 %

Importance

Changes 0
Metric Value
dl 20
loc 20
rs 9.6
c 0
b 0
f 0
cc 1
nc 1
nop 3
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 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...
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
        return $header;
675
    }
676
677 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...
678
    {
679
        $em = $this->serviceLocator->get('doctrine.entitymanager.orm_default');
680
681
        $qb = $em->createQueryBuilder();
682
        $qb->select('
683
            p.id,
684
            u.username,
685
            u.title,
686
            u.firstname,
687
            u.lastname,
688
            u.displayName,
689
            u.email,
690
            u.optin,
691
            u.optinPartner,
692
            u.address,
693
            u.address2,
694
            u.postalCode,
695
            u.city,
696
            u.telephone,
697
            u.mobile,
698
            u.created_at,
699
            u.dob,
700
            e.winner,
701
            e.socialShares,
702
            e.playerData,
703
            e.updated_at,
704
            p.status,
705
            p
706
            ')
707
            ->from('PlaygroundGame\Entity\PostVotePost', 'p')
708
            ->innerJoin('p.entry', 'e')
709
            ->leftJoin('p.user', 'u')
710
            ->where($qb->expr()->eq('e.game', ':game'));
711
        
712
        $qb->setParameter('game', $game);
713
714
        return $qb->getQuery();
715
    }
716
717
    /**
718
    * getGameEntries : All entries of a game
719
    *
720
    * @return Array of PlaygroundGame\Entity\Game
721
    */
722
    public function getGameEntries($header, $entries, $game)
723
    {
724
        $results = array();
725
726
        foreach ($entries as $k => $entry) {
727
            $entryData = json_decode($entry['playerData'], true);
728
            $postElements = $entry[0]->getPostElements();
729
730
            foreach ($header as $key => $v) {
731
                if (isset($entryData[$key]) && $key !=='id') {
732
                    $results[$k][$key] = (is_array($entryData[$key]))?implode(', ', $entryData[$key]):$entryData[$key];
733
                } elseif (array_key_exists($key, $entry)) {
734
                    $results[$k][$key] = ($entry[$key] instanceof \DateTime)?$entry[$key]->format('Y-m-d'):$entry[$key];
735
                } else {
736
                    $results[$k][$key] = '';
737
                }
738
739
                foreach ($postElements as $e) {
740
                    if ($key === $e->getName()) {
741
                        $results[$k][$key] = (is_array($e->getValue()))?implode(', ', $e->getValue()):$e->getValue();
742
                        break;
743
                    }
744
                }
745
746
                if ($key === 'votes') {
747
                    $results[$k][$key] = count($entry[0]->getVotes());
748
                    break;
749
                }
750
            }
751
        }
752
753
        return $results;
754
    }
755
756
    public function getPostVoteFormMapper()
757
    {
758
        if (null === $this->postvoteformMapper) {
759
            $this->postvoteformMapper = $this->serviceLocator->get('playgroundgame_postvoteform_mapper');
760
        }
761
762
        return $this->postvoteformMapper;
763
    }
764
765
    public function setPostVoteFormMapper($postvoteformMapper)
766
    {
767
        $this->postvoteformMapper = $postvoteformMapper;
768
769
        return $this;
770
    }
771
772
    public function getPostVotePostElementMapper()
773
    {
774
        if (null === $this->postVotePostElementMapper) {
775
            $this->postVotePostElementMapper = $this->serviceLocator->get(
776
                'playgroundgame_postvotepostelement_mapper'
777
            );
778
        }
779
780
        return $this->postVotePostElementMapper;
781
    }
782
783
    public function setPostVotePostElementMapper($postVotePostElementMapper)
784
    {
785
        $this->postVotePostElementMapper = $postVotePostElementMapper;
786
787
        return $this;
788
    }
789
790
    public function getPostVoteVoteMapper()
791
    {
792
        if (null === $this->postVoteVoteMapper) {
793
            $this->postVoteVoteMapper = $this->serviceLocator->get('playgroundgame_postvotevote_mapper');
794
        }
795
796
        return $this->postVoteVoteMapper;
797
    }
798
799
    public function setPostVoteVoteMapper($postVoteVoteMapper)
800
    {
801
        $this->postVoteVoteMapper = $postVoteVoteMapper;
802
803
        return $this;
804
    }
805
806
    public function getPostVoteCommentMapper()
807
    {
808
        if (null === $this->postVoteCommentMapper) {
809
            $this->postVoteCommentMapper = $this->serviceLocator->get('playgroundgame_postvotecomment_mapper');
810
        }
811
812
        return $this->postVoteCommentMapper;
813
    }
814
815
    public function setPostVoteCommentMapper($postVoteCommentMapper)
816
    {
817
        $this->postVoteCommentMapper = $postVoteCommentMapper;
818
819
        return $this;
820
    }
821
822
    public function getPostVotePostMapper()
823
    {
824
        if (null === $this->postVotePostMapper) {
825
            $this->postVotePostMapper = $this->serviceLocator->get('playgroundgame_postvotepost_mapper');
826
        }
827
828
        return $this->postVotePostMapper;
829
    }
830
831
    public function getPostVoteShareMapper()
832
    {
833
        if (null === $this->postVoteShareMapper) {
834
            $this->postVoteShareMapper = $this->serviceLocator->get('playgroundgame_postvoteshare_mapper');
835
        }
836
837
        return $this->postVoteShareMapper;
838
    }
839
840
    public function getPostVoteViewMapper()
841
    {
842
        if (null === $this->postVoteViewMapper) {
843
            $this->postVoteViewMapper = $this->serviceLocator->get('playgroundgame_postvoteview_mapper');
844
        }
845
846
        return $this->postVoteViewMapper;
847
    }
848
849
    public function setPostVotePostMapper($postVotePostMapper)
850
    {
851
        $this->postVotePostMapper = $postVotePostMapper;
852
853
        return $this;
854
    }
855
856
    public function getPostVoteMapper()
857
    {
858
        if (null === $this->postvoteMapper) {
859
            $this->postvoteMapper = $this->serviceLocator->get('playgroundgame_postvote_mapper');
860
        }
861
862
        return $this->postvoteMapper;
863
    }
864
865
    /**
866
     * setQuizQuestionMapper
867
     *
868
     * @return PostVote
869
     */
870
    public function setPostVoteMapper($postvoteMapper)
871
    {
872
        $this->postvoteMapper = $postvoteMapper;
873
874
        return $this;
875
    }
876
}
877