Completed
Pull Request — master (#164)
by Ihor
25:00 queued 13:13
created

UserController   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 107
Duplicated Lines 30.84 %

Coupling/Cohesion

Components 1
Dependencies 9

Test Coverage

Coverage 28.95%

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 33
loc 107
ccs 11
cts 38
cp 0.2895
rs 10
wmc 7
lcom 1
cbo 9

5 Methods

Rating   Name   Duplication   Size   Complexity  
A registerAction() 0 19 3
A loginUpdateAction() 12 12 1
A loginSocialAction() 12 12 1
A logoutAction() 0 13 1
A meAction() 9 9 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace AppBundle\Controller;
4
5
use AppBundle\Entity\User;
6
use AppBundle\Model\UserResponse;
7
use FOS\RestBundle\Controller\Annotations\Get;
8
use FOS\RestBundle\Controller\Annotations\Post;
9
use FOS\RestBundle\Controller\Annotations\RouteResource;
10
use FOS\RestBundle\View\View;
11
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
12
use Symfony\Component\HttpFoundation\Request;
13
use Symfony\Component\HttpKernel\Exception\HttpException;
14
15
/**
16
 * @RouteResource("User")
17
 */
18
class UserController extends Controller
19
{
20
    /**
21
     * @param Request $request
22
     *
23
     * @Post("/users/register")
24
     *
25
     * @return View
26
     */
27 3
    public function registerAction(Request $request): View
28
    {
29 3
        $apiKey = $request->headers->get('API-Key-Token');
30 3
        $user = $this->getUser();
31
32 3
        if (!$user && !$apiKey) {
33 1
            $userNew = $this->get('user_login')
34 1
                ->newUser();
35
36 1
            $userResponse = new UserResponse($userNew);
37
38 1
            return View::create($userResponse);
39
        }
40
41 2
        throw new HttpException(
42 2
            409,
43 2
            'You are already logged in. Logout first (/users/logout) and then login again'
44
        );
45
    }
46
47
    /**
48
     * @param Request $request
49
     *
50
     * @Post("/users/login/update")
51
     *
52
     * @return View
53
     */
54 View Code Duplication
    public function loginUpdateAction(Request $request): View
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
55
    {
56
        $user = $this->get('user_login')
57
            ->updateUser(
58
                $request->headers->get('API-Key-Token'),
59
                $request->getContent()
60
            );
61
62
        $userResponse = new UserResponse($user);
63
64
        return View::create($userResponse);
65
    }
66
67
    /**
68
     * @param Request $request
69
     *
70
     * @Post("/users/login/social")
71
     *
72
     * @return View
73
     */
74 View Code Duplication
    public function loginSocialAction(Request $request): View
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
75
    {
76
        $user = $this->get('user_login')
77
            ->loginSocialNetwork(
78
                $request->headers->get('API-Key-Token'),
79
                $request->getContent()
80
            );
81
82
        $userResponse = new UserResponse($user);
83
84
        return View::create($userResponse);
85
    }
86
87
    /**
88
     * @param Request $request
89
     *
90
     * @Post("/users/logout")
91
     *
92
     * @return View
93
     */
94
    public function logoutAction(Request $request): View
95
    {
96
        $apiKey = $request->headers->get('API-Key-Token');
97
        $em = $this->getDoctrine()->getManager();
98
99
        $user = $em->getRepository('AppBundle:User')
100
            ->findOneBy(['apiKey' => $apiKey]);
101
        $user->setApiKey(null);
102
103
        $em->flush();
104
105
        return View::create(null, 204);
106
    }
107
108
    /**
109
     * @param Request $request
110
     *
111
     * @Get("/users/me")
112
     *
113
     * @return View
114
     */
115 View Code Duplication
    public function meAction(Request $request): View
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
116
    {
117
        $user = $this->getDoctrine()->getRepository('AppBundle:User')
118
            ->findOneBy(['apiKey' => $request->headers->get('API-Key-Token')]);
119
120
        $userResponse = new UserResponse($user);
121
122
        return View::create($userResponse);
123
    }
124
}
125