Passed
Push — master ( 3e0e76...bf30a1 )
by Luiz Kim
02:15
created

DefaultClientController::returnAction()   B

Complexity

Conditions 6
Paths 70

Size

Total Lines 55
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 36
c 0
b 0
f 0
dl 0
loc 55
rs 8.7217
cc 6
nc 70
nop 1

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 ControleOnline\Controller\Oauth;
4
5
use ControleOnline\Service\UserService;
6
use Doctrine\ORM\EntityManagerInterface;
0 ignored issues
show
Bug introduced by
The type Doctrine\ORM\EntityManagerInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
7
use Exception;
8
use League\OAuth2\Client\Token\AccessToken;
0 ignored issues
show
Bug introduced by
The type League\OAuth2\Client\Token\AccessToken was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
9
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
0 ignored issues
show
Bug introduced by
The type Symfony\Bundle\Framework...ller\AbstractController was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
10
use Symfony\Component\HttpFoundation\Request;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\HttpFoundation\Request was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
11
use Symfony\Component\HttpFoundation\JsonResponse;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\HttpFoundation\JsonResponse was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
12
13
14
class DefaultClientController extends AbstractController
15
{
16
17
    protected $clientId;
18
    protected $clientSecret;
19
    protected $provider;
20
21
    public function __construct(protected EntityManagerInterface $manager, protected UserService $userService) {}
22
23
    protected function connectAction()
24
    {
25
        $authUrl = $this->provider->getAuthorizationUrl();
26
        header('Location: ' . $authUrl);
27
        exit;
28
    }
29
30
31
32
    protected function returnAction(Request $request): JsonResponse
33
    {
34
        try {
35
36
37
            if ($request->get('code'))
38
                $token = $this->provider->getAccessToken('authorization_code', [
39
                    'code' => $request->get('code')
40
                ]);
41
42
            if ($request->get('access_token'))
43
                $token = new AccessToken([
44
                    'access_token' => $request->get('access_token'),
45
                ]);
46
47
            if ($request->get('code'))
48
                $token = $this->provider->getAccessToken('authorization_code', [
49
                    'code' => $request->get('code')
50
                ]);
51
52
            $ownerDetails = $this->provider->getResourceOwner($token);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $token does not seem to be defined for all execution paths leading up to this point.
Loading history...
53
54
            $user = $this->userService->discoveryUser($ownerDetails->getEmail(), md5(microtime()), $ownerDetails->getFirstName(), $ownerDetails->getLasttName());
55
56
            $data = [
57
                'id'        => $user->getPeople()->getId(),
58
                'username'  => $user->getUsername(),
59
                'roles'     => $user->getRoles(),
60
                'api_key'   => $user->getApiKey(),
61
                'people'    => $user->getPeople()->getId(),
62
                'mycompany' => $this->userService->getCompanyId($user),
63
                'realname'  => $ownerDetails->getFirstName(),
64
                'avatar'    => $user->getPeople()->getImage() ? '/files/' . $user->getPeople()->getImage()->getId() . '/download' : null,
65
                'email'     => $ownerDetails->getEmail(),
66
                'active'    => (int) $user->getPeople()->getEnabled(),
67
            ];
68
69
70
71
            return new JsonResponse([
72
                'response' => [
73
                    'data'    => $data,
74
                    'count'   => 1,
75
                    'error'   => '',
76
                    'success' => true,
77
                ],
78
            ]);
79
        } catch (Exception $e) {
80
81
            return new JsonResponse([
82
                'response' => [
83
                    'data'    => [],
84
                    'count'   => 0,
85
                    'error'   => $e->getMessage(),
86
                    'success' => false,
87
                ],
88
            ]);
89
        }
90
    }
91
92
  
93
}
94