Completed
Pull Request — master (#137)
by
unknown
11:18
created

CustomerController::loginAction()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 58
Code Lines 50

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 50
c 0
b 0
f 0
nc 6
nop 1
dl 0
loc 58
rs 8.7274

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace AppBundle\Controller;
4
5
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
6
use Symfony\Component\HttpFoundation\Request;
7
use AppBundle\Entity\Customer;
8
use Symfony\Component\HttpFoundation\JsonResponse;
9
10
class CustomerController extends Controller
11
{
12
    /**
13
     * @param Request $request
14
     * @return bool|JsonResponse
15
     */
16
    public function loginAction(Request $request)
17
    {
18
        $em = $this->getDoctrine()->getManager();
19
        $apiKeyFromRequest = $request->headers->get('API-Key-Token');
20
        $socialNetwork = $request->request->get('social_network');
21
        $socialToken = $request->request->get('social_token');
22
        $apiKey = uniqid('token_');
23
        $firstName = $request->request->get('first_name');
24
        $lastName = $request->request->get('last_name');
25
        $email = $request->request->get('email');
26
27
        $validatorResult = $this->get('service_customers_login_validator')
28
            ->resultOptions($this->getUser(), $apiKeyFromRequest, $socialNetwork, $firstName, $lastName, $email);
29
30
        switch ($validatorResult) {
31
            case 'social network true':
32
                $UserFacebook = $this->get('service_facebook_sdk')
33
                    ->getUserFacebook($socialToken);
34
                $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...
35
                    ->findOneByApiKey($apiKeyFromRequest);
36
                $userFindApiKey->setEmail($UserFacebook->getEmail());
37
                $userFindApiKey->setFacebookId($UserFacebook->getId());
38
                $userFindApiKey->setFirstName($UserFacebook->getFirstName());
39
                $userFindApiKey->setLastName($UserFacebook->getLastName());
40
                $userFindApiKey->setApiKey($apiKey);
41
                $em->flush();
42
43
                return new JsonResponse('customer');
44
                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...
45
            case 'Invalid email/first_name/last_name':
46
                return new JsonResponse('401');
47
                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...
48
            case 'customer input of the form':
49
                $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...
50
                    ->findOneByApiKey($apiKeyFromRequest);
51
                $userFindApiKey->setFirstName($firstName);
52
                $userFindApiKey->setLastName($lastName);
53
                $userFindApiKey->setEmail($email);
54
                $em->flush();
55
56
                return new JsonResponse('customer');
57
                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...
58
            case 'not valid apiKey':
59
                return new JsonResponse('403');
60
                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...
61
            case 'new customer':
62
                $customer = new Customer();
63
                $customer->setUsername('customer');
64
                $customer->setApiKey($apiKey);
65
                $em->persist($customer);
66
                $em->flush();
67
68
                return new JsonResponse('customer');
69
                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...
70
            default:
71
                return new JsonResponse('default');
72
        }
73
    }
74
}
75