Completed
Push — master ( 315e5f...030a80 )
by greg
05:43 queued 02:53
created

Entry::findByGameId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace PlaygroundGame\Mapper;
4
5
use PlaygroundGame\Mapper\AbstractMapper;
6
7
class Entry extends AbstractMapper
8
{
9
    
10
    public function countByGame(\PlaygroundGame\Entity\Game $game)
11
    {
12
        $query = $this->em->createQuery('SELECT COUNT(e.id) FROM PlaygroundGame\Entity\Entry e WHERE e.game = :game');
13
        $query->setParameter('game', $game);
14
        return $query->getSingleScalarResult();
15
    }
16
17
    public function draw($game, $userClass, $total)
0 ignored issues
show
Unused Code introduced by
The parameter $userClass 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...
18
    {
19
        $sql ='
20
            SELECT 
21
                u.user_id uid, 
22
                u.username, 
23
                u.firstname, 
24
                u.lastname, 
25
                u.email, 
26
                u.optin_partner, 
27
                e.created_at ecreated_at, 
28
                e.updated_at eupdated_at, 
29
                e.* 
30
            FROM game_entry as e
31
            INNER JOIN user AS u ON e.user_id = u.user_id
32
            WHERE e.game_id = :game_id AND e.drawable = 1
33
            GROUP BY u.user_id
34
            ORDER BY RAND()
35
            LIMIT :total
36
        ';
37
        
38
        $rsm = new \Doctrine\ORM\Query\ResultSetMappingBuilder($this->em);
39
        $rsm->addRootEntityFromClassMetadata(
40
            '\PlaygroundGame\Entity\Entry',
41
            'e',
42
            array('id' => 'id', 'created_at' => 'ecreated_at', 'updated_at' => 'eupdated_at')
43
        );
44
        $query = $this->em->createNativeQuery($sql, $rsm);
45
        $query->setParameter('game_id', $game->getId());
46
        $query->setParameter('total', $total);
47
        
48
        return $query->getResult();
49
    }
50
51
    public function queryByGame(\PlaygroundGame\Entity\Game $game)
52
    {
53
        $query = $this->em->createQuery('SELECT e FROM PlaygroundGame\Entity\Entry e WHERE e.game = :game');
54
        $query->setParameter('game', $game);
55
        return $query;
56
    }
57
58
    public function findByGameId($game)
59
    {
60
        return $this->getEntityRepository()->findBy(array('game' => $game));
61
    }
62
63
    /**
64
     * Get all the entries of the player except those offered as bonus
65
     *
66
     * @param unknown_type $game
67
     * @param unknown_type $user
68
     */
69 View Code Duplication
    public function findLastEntriesByUser($game, $user, $dateLimit)
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...
70
    {
71
        $query = $this->em->createQuery(
72
            'SELECT COUNT(e.id) FROM PlaygroundGame\Entity\Entry e 
73
             WHERE e.user = :user AND e.game = :game AND (e.bonus = 0 OR e.bonus IS NULL) AND e.created_at >= :date'
74
        );
75
        $query->setParameter('user', $user);
76
        $query->setParameter('game', $game);
77
        $query->setParameter('date', $dateLimit);
78
79
        $total = $query->getSingleScalarResult();
80
81
        return $total;
82
    }
83
    
84 View Code Duplication
    public function findLastEntriesByAnonymousIdentifier($game, $anonymousIdentifier, $dateLimit)
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...
85
    {
86
    
87
        $query = $this->em->createQuery(
88
            'SELECT COUNT(e.id) FROM PlaygroundGame\Entity\Entry e 
89
             WHERE e.anonymousIdentifier = :anonymousIdentifier AND e.game = :game 
90
             AND (e.bonus = 0 OR e.bonus IS NULL) AND e.created_at >= :date'
91
        );
92
        $query->setParameter('anonymousIdentifier', $anonymousIdentifier);
93
        $query->setParameter('game', $game);
94
        $query->setParameter('date', $dateLimit);
95
    
96
        $total = $query->getSingleScalarResult();
97
98
        return $total;
99
    }
100
101 View Code Duplication
    public function findLastEntriesByIp($game, $ip, $dateLimit)
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...
102
    {
103
    
104
        $query = $this->em->createQuery(
105
            'SELECT COUNT(e.id) FROM PlaygroundGame\Entity\Entry e 
106
             WHERE e.ip = :ip AND e.game = :game AND (e.bonus = 0 OR e.bonus IS NULL) AND e.created_at >= :date'
107
        );
108
        $query->setParameter('ip', $ip);
109
        $query->setParameter('game', $game);
110
        $query->setParameter('date', $dateLimit);
111
    
112
        $total = $query->getSingleScalarResult();
113
    
114
        return $total;
115
    }
116
117
    /**
118
     * get users with only one participation able to
119
     * replay the game in the timeframe (I except offered entries marked as bonus)
120
     *
121
     * @param unknown_type $game
122
     */
123
    public function findPlayersWithOneEntryBy($game, $dateLimit)
124
    {
125
        $query = $this->em->createQuery(
126
            'SELECT e, u FROM PlaygroundGame\Entity\Entry e
127
            JOIN e.user u
128
            WHERE e.game = :game
129
                AND (e.bonus = 0 OR e.bonus IS NULL)
130
            GROUP BY e.user
131
            HAVING COUNT(e.id) = 1
132
            AND e.created_at <= :date '
133
        );
134
        $query->setParameter('game', $game);
135
        $query->setParameter('date', $dateLimit);
136
137
        $result = $query->getResult();
138
139
        return $result;
140
    }
141
142
    /**
143
     * Compte les nombre de participations bonus
144
     * @param unknown_type $game
145
     * @param unknown_type $user
146
     */
147
    public function checkBonusEntry($game, $user)
148
    {
149
        $query = $this->em->createQuery(
150
            'SELECT COUNT(e.id) FROM PlaygroundGame\Entity\Entry e 
151
             WHERE e.user = :user AND e.game = :game AND (e.bonus = 0 OR e.bonus IS NULL)'
152
        );
153
        $query->setParameter('user', $user);
154
        $query->setParameter('game', $game);
155
        $nbEntries = $query->getSingleScalarResult();
156
157
        $query = $this->em->createQuery(
158
            'SELECT COUNT(e.id) FROM PlaygroundGame\Entity\Entry e 
159
             WHERE e.user = :user AND e.game = :game AND e.bonus = 1'
160
        );
161
        $query->setParameter('user', $user);
162
        $query->setParameter('game', $game);
163
        $nbBonusEntries = $query->getSingleScalarResult();
164
165
        if (($nbEntries - $nbBonusEntries) <= 0) {
166
            return false;
167
        }
168
169
        return true;
170
    }
171
172
    public function getEntityRepository()
173
    {
174
        if (null === $this->er) {
175
            $this->er = $this->em->getRepository('PlaygroundGame\Entity\Entry');
176
        }
177
178
        return $this->er;
179
    }
180
}
181