Completed
Push — develop ( fa353b...53d6fd )
by greg
03:24
created

Entry::findPlayersWithOneEntryBy()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 1 Features 0
Metric Value
c 4
b 1
f 0
dl 0
loc 20
rs 9.4286
cc 1
eloc 8
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 getDateLimit($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
        return $dateLimit;
135
    }
136
137 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...
138
    {
139
        $dateLimit = $this->getDateLimit($limitScale);
140
    
141
        $query = $this->em->createQuery(
142
            'SELECT COUNT(e.id) FROM PlaygroundGame\Entity\Entry e 
143
             WHERE e.ip = :ip AND e.game = :game AND (e.bonus = 0 OR e.bonus IS NULL) AND e.created_at >= :date'
144
        );
145
        $query->setParameter('ip', $ip);
146
        $query->setParameter('game', $game);
147
        $query->setParameter('date', $dateLimit);
148
    
149
        $total = $query->getSingleScalarResult();
150
    
151
        return $total;
152
    }
153
154
    /**
155
     * get users with only one participation able to
156
     * replay the game in the timeframe (I except offered entries marked as bonus)
157
     *
158
     * @param unknown_type $game
159
     */
160
    public function findPlayersWithOneEntryBy($game)
161
    {
162
        $dateLimit = $this->getDateLimit($game->getPlayLimitScale());
163
164
        $query = $this->em->createQuery(
165
            'SELECT e, u FROM PlaygroundGame\Entity\Entry e
166
            JOIN e.user u
167
            WHERE e.game = :game
168
                AND (e.bonus = 0 OR e.bonus IS NULL)
169
            GROUP BY e.user
170
            HAVING COUNT(e.id) = 1
171
            AND e.created_at <= :date '
172
        );
173
        $query->setParameter('game', $game);
174
        $query->setParameter('date', $dateLimit);
175
176
        $result = $query->getResult();
177
178
        return $result;
179
    }
180
181
    /**
182
     * Compte les nombre de participations bonus
183
     * @param unknown_type $game
184
     * @param unknown_type $user
185
     */
186
    public function checkBonusEntry($game, $user)
187
    {
188
        $query = $this->em->createQuery(
189
            'SELECT COUNT(e.id) FROM PlaygroundGame\Entity\Entry e 
190
             WHERE e.user = :user AND e.game = :game AND (e.bonus = 0 OR e.bonus IS NULL)'
191
        );
192
        $query->setParameter('user', $user);
193
        $query->setParameter('game', $game);
194
        $nbEntries = $query->getSingleScalarResult();
195
196
        $query = $this->em->createQuery(
197
            'SELECT COUNT(e.id) FROM PlaygroundGame\Entity\Entry e 
198
             WHERE e.user = :user AND e.game = :game AND e.bonus = 1'
199
        );
200
        $query->setParameter('user', $user);
201
        $query->setParameter('game', $game);
202
        $nbBonusEntries = $query->getSingleScalarResult();
203
204
        if (($nbEntries - $nbBonusEntries) <= 0) {
205
            return false;
206
        }
207
208
        return true;
209
    }
210
211
    public function getEntityRepository()
212
    {
213
        if (null === $this->er) {
214
            $this->er = $this->em->getRepository('PlaygroundGame\Entity\Entry');
215
        }
216
217
        return $this->er;
218
    }
219
}
220