Completed
Push — master ( 4ea7f1...ba6dcf )
by Louis
14s
created

getFoyerStatisticsDashboardAction()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 31
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 31
rs 8.8571
c 0
b 0
f 0
cc 3
eloc 18
nc 4
nop 0
1
<?php
2
3
namespace KI\FoyerBundle\Controller;
4
5
use KI\CoreBundle\Controller\BaseController;
6
use Nelmio\ApiDocBundle\Annotation\ApiDoc;
7
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
8
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
9
use Symfony\Component\DependencyInjection\ContainerInterface;
10
use Symfony\Component\HttpFoundation\StreamedResponse;
11
12
class DefaultController extends BaseController
13
{
14
    public function setContainer(ContainerInterface $container = null)
15
    {
16
        parent::setContainer($container);
17
        $this->initialize('User', 'User');
18
    }
19
20
    /**
21
     * @ApiDoc(
22
     *  description="Retourne des statistiques générales sur le Foyer",
23
     *  statusCodes={
24
     *   200="Requête traitée avec succès",
25
     *   401="Une authentification est nécessaire pour effectuer cette action",
26
     *   403="Pas les droits suffisants pour effectuer cette action",
27
     *   503="Service temporairement indisponible ou en maintenance",
28
     *  },
29
     *  section="Foyer"
30
     * )
31
     * @Route("/statistics/foyer/dashboard")
32
     * @Method("GET")
33
     */
34
    public function getFoyerStatisticsDashboardAction()
35
    {
36
        $this->trust($this->isFoyerMember());
37
38
        $statistics = [
39
            'promoBalances' => [
40
                'labels' => [],
41
                'data' => [],
42
            ],
43
            'soldBeers' => [
44
                'labels' => [],
45
                'data' => [],
46
            ],
47
        ];
48
49
        $promoBalances = $this->manager->getRepository('KIFoyerBundle:Transaction')->getPromoBalances();
50
51
        foreach ($promoBalances as $promoBalance){
52
            $statistics['promoBalances']['labels'][] = trim($promoBalance['promo']);
53
            $statistics['promoBalances']['data'][] = round($promoBalance['promoBalance'], 2);
54
        }
55
56
        $soldBeers = $this->manager->getRepository('KIFoyerBundle:Transaction')->getSoldBeers();
57
58
        foreach ($soldBeers as $soldBeer){
59
            $statistics['soldBeers']['labels'][] = trim($soldBeer['name']);
60
            $statistics['soldBeers']['data'][] = $soldBeer['soldBeer'];
61
        }
62
63
        return $this->json($statistics);
64
    }
65
66
    /**
67
     * @ApiDoc(
68
     *  description="Retourne des statistiques Foyer de l'utilisateur",
69
     *  statusCodes={
70
     *   200="Requête traitée avec succès",
71
     *   401="Une authentification est nécessaire pour effectuer cette action",
72
     *   403="Pas les droits suffisants pour effectuer cette action",
73
     *   409="La requête ne peut être traitée à l’état actuel, problème de reconnaisance de nom",
74
     *   503="Service temporairement indisponible ou en maintenance",
75
     *  },
76
     *  section="Foyer"
77
     * )
78
     * @Route("/statistics/foyer/{slug}")
79
     * @Method("GET")
80
     */
81
    public function getFoyerStatisticsAction($slug)
82
    {
83
        $this->trust(!$this->is('EXTERIEUR'));
84
85
        $user = $this->findBySlug($slug);
86
87
        if (!$user->getStatsFoyer()) {
88
            return $this->json(null, 200);
89
        }
90
        $statistics = $this->manager->getRepository('KIFoyerBundle:Transaction')->getUserStatistics($user);
91
92
        return $this->json($statistics);
93
    }
94
95
    /**
96
     * @ApiDoc(
97
     *  description="Retourne des statistiques générales sur le Foyer",
98
     *  statusCodes={
99
     *   200="Requête traitée avec succès",
100
     *   401="Une authentification est nécessaire pour effectuer cette action",
101
     *   403="Pas les droits suffisants pour effectuer cette action",
102
     *   503="Service temporairement indisponible ou en maintenance",
103
     *  },
104
     *  section="Foyer"
105
     * )
106
     * @Route("/statistics/foyer")
107
     * @Method("GET")
108
     */
109
    public function getFoyerStatisticsMainAction()
110
    {
111
        $this->trust(!$this->is('EXTERIEUR'));
112
113
        $statistics = [
114
            'hallOfFame' => $this->manager->getRepository('KIFoyerBundle:Transaction')->getHallOfFame(),
115
        ];
116
117
        return $this->json($statistics);
118
    }
119
120
    /**
121
     * @ApiDoc(
122
     *  description="Retourne le csv des personnes ayant un compte foyer négatif",
123
     *  statusCodes={
124
     *   200="Requête traitée avec succès",
125
     *   401="Une authentification est nécessaire pour effectuer cette action",
126
     *   403="Pas les droits suffisants pour effectuer cette action",
127
     *   503="Service temporairement indisponible ou en maintenance",
128
     *  },
129
     *  section="Foyer"
130
     * )
131
     * @Route("/foyer/debts")
132
     * @Method("GET")
133
     */
134
    public function getFoyerDebtsAction()
135
    {
136
        $this->trust($this->isFoyerMember());
137
138
        $response = new StreamedResponse(function () {
139
            $results = $this->repository->getDebtsIterator();
140
            $handle = fopen('php://output', 'r+');
141
142
            fputcsv($handle, ['username', 'email', 'promo', 'firstName', 'lastName', 'balance']);
143
144
            foreach ($results as $row) {
145
                fputcsv($handle, $row[$results->key()]);
146
            }
147
148
            fclose($handle);
149
        });
150
151
        $response->headers->set('Content-Type', 'application/force-download');
152
        $response->headers->set('Content-Disposition', 'attachment; filename="dettes.csv"');
153
154
        return $response;
155
    }
156
157
    /**
158
     * @ApiDoc(
159
     *  description="Retourne le csv de la répartition de l'argent par promo",
160
     *  statusCodes={
161
     *   200="Requête traitée avec succès",
162
     *   401="Une authentification est nécessaire pour effectuer cette action",
163
     *   403="Pas les droits suffisants pour effectuer cette action",
164
     *   503="Service temporairement indisponible ou en maintenance",
165
     *  },
166
     *  section="Foyer"
167
     * )
168
     * @Route("/foyer/promo-balance")
169
     * @Method("GET")
170
     */
171
    public function getFoyerPromoBalanceAction()
172
    {
173
        $this->trust($this->isFoyerMember());
174
175
        $response = new StreamedResponse(function () {
176
            $results = $this->repository->getPromoBalance();
177
            $handle = fopen('php://output', 'r+');
178
179
            fputcsv($handle, ['promo', 'balance']);
180
181
            foreach ($results as $row) {
182
                fputcsv($handle, $row);
183
            }
184
185
            fclose($handle);
186
        });
187
188
        $response->headers->set('Content-Type', 'application/force-download');
189
        $response->headers->set('Content-Disposition', 'attachment; filename="promo-balance.csv"');
190
191
        return $response;
192
    }
193
}
194