Completed
Push — master ( 4aa860...5dfdf5 )
by greg
57:27 queued 57:27
created

PostVote   D

Complexity

Total Complexity 94

Size/Duplication

Total Lines 659
Duplicated Lines 11.08 %

Coupling/Cohesion

Components 1
Dependencies 10

Importance

Changes 15
Bugs 7 Features 0
Metric Value
wmc 94
c 15
b 7
f 0
lcom 1
cbo 10
dl 73
loc 659
rs 4.6279

30 Methods

Rating   Name   Duplication   Size   Complexity  
A getGameEntity() 0 4 1
A getPath() 0 14 3
A getMediaUrl() 0 7 1
A checkPost() 0 14 2
B deleteFilePosted() 0 25 4
B createForm() 30 30 5
C findArrayOfValidatedPosts() 0 80 10
B addVote() 0 32 5
B uploadFileToPost() 0 32 4
C createPost() 0 80 13
B confirmPost() 0 44 5
B addComment() 0 24 2
A getCommentsForPostvote() 0 7 1
A getCommentsForPost() 0 7 1
A removeComment() 0 17 2
B getEntriesHeader() 6 16 5
B getEntriesQuery() 37 38 1
C getGameEntries() 0 28 11
A getPostVoteFormMapper() 0 8 2
A setPostVoteFormMapper() 0 6 1
A getPostVotePostElementMapper() 0 10 2
A setPostVotePostElementMapper() 0 6 1
A getPostVoteVoteMapper() 0 8 2
A setPostVoteVoteMapper() 0 6 1
A getPostVoteCommentMapper() 0 8 2
A setPostVoteCommentMapper() 0 6 1
A getPostVotePostMapper() 0 8 2
A setPostVotePostMapper() 0 6 1
A getPostVoteMapper() 0 8 2
A setPostVoteMapper() 0 6 1

How to fix   Duplicated Code    Complexity   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like PostVote often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use PostVote, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
namespace PlaygroundGame\Service;
4
5
use Zend\ServiceManager\ServiceManagerAwareInterface;
6
use Zend\Stdlib\ErrorHandler;
7
8
class PostVote extends Game implements ServiceManagerAwareInterface
9
{
10
    protected $postvoteMapper;
11
    protected $postvoteformMapper;
12
    protected $postVotePostMapper;
13
    protected $postVoteVoteMapper;
14
    protected $postVoteCommentMapper;
15
    protected $postVotePostElementMapper;
16
17
    public function getGameEntity()
18
    {
19
        return new \PlaygroundGame\Entity\PostVote;
20
    }
21
22
    public function getPath($post)
23
    {
24
        $path = $this->getOptions()->getMediaPath() . DIRECTORY_SEPARATOR;
25
        $path .= 'game' . $post->getPostVote()->getId() . DIRECTORY_SEPARATOR;
26
        if (!is_dir($path)) {
27
            mkdir($path, 0777, true);
28
        }
29
        $path .= 'post'. $post->getId() . DIRECTORY_SEPARATOR;
30
        if (!is_dir($path)) {
31
            mkdir($path, 0777, true);
32
        }
33
34
        return $path;
35
    }
36
37
    public function getMediaUrl($post)
38
    {
39
        $media_url = $this->getOptions()->getMediaUrl() . '/';
40
        $media_url .= 'game' . $post->getPostVote()->getId() . '/' . 'post'. $post->getId() . '/';
41
42
        return $media_url;
43
    }
44
45
    /**
46
     * @param boolean $entry
47
     */
48
    public function checkPost($entry)
49
    {
50
        $post = $this->getPostVotePostMapper()->findOneBy(array('entry' => $entry));
51
52
        if (! $post) {
53
            $post = new \PlaygroundGame\Entity\PostVotePost();
54
            $post->setPostvote($entry->getGame());
0 ignored issues
show
Bug introduced by
The method getGame cannot be called on $entry (of type boolean).

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

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

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

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

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

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

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

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

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

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

Let’s take a look at an example:

class Author {
    private $name;

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

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

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

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

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

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

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

Loading history...
136
        }
137
138
        $post = $this->checkPost($entry);
139
        $path = $this->getPath($post);
140
        $media_url = $this->getMediaUrl($post);
141
        $position=1;
142
143
        foreach ($data as $name => $value) {
144
            $postElement = $postVotePostElementMapper->findOneBy(array('post' => $post, 'name' => $name));
145
            if (! $postElement) {
146
                $postElement = new \PlaygroundGame\Entity\PostVotePostElement();
147
            }
148
            $postElement->setName($name);
149
            $postElement->setPosition($position);
150
151
            if (is_array($value) && isset($value['tmp_name'])) {
152
                // The file upload has been done in ajax but some weird bugs remain without it
153
154
                if (! $value['error']) {
155
                    ErrorHandler::start();
156
                    $value['name'] = $this->fileNewname($path, $value['name'], true);
157
                    move_uploaded_file($value['tmp_name'], $path . $value['name']);
158
                    $image = $this->getServiceManager()->get('playgroundcore_image_service');
159
                    $image->setImage($path . $value['name']);
160
161
                    if ($image->canCorrectOrientation()) {
162
                        $image->correctOrientation()->save();
163
                    }
164
                    $postElement->setValue($media_url . $value['name']);
165
                    
166
                    if (class_exists("Imagick")) {
167
                        $ext = pathinfo($value['name'], PATHINFO_EXTENSION);
168
                        $img = new \Imagick($path . $value['name']);
169
                        $img->cropThumbnailImage(100, 100);
170
                        $img->setImageCompression(\Imagick::COMPRESSION_JPEG);
171
                        $img->setImageCompressionQuality(75);
172
                        // Strip out unneeded meta data
173
                        $img->stripImage();
174
                        $img->writeImage($path . str_replace('.'.$ext, '-thumbnail.'.$ext, $value['name']));
175
                        ErrorHandler::stop(true);
176
                    }
177
                }
178
            } elseif (is_array($value) || $form->get($name) instanceof \Zend\Form\Element\Select) {
179
                $arValues = $form->get($name)->getValueOptions();
180
                $postElement->setValue($arValues[$value[0]]);
181
            } elseif (!empty($value)) {
182
                $postElement->setValue($value);
183
            }
184
            $postElement->setPost($post);
185
            $postVotePostElementMapper->insert($postElement);
186
            $position++;
187
        }
188
189
        $postvotePostMapper->update($post);
190
191
        // If a preview step is not proposed, I confirmPost on this step
192
        $steps = $game->getStepsArray();
193
        $previewKey = array_search('preview', $steps);
194
        if (!$previewKey) {
195
            $post = $this->confirmPost($game, $user);
196
        }
197
198
        $this->getEventManager()->trigger(__FUNCTION__ . '.post', $this, array(
199
            'user' => $user,
200
            'game' => $game,
201
            'post' => $post,
202
            'entry' => $entry
203
        ));
204
205
        return $post;
206
    }
207
208
    /**
209
     *
210
     * @return \PlaygroundGame\Entity\Game
211
     */
212
    public function confirmPost($game, $user)
213
    {
214
        $postvotePostMapper = $this->getPostVotePostMapper();
215
216
        $entryMapper = $this->getEntryMapper();
217
        $entry = $this->findLastActiveEntry($game, $user);
218
219
        if (!$entry) {
220
            return false;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return false; (false) is incompatible with the return type documented by PlaygroundGame\Service\PostVote::confirmPost of type PlaygroundGame\Entity\Game.

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

Let’s take a look at an example:

class Author {
    private $name;

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

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

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

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

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

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

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

Loading history...
221
        }
222
223
        $post = $postvotePostMapper->findOneBy(array('entry' => $entry));
224
225
        if (! $post) {
226
            return false;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return false; (false) is incompatible with the return type documented by PlaygroundGame\Service\PostVote::confirmPost of type PlaygroundGame\Entity\Game.

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

Let’s take a look at an example:

class Author {
    private $name;

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

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

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

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

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

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

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

Loading history...
227
        }
228
229
        // The post is confirmed by user. I update the status and close the associated entry
230
        // Post are validated by default, unless pre-moderation is enable for the game
231
        if ($game->getModerationType()) {
232
            $post->setStatus(1);
233
        } else {
234
            $post->setStatus(2);
235
        }
236
237
        $postvotePostMapper->update($post);
238
239
        $entry->setActive(0);
0 ignored issues
show
Bug introduced by
The method setActive cannot be called on $entry (of type boolean).

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

Loading history...
240
        $entryMapper->update($entry);
241
242
        $this->getEventManager()->trigger(__FUNCTION__ . '.post', $this, array(
243
            'user' => $user,
244
            'game' => $game,
245
            'entry' => $entry,
246
            'post' => $post
247
        ));
248
249
        if ($user) {
250
            // send mail for participation
251
            $this->sendGameMail($game, $user, $post, 'postvote');
252
        }
253
254
        return $post;
255
    }
256
257
    /**
258
     *
259
     * This service is ready for all types of games
260
     *
261
     * @param  array                  $data
262
     * @return \PlaygroundGame\Entity\Game
263
     */
264 View Code Duplication
    public function createForm(array $data, $game, $form = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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

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

Loading history...
265
    {
266
        $title ='';
267
        $description = '';
268
269
        if ($data['form_jsonified']) {
270
            $jsonPV = json_decode($data['form_jsonified']);
271
            foreach ($jsonPV as $element) {
272
                if ($element->form_properties) {
273
                    $attributes  = $element->form_properties[0];
274
                    $title       = $attributes->title;
275
                    $description = $attributes->description;
276
277
                    break;
278
                }
279
            }
280
        }
281
        if (!$form) {
282
            $form = new \PlaygroundGame\Entity\PostVoteForm();
283
        }
284
        $form->setPostvote($game);
285
        $form->setTitle($title);
286
        $form->setDescription($description);
287
        $form->setForm($data['form_jsonified']);
288
        $form->setFormTemplate($data['form_template']);
289
290
        $form = $this->getPostVoteFormMapper()->insert($form);
291
292
        return $form;
293
    }
294
295
    public function findArrayOfValidatedPosts($game, $filter, $search = '')
296
    {
297
        $em = $this->getServiceManager()->get('doctrine.entitymanager.orm_default');
298
        $qb = $em->createQueryBuilder();
299
        $and = $qb->expr()->andx();
300
        
301
        $and->add($qb->expr()->eq('p.status', 2));
302
303
        $and->add($qb->expr()->eq('g.id', ':game'));
304
        $qb->setParameter('game', $game);
305
        
306
        if ($search != '') {
307
            $and->add(
308
                $qb->expr()->orX(
309
                    $qb->expr()->like('u.username', $qb->expr()->literal('%:search%')),
310
                    $qb->expr()->like('u.firstname', $qb->expr()->literal('%:search%')),
311
                    $qb->expr()->like('u.lastname', $qb->expr()->literal('%:search%')),
312
                    $qb->expr()->like('e.value', $qb->expr()->literal('%:search%')),
313
                    $qb->expr()->isNull('g.publicationDate')
314
                )
315
            );
316
            $qb->setParameter('search', $search);
317
        }
318
        
319
        if ('push' == $filter) {
320
            $and->add(
321
                $qb->expr()->andX(
322
                    $qb->expr()->eq('p.pushed', 1)
323
                )
324
            );
325
        }
326
        
327
        $qb->select('p, COUNT(v) AS votesCount')
328
            ->from('PlaygroundGame\Entity\PostVotePost', 'p')
329
            ->innerJoin('p.postvote', 'g')
330
            ->leftJoin('p.user', 'u')
331
            ->innerJoin('p.postElements', 'e')
332
            ->leftJoin('p.votes', 'v')
333
            ->where($and)
334
            ->groupBy('p.id');
335
 
336
        switch ($filter) {
337
            case 'random':
338
                $qb->orderBy('e.value', 'ASC');
339
                break;
340
            case 'vote':
341
                $qb->orderBy('votesCount', 'DESC');
342
                break;
343
            case 'date':
344
                $qb->orderBy('p.createdAt', 'DESC');
345
                break;
346
            case 'push':
347
                $qb->orderBy('p.createdAt', 'DESC');
348
                break;
349
        }
350
        
351
        $query = $qb->getQuery();
352
        
353
        $posts = $query->getResult();
354
        $arrayPosts = array();
355
        $i=0;
356
        foreach ($posts as $postRaw) {
357
            $data = array();
358
            $post = $postRaw[0];
359
            if ($post) {
360
                foreach ($post->getPostElements() as $element) {
361
                    $data[$element->getPosition()] = $element->getValue();
362
                }
363
                $arrayPosts[$i]['post']  = $post;
364
                $arrayPosts[$i]['data']  = $data;
365
                $arrayPosts[$i]['votes'] = count($post->getVotes());
366
                $arrayPosts[$i]['id']    = $post->getId();
367
                $arrayPosts[$i]['user']  = $post->getUser();
368
                $arrayPosts[$i]['createdAt']  = $post->getCreatedAt();
369
                $i++;
370
            }
371
        }
372
373
        return $arrayPosts;
374
    }
375
376
    public function addVote($user, $ipAddress, $post)
377
    {
378
        $postvoteVoteMapper = $this->getPostVoteVoteMapper();
379
        $postId = $post->getId();
380
381
        if ($user) {
382
            $userId = $user->getId();
383
            $entryUser = count($postvoteVoteMapper->findBy(array('userId' => $userId, 'post' =>$postId)));
384
        } else {
385
            $entryUser =count($postvoteVoteMapper->findBy(array('ip' => $ipAddress, 'post' =>$postId)));
386
        }
387
        if ($entryUser && $entryUser > 0) {
388
            return false;
389
        } else {
390
            $vote = new \PlaygroundGame\Entity\PostVoteVote();
391
            $vote->setPost($post);
392
            $vote->setIp($ipAddress);
393
            if ($user) {
394
                $vote->setUserId($user->getId());
395
            }
396
397
            $postvoteVoteMapper->insert($vote);
398
            $game = $post->getPostvote();
399
            $this->getEventManager()->trigger(
400
                'vote_postvote.post',
401
                $this,
402
                array('user' => $user, 'game' => $game, 'post' => $post, 'vote' => $vote)
403
            );
404
405
            return true;
406
        }
407
    }
408
409
    public function addComment($user, $ipAddress, $post, $message = '')
410
    {
411
        $postvoteCommentMapper = $this->getPostVoteCommentMapper();
412
        $postId = $post->getId();
0 ignored issues
show
Unused Code introduced by
$postId is not used, you could remove the assignment.

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

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

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

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

Loading history...
413
        $game = $post->getPostvote();
414
        $comment = new \PlaygroundGame\Entity\PostVoteComment();
415
        $comment->setPost($post);
416
        $comment->setIp($ipAddress);
417
        $comment->setMessage($message);
0 ignored issues
show
Documentation introduced by
$message is of type string, but the function expects a object<PlaygroundGame\Entity\unknown_type>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

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