DefaultClientController   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Importance

Changes 6
Bugs 0 Features 0
Metric Value
eloc 37
dl 0
loc 62
rs 10
c 6
b 0
f 0
wmc 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A connectAction() 0 5 1
A __construct() 0 1 1
A returnAction() 0 46 5
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
    protected function returnAction(Request $request): JsonResponse
31
    {
32
        try {
33
34
            if ($request->get('code'))
35
                $token = $this->provider->getAccessToken('authorization_code', [
36
                    'code' => $request->get('code')
37
                ]);
38
39
            if ($request->get('access_token'))
40
                $token = new AccessToken([
41
                    'access_token' => $request->get('access_token'),
42
                ]);
43
44
            if ($request->get('code'))
45
                $token = $this->provider->getAccessToken('authorization_code', [
46
                    'code' => $request->get('code')
47
                ]);
48
49
            $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...
50
51
            $user = $this->userService->discoveryUser(
52
                $ownerDetails->getEmail(),
53
                md5(microtime()),
54
                $ownerDetails->getFirstName(),
55
                ''
56
            );
57
58
            $data = $this->userService->getUserSession($user);
59
60
            return new JsonResponse([
61
                'response' => [
62
                    'data'    => $data,
63
                    'count'   => 1,
64
                    'error'   => '',
65
                    'success' => true,
66
                ],
67
            ]);
68
        } catch (Exception $e) {
69
70
            return new JsonResponse([
71
                'response' => [
72
                    'data'    => [],
73
                    'count'   => 0,
74
                    'error'   => $e->getMessage(),
75
                    'success' => false,
76
                ],
77
            ]);
78
        }
79
    }
80
}
81