Completed
Push — develop ( 7bb4ed...37ee4f )
by greg
07:05
created

Entry::draw()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 33
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 33
rs 8.8571
cc 1
eloc 11
nc 1
nop 3
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
     * @param unknown_type $limitScale
0 ignored issues
show
Bug introduced by
There is no parameter named $limitScale. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

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