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

MainController::indexAction()   B

Complexity

Conditions 4
Paths 3

Size

Total Lines 38
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 38
ccs 17
cts 17
cp 1
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 24
nc 3
nop 4
crap 4
1
<?php
2
3
namespace Skobkin\Bundle\PointToolsBundle\Controller;
4
5
use Doctrine\ORM\EntityManager;
6
use Skobkin\Bundle\PointToolsBundle\Form\UserSearchType;
7
use Skobkin\Bundle\PointToolsBundle\Repository\{SubscriptionEventRepository, SubscriptionRepository, UserRepository};
8
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
9
use Symfony\Component\Form\FormError;
10
use Symfony\Component\HttpFoundation\{JsonResponse, Request, Response};
11
12
class MainController extends AbstractController
13
{
14
    const AJAX_AUTOCOMPLETE_SIZE = 10;
15
16
    /** @var int */
17 4
    private $appUserId;
18
19
    /** @var string */
20 4
    private $appUserLogin;
21
22 4
    public function __construct(int $appUserId, string $appUserLogin)
23 4
    {
24 4
        $this->appUserId = $appUserId;
25
        $this->appUserLogin = $appUserLogin;
26 4
    }
27
28
    public function indexAction(
29
        Request $request,
30 4
        UserRepository $userRepository,
31
        SubscriptionRepository $subscriptionRepository,
32 4
        SubscriptionEventRepository $subscriptionEventRepository
33 2
    ): Response {
34
        /** @var EntityManager $em */
35 2
        $em = $this->getDoctrine()->getManager();
0 ignored issues
show
Unused Code introduced by
$em is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
36 1
37
        $form = $this->createForm(
38
            UserSearchType::class,
39 1
            null,
40
            [
41
                'action' => $this->generateUrl('index'),
42 4
            ]
43 4
        );
44 4
45 4
        $form->handleRequest($request);
46 4
47 4
        if ($form->isSubmitted() && $form->isValid()) {
48 4
            $login = $form->get('login')->getData();
49
50
            if (null !== $user = $userRepository->findOneBy(['login' => $login])) {
51
                return $this->redirectToRoute('user_show', ['login' => $login]);
52
            }
53
54
            $form->get('login')->addError(new FormError('Login not found'));
55
        }
56
57
        return $this->render('SkobkinPointToolsBundle:Main:index.html.twig', [
58
            'form' => $form->createView(),
59 2
            'autocomplete_size' => self::AJAX_AUTOCOMPLETE_SIZE,
60
            'users_count' => $userRepository->getUsersCount(),
61 2
            'subscribers_count' => $subscriptionRepository->getUserSubscribersCountById($this->appUserId),
62
            'events_count' => $subscriptionEventRepository->getLastDayEventsCount(),
63 2
            'service_login' => $this->appUserLogin,
64
        ]);
65 2
    }
66 1
67 1
    /**
68 1
     * Returns user search autocomplete data in JSON
69
     *
70
     * @param string $login
71
     *
72 2
     * @return JsonResponse
73
     */
74
    public function searchUserAjaxAction(string $login, UserRepository $userRepository): Response
75
    {
76
        $em = $this->getDoctrine()->getManager();
0 ignored issues
show
Unused Code introduced by
$em is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
77
78
        $result = [];
79
80
        foreach ($userRepository->findUsersLikeLogin($login, self::AJAX_AUTOCOMPLETE_SIZE) as $user) {
81
            $result[] = [
82
                'login' => $user->getLogin(),
83
                'name' => $user->getName(),
84
            ];
85
        }
86
87
        return $this->json($result);
88
    }
89
}
90