Test Setup Failed
Push — master ( 584547...730ac2 )
by Alexey
14:04
created

UserController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Skobkin\Bundle\PointToolsBundle\Controller;
4
5
use Knp\Component\Pager\Paginator;
6
use Skobkin\Bundle\PointToolsBundle\DTO\{DailyEvents, TopUserDTO};
7
use Skobkin\Bundle\PointToolsBundle\Entity\User;
8
use Skobkin\Bundle\PointToolsBundle\Repository\{SubscriptionEventRepository, UserRenameEventRepository, UserRepository};
9
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
10
use Ob\HighchartsBundle\Highcharts\Highchart;
11
use Symfony\Component\HttpFoundation\{Request, Response};
12
use Symfony\Component\Translation\TranslatorInterface;
13
14
class UserController extends AbstractController
15
{
16
    /** @var TranslatorInterface */
17
    private $translator;
18
19 3
    public function __construct(TranslatorInterface $translator)
20
    {
21
        $this->translator = $translator;
22 3
    }
23
24
    /**
25 3
     * @param string $login
26
     */
27 3
    public function showAction(
28
        Request $request,
29
        string $login,
30
        SubscriptionEventRepository $subscriptionEventRepository,
31 3
        UserRepository $userRepository,
32
        UserRenameEventRepository $renameEventRepository,
33 3
        Paginator $paginator
34 3
    ): Response {
35 3
        /** @var User $user */
36 3
        $user = $userRepository->findUserByLogin($login);
37
38
        if (!$user) {
39 3
            throw $this->createNotFoundException('User ' . $login . ' not found.');
40
        }
41 3
42 3
        $subscriberEventsPagination = $paginator->paginate(
43 3
            $subscriptionEventRepository->createUserLastSubscribersEventsQuery($user),
44 3
            $request->query->getInt('page', 1),
45 3
            10
46 3
        );
47
48
        return $this->render('SkobkinPointToolsBundle:User:show.html.twig', [
49
            'user' => $user,
50 1
            'subscribers' => $userRepository->findUserSubscribersById($user->getId()),
51
            'subscriptions_log' => $subscriberEventsPagination,
52 1
            'rename_log' => $renameEventRepository->findBy(['user' => $user], ['date' => 'DESC'], 10),
53
        ]);
54 1
    }
55
56 1
    public function topAction(UserRepository $userRepository, SubscriptionEventRepository $subscriptionEventRepository): Response
57 1
    {
58 1
        $topUsers = $userRepository->getTopUsers();
59
        $eventsByDay = $subscriptionEventRepository->getLastEventsByDay();
60
61
        return $this->render('@SkobkinPointTools/User/top.html.twig', [
62
            'events_dynamic_chat' => $this->createEventsDynamicChart($eventsByDay),
0 ignored issues
show
Documentation introduced by
$eventsByDay is of type array<integer,object<Sko...ity\SubscriptionEvent>>, but the function expects a array<integer,object<DailyEvents>>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
63
            'top_chart' => $this->createTopUsersGraph($topUsers),
0 ignored issues
show
Documentation introduced by
$topUsers is of type array<integer,object<Sko...Bundle\DTO\TopUserDTO>>, but the function expects a array<integer,object<TopUserDTO>>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
64
        ]);
65
    }
66
67
    /**
68
     * @todo move to the service
69 1
     *
70
     * @param DailyEvents[] $eventsByDay
71 1
     */
72
    private function createEventsDynamicChart(array $eventsByDay = []): Highchart
73
    {
74 1
        $data = [];
75
76
        foreach ($eventsByDay as $dailyEvents) {
77
            $data[$dailyEvents->getDate()->format('d.m')] = $dailyEvents->getEventsCount();
78
        }
79 1
80 1
        return $this->createChart('eventschart', 'line', $data, 'Events by day', 'amount');
81 1
    }
82
83
    /**
84
     * @todo move to the service
85
     *
86 1
     * @param TopUserDTO[] $topUsers
87 1
     */
88
    private function createTopUsersGraph(array $topUsers = []): Highchart
89
    {
90
        $data = [];
91 1
92 1
        foreach ($topUsers as $topUser) {
93 1
            $data[$topUser->getLogin()] = $topUser->getSubscribersCount();
94 1
        }
95 1
96 1
        return $this->createChart('topchart', 'bar', $data, 'Top users', 'amount');
97 1
    }
98 1
99
    private function createChart(string $blockId, string $type, array $data, string $bottomLabel, string $amountLabel): Highchart
100
    {
101
        $chartData = [
102 1
            'keys' => [],
103 1
            'values' => [],
104
        ];
105 1
106
        // Preparing chart data
107
        foreach ($data as $key => $value) {
108
            $chartData['keys'][] = $key;
109
            $chartData['values'][] = $value;
110
        }
111
112
        // Chart
113
        $series = [[
114
            'name' => $this->translator->trans($amountLabel),
115
            'data' => $chartData['values'],
116
        ]];
117
118
        // Initializing chart
119
        $ob = new Highchart();
120
        $ob->chart->renderTo($blockId);
121
        $ob->chart->type($type);
122
        $ob->title->text($this->translator->trans($bottomLabel));
123
        $ob->xAxis->title(['text' => null]);
124
        $ob->xAxis->categories($chartData['keys']);
125
        $ob->yAxis->title(['text' => $this->translator->trans($amountLabel)]);
126
        $ob->plotOptions->bar([
127
            'dataLabels' => [
128
                'enabled' => true
129
            ]
130
        ]);
131
        $ob->series($series);
132
133
        return $ob;
134
    }
135
}
136