Completed
Pull Request — master (#254)
by greg
03:06
created

Entry::findPlayersWithOneEntryBy()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 50
Code Lines 37

Duplication

Lines 50
Ratio 100 %

Importance

Changes 3
Bugs 1 Features 0
Metric Value
c 3
b 1
f 0
dl 50
loc 50
rs 8.6316
cc 6
eloc 37
nc 6
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, $limitScale)
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
        $now = new \DateTime("now");
104
        switch ($limitScale) {
105
            case 'always':
106
                $interval = 'P100Y';
107
                $now->sub(new \DateInterval($interval));
108
                $dateLimit = $now->format('Y-m-d') . ' 0:0:0';
109
                break;
110
            case 'day':
111
                $dateLimit = $now->format('Y-m-d') . ' 0:0:0';
112
                break;
113
            case 'week':
114
                $interval = 'P7D';
115
                $now->sub(new \DateInterval($interval));
116
                $dateLimit = $now->format('Y-m-d') . ' 0:0:0';
117
                break;
118
            case 'month':
119
                $interval = 'P1M';
120
                $now->sub(new \DateInterval($interval));
121
                $dateLimit = $now->format('Y-m-d') . ' 0:0:0';
122
                break;
123
            case 'year':
124
                $interval = 'P1Y';
125
                $now->sub(new \DateInterval($interval));
126
                $dateLimit = $now->format('Y-m-d') . ' 0:0:0';
127
                break;
128
            default:
129
                $interval = 'P100Y';
130
                $now->sub(new \DateInterval($interval));
131
                $dateLimit = $now->format('Y-m-d') . ' 0:0:0';
132
        }
133
    
134
        $query = $this->em->createQuery(
135
            'SELECT COUNT(e.id) FROM PlaygroundGame\Entity\Entry e 
136
             WHERE e.ip = :ip AND e.game = :game AND (e.bonus = 0 OR e.bonus IS NULL) AND e.created_at >= :date'
137
        );
138
        $query->setParameter('ip', $ip);
139
        $query->setParameter('game', $game);
140
        $query->setParameter('date', $dateLimit);
141
    
142
        $total = $query->getSingleScalarResult();
143
    
144
        return $total;
145
    }
146
147
    /**
148
     * get users with only one participation able to
149
     * replay the game in the timeframe (I except offered entries marked as bonus)
150
     *
151
     * @param unknown_type $game
152
     */
153 View Code Duplication
    public function findPlayersWithOneEntryBy($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...
154
    {
155
        $now = new \DateTime("now");
156
        $limitScale = $game->getPlayLimitScale();
157
        switch ($limitScale) {
158
            case 'always':
159
                $interval = 'P100Y';
160
                $now->sub(new \DateInterval($interval));
161
                $dateLimit = $now->format('Y-m-d') . ' 0:0:0';
162
                break;
163
            case 'day':
164
                $dateLimit = $now->format('Y-m-d') . ' 0:0:0';
165
                break;
166
            case 'week':
167
                $interval = 'P7D';
168
                $now->sub(new \DateInterval($interval));
169
                $dateLimit = $now->format('Y-m-d') . ' 0:0:0';
170
                break;
171
            case 'month':
172
                $interval = 'P1M';
173
                $now->sub(new \DateInterval($interval));
174
                $dateLimit = $now->format('Y-m-d') . ' 0:0:0';
175
                break;
176
            case 'year':
177
                $interval = 'P1Y';
178
                $now->sub(new \DateInterval($interval));
179
                $dateLimit = $now->format('Y-m-d') . ' 0:0:0';
180
                break;
181
            default:
182
                $interval = 'P100Y';
183
                $now->sub(new \DateInterval($interval));
184
                $dateLimit = $now->format('Y-m-d') . ' 0:0:0';
185
        }
186
187
        $query = $this->em->createQuery(
188
            'SELECT e, u FROM PlaygroundGame\Entity\Entry e
189
            JOIN e.user u
190
            WHERE e.game = :game
191
                AND (e.bonus = 0 OR e.bonus IS NULL)
192
            GROUP BY e.user
193
            HAVING COUNT(e.id) = 1
194
            AND e.created_at <= :date '
195
        );
196
        $query->setParameter('game', $game);
197
        $query->setParameter('date', $dateLimit);
198
199
        $result = $query->getResult();
200
201
        return $result;
202
    }
203
204
    /**
205
     * Compte les nombre de participations bonus
206
     * @param unknown_type $game
207
     * @param unknown_type $user
208
     */
209
    public function checkBonusEntry($game, $user)
210
    {
211
        $query = $this->em->createQuery(
212
            'SELECT COUNT(e.id) FROM PlaygroundGame\Entity\Entry e 
213
             WHERE e.user = :user AND e.game = :game AND (e.bonus = 0 OR e.bonus IS NULL)'
214
        );
215
        $query->setParameter('user', $user);
216
        $query->setParameter('game', $game);
217
        $nbEntries = $query->getSingleScalarResult();
218
219
        $query = $this->em->createQuery(
220
            'SELECT COUNT(e.id) FROM PlaygroundGame\Entity\Entry e 
221
             WHERE e.user = :user AND e.game = :game AND e.bonus = 1'
222
        );
223
        $query->setParameter('user', $user);
224
        $query->setParameter('game', $game);
225
        $nbBonusEntries = $query->getSingleScalarResult();
226
227
        if (($nbEntries - $nbBonusEntries) <= 0) {
228
            return false;
229
        }
230
231
        return true;
232
    }
233
234
    public function getEntityRepository()
235
    {
236
        if (null === $this->er) {
237
            $this->er = $this->em->getRepository('PlaygroundGame\Entity\Entry');
238
        }
239
240
        return $this->er;
241
    }
242
}
243