Completed
Push — master ( 096baa...85634b )
by Louis
26s queued 13s
created

TransactionRepository::getHallOfFame()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 26
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 26
rs 8.8571
c 1
b 0
f 0
cc 2
eloc 9
nc 2
nop 0
1
<?php
2
namespace KI\FoyerBundle\Repository;
3
4
use Doctrine\ORM\EntityRepository;
5
use KI\FoyerBundle\Entity\Transaction;
6
use KI\UserBundle\Entity\User;
7
8
class TransactionRepository extends EntityRepository
9
{
10
    /**
11
     * @return object[]
12
     */
13
    public function getHallOfFame()
14
    {
15
        $sept1 = strtotime("September 1st -1Year");
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal September 1st -1Year does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
16
17
        $hallOfFame = $this->getEntityManager()->createQuery('SELECT usr AS user, SUM(beer.volume) AS liters FROM
18
            KIUserBundle:User usr,
19
            KIFoyerBundle:Transaction transac, 
20
            KIFoyerBundle:Beer beer
21
            WHERE transac.user = usr
22
            AND transac.beer = beer            
23
            AND transac.beer IS NOT NULL
24
            AND usr.balance > 0
25
            AND transac.date > :schoolYear
26
            GROUP BY usr.id 
27
            ORDER BY liters DESC
28
            ')
29
            ->setParameter('schoolYear', $sept1)
30
            ->setMaxResults(10)
31
            ->getResult();
32
33
        foreach ($hallOfFame as &$data){
34
            $data['liters'] = round($data['liters'], 2);
35
        }
36
37
        return $hallOfFame;
38
    }
39
40
    /**
41
     * @return object[]
42
     */
43
    public function getPromoBalances()
44
    {
45
        $promoBalances = $this->getEntityManager()->createQuery('SELECT 
46
            SUM(usr.balance) AS promoBalance, usr.promo as promo FROM
47
            KIUserBundle:User usr
48
            GROUP BY promo
49
            ORDER BY promo ASC
50
            ')
51
            ->getArrayResult();
52
53
        foreach ($promoBalances as &$promoBalance){
54
            $promoBalance['promoBalance'] = round($promoBalance['promoBalance'], 2);
55
        }
56
57
        return $promoBalances;
58
    }
59
60
61
    /**
62
     * Retourne les statistiques d'un utilisateur particulier
63
     * @param  User $user
64
     * @return array
65
     */
66
    public function getUserStatistics(User $user)
67
    {
68
        $timeGraph = [];
69
        $pieGraph = [];
70
        $beersCountArray = [];
71
        $volume = 0;
72
        $beerCount = 0;
73
74
        $transactions = $this->findBy(['user' => $user], ['date' => 'ASC']);
75
76
        foreach ($transactions as $transaction) {
77
            $beer = $transaction->getBeer();
78
            // Compte crédité, pas une conso
79
            if ($beer === null) {
80
                continue;
81
            }
82
            $volume += $beer->getVolume();
83
84
            $timeGraph[$transaction->getDate()] = $volume;
85
86
            $beerCount++;
87
88
            if (!isset($pieGraph[$beer->getSlug()])) {
89
                $pieGraph[$beer->getSlug()] = ['beer' => $beer, 'count' => 0];
90
                $beersCountArray[$beer->getSlug()] = 0;
91
            }
92
            $pieGraph[$beer->getSlug()]['count']++;
93
            $beersCountArray[$beer->getSlug()]++;
94
        }
95
96
        array_multisort($beersCountArray, SORT_DESC, $pieGraph);
97
98
        return [
99
            'beersDrunk'    => $pieGraph,
100
            'stackedLiters' => $timeGraph,
101
            'totalLiters'   => $volume,
102
            'totalBeers'    => $beerCount
103
        ];
104
    }
105
}
106