Completed
Pull Request — master (#137)
by
unknown
14:38
created

CustomerController   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 10

Importance

Changes 2
Bugs 0 Features 1
Metric Value
dl 0
loc 73
rs 10
c 2
b 0
f 1
wmc 7
lcom 1
cbo 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B loginAction() 0 65 7
1
<?php
2
3
namespace AppBundle\Controller;
4
5
use AppBundle\Form\Type\CustomerType;
6
use FOS\RestBundle\View\View;
7
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
8
use Symfony\Component\HttpFoundation\Request;
9
use AppBundle\Entity\Customer;
10
use Symfony\Component\HttpFoundation\JsonResponse;
11
use FOS\RestBundle\Controller\Annotations\RouteResource;
12
13
/**
14
 * @RouteResource("Customer")
15
 */
16
class CustomerController extends Controller
17
{
18
    /**
19
     * @param Request $request
20
     *
21
     * @return JsonResponse|View
22
     */
23
    public function loginAction(Request $request)
24
    {
25
        $form = $this->createForm(CustomerType::class);
26
        $form->submit($request->request->all());
27
28
        if ($form->isValid()) {
29
            $em = $this->getDoctrine()->getManager();
30
            $apiKeyFromRequest = $request->headers->get('API-Key-Token');
31
            $socialNetwork = $request->request->get('socialNetwork');
32
            $socialToken = $request->request->get('socialToken');
33
            $apiKey = uniqid('token_');
34
            $firstName = $request->request->get('firstName');
35
            $lastName = $request->request->get('lastName');
36
            $email = $request->request->get('email');
37
38
            $validatorResult = $this->get('service_customers_login_validator')
39
                ->resultOptions($this->getUser(), $apiKeyFromRequest, $socialNetwork, $firstName, $lastName, $email);
40
41
            switch ($validatorResult) {
42
                case 'social network true':
43
                    $UserFacebook = $this->get('service_facebook_sdk')
44
                        ->getUserFacebook($socialToken);
45
                    $userFindApiKey = $em->getRepository('AppBundle:Customer')
0 ignored issues
show
Bug introduced by
The method findOneByApiKey() does not exist on Doctrine\Common\Persistence\ObjectRepository. Did you maybe mean findOneBy()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
46
                        ->findOneByApiKey($apiKeyFromRequest);
47
                    $userFindApiKey->setEmail($UserFacebook->getEmail());
48
                    $userFindApiKey->setFacebookId($UserFacebook->getId());
49
                    $userFindApiKey->setFirstName($UserFacebook->getFirstName());
50
                    $userFindApiKey->setLastName($UserFacebook->getLastName());
51
                    $userFindApiKey->setApiKey($apiKey);
52
                    $em->flush();
53
54
                    return new JsonResponse('customer social');
55
                    break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
56
                case 'Invalid email/first_name/last_name':
57
                    return new JsonResponse('401');
58
                    break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
59
                case 'customer input of the form':
60
                    $userFindApiKey = $em->getRepository('AppBundle:Customer')
0 ignored issues
show
Bug introduced by
The method findOneByApiKey() does not exist on Doctrine\Common\Persistence\ObjectRepository. Did you maybe mean findOneBy()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
61
                        ->findOneByApiKey($apiKeyFromRequest);
62
                    $userFindApiKey->setFirstName($firstName);
63
                    $userFindApiKey->setLastName($lastName);
64
                    $userFindApiKey->setEmail($email);
65
                    $em->flush();
66
67
                    return new JsonResponse('customer form');
68
                    break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
69
                case 'not valid apiKey':
70
                    return new JsonResponse('403');
71
                    break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
72
                case 'new customer':
73
                    $customer = new Customer();
74
                    $customer->setUsername('customer');
75
                    $customer->setApiKey($apiKey);
76
                    $em->persist($customer);
77
                    $em->flush();
78
79
                    return new JsonResponse('customer');
80
                    break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
81
                default:
82
                    return new JsonResponse('default');
83
            }
84
        }
85
86
        return View::create($form, 400);
87
    }
88
}
89