Completed
Push — master ( 2e0eb3...b2b032 )
by Pavel
07:23 queued 02:50
created

Registration::updateRegistrationUser()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 25
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 3
Bugs 0 Features 1
Metric Value
c 3
b 0
f 1
dl 0
loc 25
ccs 0
cts 16
cp 0
rs 8.8571
cc 3
eloc 16
nc 3
nop 2
crap 12
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('homepage'));
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($plainPassword)) {
0 ignored issues
show
Bug introduced by
The variable $plainPassword seems to never exist, and therefore empty should always return true. Did you maybe rename this variable?

This check looks for calls to isset(...) or empty() on variables that are yet undefined. These calls will always produce the same result and can be removed.

This is most likely caused by the renaming of a variable or the removal of a function/method parameter.

Loading history...
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('homepage'));
135
        }
136
137
        return ['form' => $form->createView()];
138
    }
139
}