Passed
Branchmaster (706933)
by Alexey
02:55
created

MainController   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 11

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
c 1
b 0
f 0
lcom 1
cbo 11
dl 0
loc 63
ccs 28
cts 28
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
B indexAction() 0 35 4
A searchUserAjaxAction() 0 15 2
1
<?php
2
3
namespace Skobkin\Bundle\PointToolsBundle\Controller;
4
5
use Doctrine\ORM\EntityManager;
6
use Skobkin\Bundle\PointToolsBundle\Form\UserSearchType;
7
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
8
use Symfony\Component\Form\FormError;
9
use Symfony\Component\HttpFoundation\JsonResponse;
10
use Symfony\Component\HttpFoundation\Request;
11
12
class MainController extends Controller
13
{
14
    const AJAX_AUTOCOMPLETE_SIZE = 10;
15
16 4
    public function indexAction(Request $request)
17
    {
18
        /** @var EntityManager $em */
19 4
        $em = $this->getDoctrine()->getManager();
20
21 4
        $form = $this->createForm(
22 4
            new UserSearchType(),
23 4
            null,
24
            [
25 4
                'action' => $this->generateUrl('index'),
26 4
                'method' => 'POST',
27
            ]
28
        );
29
30 4
        $form->handleRequest($request);
31
32 4
        if ($form->isSubmitted() && $form->isValid()) {
33 2
            $login = $form->get('login')->getData();
34
35 2
            if (null !== $user = $em->getRepository('SkobkinPointToolsBundle:User')->findOneBy(['login' => $login])) {
36 1
                return $this->redirectToRoute('user_show', ['login' => $login]);
37
            }
38
39 1
            $form->get('login')->addError(new FormError('Login not found'));
40
        }
41
42 4
        return $this->render('SkobkinPointToolsBundle:Main:index.html.twig', [
43 4
            'form' => $form->createView(),
44 4
            'autocomplete_size' => self::AJAX_AUTOCOMPLETE_SIZE,
45 4
            'users_count' => $em->getRepository('SkobkinPointToolsBundle:User')->getUsersCount(),
46 4
            'subscribers_count' => $em->getRepository('SkobkinPointToolsBundle:Subscription')->getUserSubscribersCountById($this->container->getParameter('point_id')),
47 4
            'events_count' => $em->getRepository('SkobkinPointToolsBundle:SubscriptionEvent')->getLastDayEventsCount(),
48 4
            'service_login' => $this->container->getParameter('point_login'),
49
        ]);
50
    }
51
52
    /**
53
     * Returns user search autocomplete data in JSON
54
     *
55
     * @param $login
56
     *
57
     * @return JsonResponse
58
     */
59 2
    public function searchUserAjaxAction($login)
60
    {
61 2
        $em = $this->getDoctrine()->getManager();
62
63 2
        $result = [];
64
65 2
        foreach ($em->getRepository('SkobkinPointToolsBundle:User')->findUsersLikeLogin($login, self::AJAX_AUTOCOMPLETE_SIZE) as $user) {
66 1
            $result[] = [
67 1
                'login' => $user->getLogin(),
68 1
                'name' => $user->getName(),
69
            ];
70
        }
71
72 2
        return new JsonResponse($result);
73
    }
74
}
75