Passed
Push — master ( a137f9...d5d520 )
by Stepan
04:26
created

DefaultController::cabinetAction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 24
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 16
dl 0
loc 24
rs 9.7333
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Application\Bundle\UserBundle\Controller;
4
5
use Application\Bundle\UserBundle\Entity\User;
6
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
7
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
8
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
9
use Symfony\Component\HttpFoundation\JsonResponse;
10
use Symfony\Component\HttpFoundation\Response;
11
use Symfony\Component\Validator\ConstraintViolation;
12
13
/**
14
 * Class DefaultController.
15
 */
16
class DefaultController extends Controller
17
{
18
    /**
19
     * @Route(path="/cabinet", name="cabinet")
20
     *
21
     * @Security("has_role('ROLE_USER')")
22
     *
23
     * @return Response
24
     */
25
    public function cabinetAction()
26
    {
27
        /** @var User $user */
28
        $user = $this->getUser();
29
30
        $userActiveEvents = $this->getDoctrine()
31
            ->getRepository('StfalconEventBundle:Event')
32
            ->getSortedUserWannaVisitEventsByActive($user, true, 'ASC');
33
34
        $userPastEvents = $this->getDoctrine()
35
            ->getRepository('StfalconEventBundle:Event')
36
            ->getSortedUserWannaVisitEventsByActive($user, false, 'DESC');
37
38
        // list of events for refferal url
39
        $allActiveEvents = $this->getDoctrine()
40
            ->getRepository('StfalconEventBundle:Event')
41
            ->findBy(['active' => true, 'adminOnly' => false]);
42
43
        return $this->render('@ApplicationUser/Default/cabinet.html.twig', [
44
            'user' => $user,
45
            'user_active_events' => $userActiveEvents,
46
            'user_past_events' => $userPastEvents,
47
            'events' => $allActiveEvents,
48
            'code' => $this->get('stfalcon_event.referral.service')->getReferralCode(),
49
        ]);
50
    }
51
52
    /**
53
     * @Route(path="/update-user-phone/{phoneNumber}", name="update_user_phone",
54
     *     methods={"POST"},
55
     *     options = {"expose"=true},
56
     *     condition="request.isXmlHttpRequest()")
57
     * @Security("has_role('ROLE_USER')")
58
     *
59
     * @param string $phoneNumber
60
     *
61
     * @return JsonResponse
62
     */
63
    public function updateUserPhoneAction($phoneNumber)
64
    {
65
        /** @var User $user */
66
        $user = $this->getUser();
67
        $userManager = $this->get('stfalcon.user_manager');
68
        $validator = $this->get('validator');
69
        $user->setPhone($phoneNumber);
70
        $errors = $validator->validate($user);
71
        /** @var ConstraintViolation $error */
72
        foreach ($errors as $error) {
73
            if ('name' === $error->getPropertyPath()) {
74
                $user->getName();
75
            } elseif ('surname' === $error->getPropertyPath()) {
76
                $user->getSurname();
77
            }
78
        }
79
80
        $errors = $validator->validate($user);
81
82
        if (count($errors) > 0) {
83
            $errorsString = (string) $errors;
84
85
            return new JsonResponse(['result' => true, 'error' => $errorsString]);
86
        }
87
88
        $userManager->updateUser($user);
89
90
        return new JsonResponse(['result' => true]);
91
    }
92
}
93