Completed
Push — master ( c73ac3...acbb41 )
by Serhii
34:37 queued 19:37
created

CustomerController   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 77
Duplicated Lines 31.17 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 3
Bugs 0 Features 1
Metric Value
c 3
b 0
f 1
dl 24
loc 77
rs 10
wmc 6
lcom 1
cbo 7

4 Methods

Rating   Name   Duplication   Size   Complexity  
A loginNewAction() 0 15 3
A loginUpdateAction() 12 12 1
A loginSocialAction() 12 12 1
A logoutAction() 0 13 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\Model\CustomerResponse;
6
use FOS\RestBundle\Controller\Annotations\Post;
7
use FOS\RestBundle\Controller\Annotations\RouteResource;
8
use FOS\RestBundle\View\View;
9
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
10
use Symfony\Component\HttpFoundation\Request;
11
12
/**
13
 * @RouteResource("Customer")
14
 */
15
class CustomerController extends Controller
16
{
17
    /**
18
     * @param Request $request
19
     * @Post("/customers/login/new")
20
     * @return View
21
     */
22
    public function loginNewAction(Request $request)
23
    {
24
        $apiKey = $request->headers->get('API-Key-Token');
25
        $user = $this->getUser();
26
        if (!$user && !$apiKey) {
27
            $customer = $this->get('customer_login')
28
                ->newCustomer();
29
            $customerResponse = new CustomerResponse($customer);
30
            return View::create($customerResponse);
31
        }
32
        $response = [
33
            '401' => 'Invalid API-Key-Token',
34
        ];
35
        return View::create($response, 401);
36
    }
37
38
    /**
39
     * @param Request $request
40
     * @Post("/customers/login/update")
41
     * @return View
42
     */
43 View Code Duplication
    public function loginUpdateAction(Request $request)
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...
44
    {
45
        $customer = $this->get('customer_login')
46
            ->updateCustomer(
47
                $request->headers->get('API-Key-Token'),
48
                $request->getContent()
49
            );
50
51
        $customerResponse = new CustomerResponse($customer);
52
53
        return View::create($customerResponse);
54
    }
55
56
    /**
57
     * @param Request $request
58
     * @Post("/customers/login/social")
59
     * @return View
60
     */
61 View Code Duplication
    public function loginSocialAction(Request $request)
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...
62
    {
63
        $customer = $this->get('customer_login')
64
            ->loginSocialNetwork(
65
                $request->headers->get('API-Key-Token'),
66
                $request->getContent()
67
            );
68
69
        $customerResponse = new CustomerResponse($customer);
70
71
        return View::create($customerResponse);
72
    }
73
74
    /**
75
     * @param Request $request
76
     * @return View
77
     */
78
    public function logoutAction(Request $request)
79
    {
80
        $apiKeyHead = $request->headers->get('API-Key-Token');
81
        $response = [
82
            '204' => 'Successful operation',
83
        ];
84
        $em = $this->getDoctrine()->getManager();
85
        $customer = $em->getRepository('AppBundle:Customer')
86
            ->findOneBy(['apiKey' => $apiKeyHead]);
87
        $customer->setApiKey(null);
88
        $em->flush();
89
        return View::create($response, 204);
90
    }
91
}
92