Completed
Branch develop (b9c805)
by Pavel
06:33
created

Registration::registrationUser()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 29
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2.814

Importance

Changes 3
Bugs 0 Features 3
Metric Value
c 3
b 0
f 3
dl 0
loc 29
ccs 7
cts 17
cp 0.4118
rs 8.8571
cc 2
eloc 17
nc 2
nop 1
crap 2.814
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->setIsReg(true);
0 ignored issues
show
Unused Code Comprehensibility introduced by
86% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
102
            $user->setHash($hash);
103
104
            $em->persist($user);
105
            $em->flush();
106
            $this->session->getFlashBag()->add('notice',
107
                'Confirm your email!!!');
108
109
            return new RedirectResponse($this->router->generate('homepage'));
110
        }
111
112 1
        return ['form' => $form->createView()];
113
    }
114
115
    public function updateRegistrationUser(Request $request, User $user)
116
    {
117
        $em = $this->doctrine->getManager();
118
        $form = $this->formFactory->create(UpdateUserSocialNetType::class, $user);
119
120
        $form->handleRequest($request);
121
122
        if ($form->isValid()) {
123
            $password = $this->passwordEncoder
124
                ->encodePassword($user, $user->getPlainPassword());
125
            $user->setPassword($password);
126
            $user->setIsActive(false);
127
            $user->setIsReg(true);
128
129
            $em->flush();
130
131
            return new RedirectResponse($this->router->generate('homepage'));
132
        }
133
134
        return ['form' => $form->createView()];
135
    }
136
}