Test Failed
Push — master ( 8544d9...bfc40d )
by Alexey
03:04
created

UserController::createEventsDynamicChart()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 35
Code Lines 23

Duplication

Lines 35
Ratio 100 %

Code Coverage

Tests 17
CRAP Score 2

Importance

Changes 0
Metric Value
dl 35
loc 35
ccs 17
cts 17
cp 1
rs 8.8571
c 0
b 0
f 0
cc 2
eloc 23
nc 2
nop 1
crap 2
1
<?php
2
3
namespace Skobkin\Bundle\PointToolsBundle\Controller;
4
5
use Doctrine\ORM\EntityManager;
6
use Skobkin\Bundle\PointToolsBundle\DTO\DailyEvents;
7
use Skobkin\Bundle\PointToolsBundle\DTO\TopUserDTO;
8
use Skobkin\Bundle\PointToolsBundle\Entity\User;
9
use Skobkin\Bundle\PointToolsBundle\Service\UserApi;
10
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
11
use Ob\HighchartsBundle\Highcharts\Highchart;
12
use Symfony\Component\HttpFoundation\Request;
13
use Symfony\Component\HttpFoundation\Response;
14
15
class UserController extends Controller
16
{
17
    /**
18
     * @param string $login
19 3
     */
20
    public function showAction(Request $request, string $login): Response
21
    {
22 3
        /** @var EntityManager $em */
23
        $em = $this->getDoctrine()->getManager();
24
25 3
        /** @var User $user */
26
        $user = $em->getRepository('SkobkinPointToolsBundle:User')->findUserByLogin($login);
27 3
28
        if (!$user) {
29
            throw $this->createNotFoundException('User ' . $login . ' not found.');
30
        }
31 3
32
        $paginator = $this->get('knp_paginator');
33 3
34 3
        $subscriberEventsPagination = $paginator->paginate(
35 3
            $em->getRepository('SkobkinPointToolsBundle:SubscriptionEvent')->createUserLastSubscribersEventsQuery($user),
36 3
            $request->query->getInt('page', 1),
37
            10
38
        );
39 3
40
        $userApi = $this->get('app.point.api_user');
41 3
42 3
        return $this->render('SkobkinPointToolsBundle:User:show.html.twig', [
43 3
            'user' => $user,
44 3
            'subscribers' => $em->getRepository('SkobkinPointToolsBundle:User')->findUserSubscribersById($user->getId()),
45 3
            'subscriptions_log' => $subscriberEventsPagination,
46 3
            'rename_log' => $em->getRepository('SkobkinPointToolsBundle:UserRenameEvent')->findBy(['user' => $user], ['date' => 'DESC'], 10),
47
            'avatar_url' => $userApi->getAvatarUrl($user, UserApi::AVATAR_SIZE_LARGE),
48
        ]);
49
    }
50 1
51
    public function topAction(): Response
52 1
    {
53
        $userRepo = $this->getDoctrine()->getManager()->getRepository('SkobkinPointToolsBundle:User');
54 1
        $subscriptionsRecordsRepo = $this->getDoctrine()->getManager()->getRepository('SkobkinPointToolsBundle:SubscriptionEvent');
55
        $topUsers = $userRepo->getTopUsers();
56 1
        $eventsByDay = $subscriptionsRecordsRepo->getLastEventsByDay();
57 1
58 1
        return $this->render('@SkobkinPointTools/User/top.html.twig', [
59
            //'top_users' => $topUsers,
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
60
            'events_dynamic_chat' => $this->createEventsDynamicChart($eventsByDay),
61
            'top_chart' => $this->createTopUsersGraph($topUsers),
62
        ]);
63
    }
64
65
    /**
66
     * @todo move to the service
67
     *
68
     * @param DailyEvents[] $eventsByDay
69 1
     */
70 View Code Duplication
    private function createEventsDynamicChart(array $eventsByDay = []): Highchart
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 1
    {
72
        $translator = $this->get('translator');
73
74 1
        $chartData = [
75
            'titles' => [],
76
            'events' => [],
77
        ];
78
79 1
        foreach ($eventsByDay as $day) {
80 1
            $chartData['titles'][] = $day->getDate()->format('d.m');
81 1
            $chartData['events'][] = $day->getEventsCount();
82
        }
83
84
        $series = [[
85
            'name' => $translator->trans('Events'),
86 1
            'data' => $chartData['events'],
87 1
        ]];
88
89
        $ob = new Highchart();
90
        $ob->chart->renderTo('eventschart');
91 1
        $ob->chart->type('line');
92 1
        $ob->title->text($translator->trans('Events by day'));
93 1
        $ob->xAxis->title(['text' => null]);
94 1
        $ob->xAxis->categories($chartData['titles']);
95 1
        $ob->yAxis->title(['text' => $translator->trans('amount')]);
96 1
        $ob->plotOptions->bar([
97 1
            'dataLabels' => [
98 1
                'enabled' => true,
99
            ]
100
        ]);
101
        $ob->series($series);
102 1
103 1
        return $ob;
104
    }
105 1
106
    /**
107
     * @todo move to the service
108
     *
109
     * @param TopUserDTO[] $topUsers
110
     */
111 View Code Duplication
    private function createTopUsersGraph(array $topUsers = []): Highchart
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...
112
    {
113
        $translator = $this->get('translator');
114
115
        $chartData = [
116
            'titles' => [],
117
            'subscribers' => [],
118
        ];
119
120
        // Preparing chart data
121
        foreach ($topUsers as $user) {
122
            $chartData['titles'][] = $user->getLogin();
123
            $chartData['subscribers'][] = $user->getSubscribersCount();
124
        }
125
126
        // Chart
127
        $series = [[
128
            'name' => $translator->trans('Subscribers'),
129
            'data' => $chartData['subscribers'],
130
        ]];
131
132
        // Initializing chart
133
        $ob = new Highchart();
134
        $ob->chart->renderTo('topchart');
135
        $ob->chart->type('bar');
136
        $ob->title->text($translator->trans('Top users'));
137
        $ob->xAxis->title(['text' => null]);
138
        $ob->xAxis->categories($chartData['titles']);
139
        $ob->yAxis->title(['text' => $translator->trans('amount')]);
140
        $ob->plotOptions->bar([
141
            'dataLabels' => [
142
                'enabled' => true
143
            ]
144
        ]);
145
        $ob->series($series);
146
147
        return $ob;
148
    }
149
}
150