|
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(); |
|
|
|
|
|
|
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(); |
|
|
|
|
|
|
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
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.