Failed Conditions
Push — issue#666 ( 82e9d5...91903a )
by Guilherme
08:00
created

StatisticsController::getNewUsersByService()   B

Complexity

Conditions 4
Paths 8

Size

Total Lines 38
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
cc 4
eloc 25
nc 8
nop 3
dl 0
loc 38
ccs 0
cts 26
cp 0
crap 20
rs 8.5806
c 0
b 0
f 0
1
<?php
2
3
namespace LoginCidadao\CoreBundle\Controller;
4
5
use LoginCidadao\OAuthBundle\Entity\ClientRepository;
6
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
7
use Symfony\Component\Routing\Annotation\Route;
8
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
9
use Symfony\Component\HttpFoundation\Request;
10
use LoginCidadao\BadgesControlBundle\Model\Badge;
11
use Symfony\Component\HttpFoundation\JsonResponse;
12
use JMS\Serializer\SerializationContext;
13
14
/**
15
 * @Route("/statistics")
16
 */
17
class StatisticsController extends Controller
18
{
19
20
    /**
21
     * @Route("/", name="lc_statistics")
22
     * @Template()
23
     */
24
    public function indexAction(Request $request)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

24
    public function indexAction(/** @scrutinizer ignore-unused */ Request $request)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
25
    {
26
        return true;
27
    }
28
29
    /**
30
     * @Route("/users/badges", name="lc_statistics_user_badge")
31
     * @Template()
32
     */
33
    public function usersByBadgesAction()
34
    {
35
        $badgesHandler = $this->get('badges.handler');
36
37
        $badges = $badgesHandler->getAvailableBadges();
38
        foreach ($badges as $client => $badge) {
39
            foreach ($badge as $name => $desc) {
40
                $filterBadge     = new Badge($client, $name);
41
                $count           = $badgesHandler->countBearers($filterBadge);
42
                $b               = array_shift($count);
43
                $data[$client][] = $b;
44
            }
45
        }
46
47
        $em         = $this->getDoctrine()->getManager();
48
        $repo       = $em->getRepository('LoginCidadaoCoreBundle:Person');
49
        $totalUsers = $repo->getCountAll();
50
51
        return array("data" => $data, "totalUsers" => $totalUsers['qty']);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $data does not seem to be defined for all execution paths leading up to this point.
Loading history...
52
    }
53
54
    /**
55
     * @Route("/users/region/{type}", name="lc_statistics_user_region")
56
     * @Template()
57
     */
58
    public function usersByRegionAction($type)
59
    {
60
        $em   = $this->getDoctrine()->getManager();
61
        $repo = $em->getRepository('LoginCidadaoCoreBundle:Person');
62
        if ($type == "country") {
63
            $data = $repo->getCountByCountry();
64
        } else {
65
            $data = $repo->getCountByState();
66
        }
67
68
        $totalUsers = $repo->getCountAll();
69
70
71
        return $this->render('LoginCidadaoCoreBundle:Statistics:usersByRegion.html.twig',
72
                                array('data' => $data, 'totalUsers' => $totalUsers));
73
    }
74
75
    /**
76
     * @Route("/users/city/{stateId}", name="lc_statistics_user_city")
77
     * @Template()
78
     */
79
    public function usersByCityAction($stateId)
80
    {
81
        $em   = $this->getDoctrine()->getManager();
82
        $repo = $em->getRepository('LoginCidadaoCoreBundle:Person');
83
        $data = $repo->getCountByCity($stateId);
84
85
        return $this->render('LoginCidadaoCoreBundle:Statistics:usersByCity.html.twig',
86
                                array('data' => $data));
87
    }
88
89
    /**
90
     * @Route("/users/services", name="lc_statistics_user_services")
91
     * @Template()
92
     */
93
    public function usersByServicesAction(Request $request)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

93
    public function usersByServicesAction(/** @scrutinizer ignore-unused */ Request $request)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
94
    {
95
        /** @var ClientRepository $repo */
96
        $repo   = $this->getDoctrine()
97
            ->getRepository('LoginCidadaoOAuthBundle:Client');
98
        $totals = $repo->getCountPerson($this->getUser());
99
100
        $keys = array();
101
        foreach ($totals as $total) {
102
            $client = $total['client'];
103
            $keys[] = $client->getId();
104
        }
105
        $evoData = $this->getStatsHandler()->getIndexed('agg.client.users', $keys, 30);
106
107
        $context    = SerializationContext::create()->setGroups('date');
108
        $serializer = $this->get('jms_serializer');
109
        $evo        = $serializer->serialize($evoData, 'json', $context);
110
111
        return compact('totals', 'evo');
112
    }
113
114
    /**
115
     * @Route("/services/users-by-day.{_format}",
116
     *          name="lc_statistics_service_users_day",
117
     *          defaults={"_format": "html", "clientId": null}
118
     * )
119
     * @Template()
120
     */
121
    public function usersByServiceByDayAction(Request $request, $clientId = null)
122
    {
123
        $data = $this->getNewUsersByService(30, $clientId,
124
            $request->getRequestFormat());
125
126
        if ($request->getRequestFormat() === 'json') {
127
            return new JsonResponse($data);
128
        }
129
130
        return compact('data');
131
    }
132
133
    /**
134
     * @Route("/services/users-by-day-week.{_format}",
135
     *          name="lc_statistics_service_users_day_week",
136
     *          defaults={"_format": "html", "clientId": null}
137
     * )
138
     * @Template()
139
     */
140
    public function usersByServiceByDayOfWeekAction(Request $request)
141
    {
142
        $em      = $this->getDoctrine()->getManager();
143
        $repo    = $em->getRepository('LoginCidadaoCoreBundle:Authorization');
144
        $rawData = $repo->statsUsersByServiceByDayOfWeek();
145
146
        if ($request->getRequestFormat() === 'json') {
147
            return new JsonResponse($rawData);
148
        }
149
150
        $data = $rawData;
151
152
        return compact('data');
153
    }
154
155
    private function getNewUsersByService($days, $clientId = null,
156
                                            $format = 'html')
157
    {
158
        $em        = $this->getDoctrine()->getManager();
159
        $repo      = $em->getRepository('LoginCidadaoOAuthBundle:Client');
160
        $rawData   = $repo->statsUsersByServiceByDay($days, $clientId,
161
            $this->getUser());
162
        $rawTotals = $repo->getCountPerson($this->getUser(), $clientId);
163
164
        if ($format === 'json') {
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
165
            //return $rawData;
166
        }
167
168
        $totals = array();
169
        foreach ($rawTotals as $entry) {
170
            $client = $entry['client'];
171
172
            $totals[$client->getId()] = array('client' => $client, 'total' => $entry['qty']);
173
        }
174
175
        $data  = array();
176
        $total = array();
0 ignored issues
show
Unused Code introduced by
The assignment to $total is dead and can be removed.
Loading history...
177
        foreach ($rawData as $stat) {
178
            $id     = $stat['client'];
179
            $client = $totals[$id]['client'];
180
            $total  = $totals[$id]['total'];
181
            $count  = $stat['users'];
182
            $day    = $stat['day'];
183
            @$totalPeriod[$id] += $count;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $totalPeriod does not exist. Did you maybe mean $total?
Loading history...
184
185
            $data[$id]['client']       = $client;
186
            $data[$id]['total_period'] = $totalPeriod[$id];
187
            $data[$id]['total']        = $total;
188
            $data[$id]['days']         = $days;
189
            $data[$id]['data'][]       = array($day, $count);
190
        }
191
192
        return $data;
193
    }
194
195
    /**
196
     * @return \LoginCidadao\StatsBundle\Handler\StatsHandler
197
     */
198
    private function getStatsHandler()
199
    {
200
        return $this->get('statistics.handler');
201
    }
202
}
203