Registration::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 2
Metric Value
c 2
b 0
f 2
dl 0
loc 17
ccs 9
cts 9
cp 1
rs 9.4285
cc 1
eloc 14
nc 1
nop 7
crap 1
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: device
5
 * Date: 23.02.16
6
 * Time: 13:03
7
 */
8
9
namespace AppBundle\Services;
10
11
12
use AppBundle\Entity\User;
13
use Symfony\Bridge\Doctrine\RegistryInterface;
14
use AppBundle\Form\UserType;
15
use AppBundle\Form\UpdateUserSocialNetType;
16
use Symfony\Component\Form\FormFactoryInterface;
17
use Symfony\Component\HttpFoundation\RedirectResponse;
18
use Symfony\Component\HttpFoundation\Request;
19
use Symfony\Component\HttpFoundation\Session\Session;
20
use Symfony\Component\Routing\RouterInterface;
21
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoder;
22
use Symfony\Component\Validator\Validator\ValidatorInterface;
23
24
/**
25
 * Class Registration
26
 * @package AppBundle\Services
27
 */
28
class Registration
29
{
30
    /**
31
     * @var RegistryInterface
32
     */
33
    protected $doctrine;
34
    /**
35
     * @var FormFactoryInterface
36
     */
37
    protected $formFactory;
38
    /**
39
     * @var RouterInterface
40
     */
41
    protected $router;
42
    /**
43
     * @var ValidatorInterface
44
     */
45
    protected $validator;
46
    /**
47
     * @var UserPasswordEncoder
48
     */
49
    protected $passwordEncoder;
50
51
    protected $template;
52
53
    protected $mailer;
54
    protected $session;
55
56
    /**
57
     * @param RegistryInterface $doctrine
58
     * @param FormFactoryInterface $formFactory
59
     * @param RouterInterface $router
60
     * @param ValidatorInterface $validator
61
     * @param UserPasswordEncoder $passwordEncoder
62
     */
63 1
    public function __construct(RegistryInterface $doctrine,
64
                                FormFactoryInterface $formFactory,
65
                                RouterInterface $router,
66
                                ValidatorInterface $validator,
67
                                UserPasswordEncoder $passwordEncoder,
68
                                MailerService $mailerService,
69
                                Session $sessionService
70
                            )
71
    {
72 1
        $this->doctrine = $doctrine;
73 1
        $this->formFactory = $formFactory;
74 1
        $this->router = $router;
75 1
        $this->validator = $validator;
76 1
        $this->passwordEncoder = $passwordEncoder;
77 1
        $this->mailer = $mailerService;
78 1
        $this->session = $sessionService;
79 1
    }
80
81
    /**
82
     * @param Request $request
83
     * @return array|RedirectResponse
84
     */
85 1
    public function registrationUser(Request $request)
86
    {
87 1
        $em = $this->doctrine->getManager();
88
89 1
        $user = new User();
90 1
        $form = $this->formFactory->create(UserType::class, $user);
91
92 1
        $form->handleRequest($request);
93
94 1
        if ($form->isValid()) {
95
96
            $password = $this->passwordEncoder
97
                ->encodePassword($user, $user->getPlainPassword());
98
            $user->setPassword($password);
99
100
            $hash = $this->mailer->sendMail($user->getEmail());
101
            $user->setHash($hash);
102
103
            $em->persist($user);
104
            $em->flush();
105
            $this->session->getFlashBag()->add('notice',
106
                'Confirm your email!!!');
107
108
            return new RedirectResponse($this->router->generate('choice_modules'));
109
        }
110
111 1
        return ['form' => $form->createView()];
112
    }
113
114
    public function updateRegistrationUser(Request $request, User $user)
115
    {
116
        $em = $this->doctrine->getManager();
117
        $form = $this->formFactory->create(UpdateUserSocialNetType::class, $user);
118
        $originalPassword = $user->getPassword();
119
        $form->handleRequest($request);
120
121
        if ($form->isValid()) {
122
            if (!empty($user->getPlainPassword())) {
123
                $password = $this->passwordEncoder
124
                    ->encodePassword($user, $user->getPlainPassword());
125
                $user->setPassword($password);
126
            } else {
127
                $user->setPassword($originalPassword);
128
            }
129
130
            $user->setIsActive(true);
131
132
            $em->flush();
133
134
            return new RedirectResponse($this->router->generate('choice_modules'));
135
        }
136
137
        return ['form' => $form->createView()];
138
    }
139
}