Passed
Push — master ( 6819c5...c18963 )
by Luiz Kim
03:56 queued 02:01
created

SecurityController::token()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 38
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 24
dl 0
loc 38
rs 9.2248
c 0
b 0
f 0
cc 5
nc 5
nop 1
1
<?php
2
3
namespace ControleOnline\Controller;
4
5
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...
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 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...
8
use Symfony\Component\Routing\Annotation\Route;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Routing\Annotation\Route 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 ControleOnline\Entity\User;
10
use ControleOnline\Entity\People;
0 ignored issues
show
Bug introduced by
The type ControleOnline\Entity\People 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 App\Service\PeopleRoleService;
0 ignored issues
show
Bug introduced by
The type App\Service\PeopleRoleService 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
class SecurityController extends AbstractController
14
{
15
16
17
  public function __construct(
18
    private PeopleRoleService $roleService,
19
    private  EntityManagerInterface $manager,
20
  ) {
21
  }
22
23
  public function __invoke(Request $request)
24
  {
25
    /**
26
     * @var \ControleOnline\Entity\User
27
     */
28
    $user = $this->getUser();
29
30
    if ($user === null)
31
      return $this->json([
32
        'error' => 'User not found'
33
      ]);
34
35
    // get contact data from user
36
37
    $email  = '';
38
    $code   = '';
39
    $number = '';
40
41
    if ($user->getPeople()->getEmail()->count() > 0)
42
      $email = $user->getPeople()->getEmail()->first()->getEmail();
43
44
    if ($user->getPeople()->getPhone()->count() > 0) {
45
      $phone  = $user->getPeople()->getPhone()->first();
46
      $code   = $phone->getDdd();
47
      $number = $phone->getPhone();
48
    }
49
50
    return $this->json([
51
      'username' => $user->getUsername(),
52
      'roles'    => $user->getRoles(),
53
      'api_key'  => $user->getApiKey(),
54
      'people'   => $user->getPeople()->getId(),
55
      'mycompany'  => $this->getCompanyId($user),
56
      'realname' => $this->getUserRealName($user->getPeople()),
57
      'avatar'   => $user->getPeople()->getFile() ? '/files/download/' . $user->getPeople()->getFile()->getId() : null,
58
      'email'    => $email,
59
      'phone'    => sprintf('%s%s', $code, $number),
60
      'active'   => (int) $user->getPeople()->getEnabled(),
61
    ]);
62
  }
63
64
  private function getUserRealName(People $people): string
65
  {
66
    $realName = 'John Doe';
67
68
    if ($people->getPeopleType() == 'J')
69
      $realName = $people->getAlias();
70
71
    else {
72
      if ($people->getPeopleType() == 'F') {
73
        $realName  = $people->getName();
74
        $realName .= ' ' . $people->getAlias();
75
        $realName  = trim($realName);
76
      }
77
    }
78
79
    return $realName;
80
  }
81
82
  private function getCompany(User $user): ?People
83
  {
84
    $peopleLink = $this->manager->getRepository(People::class)->getPeopleLink($user->getPeople(), 'employee', 1);
85
    if ($peopleLink !== false && $peopleLink->getCompany() instanceof People)
86
      return $peopleLink->getCompany();
87
  }
88
89
  private function getCompanyId(User $user): ?int
90
  {
91
    $company = $this->getCompany($user);
92
    return $company ? $company->getId() : null;
93
  }
94
}
95