Passed
Push — master ( d0c8f5...836538 )
by Luiz Kim
02:59
created

UserService::getPermission()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace ControleOnline\Service;
4
5
use ControleOnline\Entity\Email;
0 ignored issues
show
Bug introduced by
The type ControleOnline\Entity\Email 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 ControleOnline\Entity\Language;
0 ignored issues
show
Bug introduced by
The type ControleOnline\Entity\Language 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 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...
8
use ControleOnline\Entity\User;
9
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...
10
use Exception;
11
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Securi...asswordEncoderInterface 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 UserService
14
{
15
  public function __construct(private EntityManagerInterface $manager, private  UserPasswordEncoderInterface $encoder) {}
16
  public function changePassword(User $user, $password)
17
  {
18
    if (!$this->getPermission())
19
      throw new Exception("You should not pass!!!", 301);
20
21
    $user->setHash(
22
      $this->encoder->encodePassword($user, $password)
23
    );
24
25
    $this->manager->persist($user);
26
    $this->manager->flush();
27
    return $user;
28
  }
29
30
  public function changeApiKey(User $user)
31
  {
32
    if (!$this->getPermission())
33
      throw new Exception("You should not pass!!!", 301);
34
35
    $user->generateApiKey();
36
37
    $this->manager->persist($user);
38
    $this->manager->flush();
39
    return $user;
40
  }
41
42
43
  public function discoveryUser($email, $password, $firstName, $lastName)
44
  {
45
46
    $user = $this->manager->getRepository(User::class)
47
      ->findOneBy([
48
        'username'       => $email,
49
      ]);
50
51
52
    $people = $this->discoveryPeople($email, $firstName, $lastName);
53
54
    if (!$user)
55
      $user = $this->createUser($people, $email, $password);
56
57
    return   $user;
58
  }
59
60
  public function getUserSession($user)
61
  {
62
63
    // get contact data from user
64
65
    $email  = '';
66
    $code   = '';
67
    $number = '';
68
69
    if ($user->getPeople()->getEmail()->count() > 0)
70
      $email = $user->getPeople()->getEmail()->first()->getEmail();
71
72
    if ($user->getPeople()->getPhone()->count() > 0) {
73
      $phone  = $user->getPeople()->getPhone()->first();
74
      $code   = $phone->getDdd();
75
      $number = $phone->getPhone();
76
    }
77
78
    return [
79
      'id'   => $user->getPeople()->getId(),
80
      'username' => $user->getUsername(),
81
      'roles'    => $user->getRoles(),
82
      'api_key'  => $user->getApiKey(),
83
      'people'   => $user->getPeople()->getId(),
84
      'mycompany'  => $this->getCompanyId($user),
85
      'realname' => $this->getUserRealName($user->getPeople()),
86
      'avatar'   => $user->getPeople()->getImage() ? '/files/' . $user->getPeople()->getImage()->getId() . '/download' : null,
87
      'email'    => $email,
88
      'phone'    => sprintf('%s%s', $code, $number),
89
      'active'   => (int) $user->getPeople()->getEnabled(),
90
    ];
91
  }
92
93
  private function getUserRealName(People $people): string
94
  {
95
    $realName = 'John Doe';
96
97
    if ($people->getPeopleType() == 'J')
98
      $realName = $people->getAlias();
99
100
    else {
101
      if ($people->getPeopleType() == 'F') {
102
        $realName  = $people->getName();
103
        $realName .= ' ' . $people->getAlias();
104
        $realName  = trim($realName);
105
      }
106
    }
107
108
    return $realName;
109
  }
110
  
111
  public function discoveryPeople($email, $firstName = '', $lastName = '')
112
  {
113
    $email = $this->manager->getRepository(Email::class)
114
      ->findOneBy([
115
        'email'       => $email,
116
      ]);
117
    if ($email) {
118
      $people = $email->getPeople();
119
    } else {
120
      $email = new Email();
121
      $email->setEmail($email);
122
      $this->manager->persist($email);
123
    }
124
125
    if (!$people) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $people does not seem to be defined for all execution paths leading up to this point.
Loading history...
126
127
      $lang = $this->manager->getRepository(Language::class)->findOneBy(['language' => 'pt-BR']);
128
      $people = new People();
129
      $people->setAlias($firstName);
130
      $people->setName($lastName);
131
      $people->setLanguage($lang);
132
      //$people->setBilling(0);
133
      //$people->setBillingDays('daily');
134
      //$people->setPaymentTerm(1);
135
      //$people->setIcms(0);
136
      $email->setPeople($people);
137
      $this->manager->persist($email);
138
    }
139
140
    $this->manager->persist($people);
141
    $this->manager->flush();
142
    return $people;
143
  }
144
145
  public function createUser(People $people, $username, $password)
146
  {
147
    if (!$this->getPermission())
148
      throw new Exception("You should not pass!!!", 301);
149
150
    $user = $this->manager->getRepository(User::class)
0 ignored issues
show
Unused Code introduced by
The assignment to $user is dead and can be removed.
Loading history...
151
      ->findOneBy([
152
        'username'       => $username,
153
      ]);
154
155
    if ($username)
156
      throw new Exception("User already exists", 301);
157
158
    $user = new User();
159
    $user->setPeople($people);
160
    $user->setHash($this->encoder->encodePassword($user, $password));
161
    $user->setUsername($username);
162
163
    $this->manager->persist($user);
164
    $this->manager->flush();
165
    return $user;
166
  }
167
168
169
  public function getCompany(User $user)
170
  {
171
    $peopleLink = $user->getPeople()->getLink()->first();
172
173
    if ($peopleLink !== false && $peopleLink->getCompany() instanceof People)
174
      return $peopleLink->getCompany();
175
  }
176
177
  public function getCompanyId(User $user)
178
  {
179
    $company = $this->getCompany($user);
180
    return $company ? $company->getId() : null;
181
  }
182
183
  /**
184
   * @todo arrumar 
185
   */
186
  private function getPermission()
187
  {
188
    return true;
189
  }
190
}
191