Passed
Push — master ( 81d6bc...6819c5 )
by Luiz Kim
02:09
created

SecurityController   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 14
eloc 39
dl 0
loc 83
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A token() 0 38 5
A getUserRealName() 0 16 3
A getCompanyId() 0 4 2
A getCompany() 0 5 3
A __construct() 0 4 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
  /**
24
   * @Route("/token", name="auth_token", methods={"POST"})
25
   */
26
  public function token(Request $request)
27
  {
28
    /**
29
     * @var \ControleOnline\Entity\User
30
     */
31
    $user = $this->getUser();
32
33
    if ($user === null)
34
      return $this->json([
35
        'error' => 'User not found'
36
      ]);
37
38
    // get contact data from user
39
40
    $email  = '';
41
    $code   = '';
42
    $number = '';
43
44
    if ($user->getPeople()->getEmail()->count() > 0)
45
      $email = $user->getPeople()->getEmail()->first()->getEmail();
46
47
    if ($user->getPeople()->getPhone()->count() > 0) {
48
      $phone  = $user->getPeople()->getPhone()->first();
49
      $code   = $phone->getDdd();
50
      $number = $phone->getPhone();
51
    }
52
53
    return $this->json([
54
      'username' => $user->getUsername(),
55
      'roles'    => $user->getRoles(),
56
      'api_key'  => $user->getApiKey(),
57
      'people'   => $user->getPeople()->getId(),
58
      'mycompany'  => $this->getCompanyId($user),
59
      'realname' => $this->getUserRealName($user->getPeople()),
60
      'avatar'   => $user->getPeople()->getFile() ? '/files/download/' . $user->getPeople()->getFile()->getId() : null,
61
      'email'    => $email,
62
      'phone'    => sprintf('%s%s', $code, $number),
63
      'active'   => (int) $user->getPeople()->getEnabled(),
64
    ]);
65
  }
66
67
  private function getUserRealName(People $people): string
68
  {
69
    $realName = 'John Doe';
70
71
    if ($people->getPeopleType() == 'J')
72
      $realName = $people->getAlias();
73
74
    else {
75
      if ($people->getPeopleType() == 'F') {
76
        $realName  = $people->getName();
77
        $realName .= ' ' . $people->getAlias();
78
        $realName  = trim($realName);
79
      }
80
    }
81
82
    return $realName;
83
  }
84
85
  private function getCompany(User $user): ?People
86
  {
87
    $peopleLink = $this->manager->getRepository(People::class)->getPeopleLink($user->getPeople(), 'employee', 1);
88
    if ($peopleLink !== false && $peopleLink->getCompany() instanceof People)
89
      return $peopleLink->getCompany();
90
  }
91
92
  private function getCompanyId(User $user): ?int
93
  {
94
    $company = $this->getCompany($user);
95
    return $company ? $company->getId() : null;
96
  }
97
}
98