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

UserService::changePassword()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 7
dl 0
loc 12
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 2
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 discoveryPeople($email, $firstName = '', $lastName = '')
61
  {
62
    $email = $this->manager->getRepository(Email::class)
63
      ->findOneBy([
64
        'email'       => $email,
65
      ]);
66
    if ($email) {
67
      $people = $email->getPeople();
68
    } else {
69
      $email = new Email();
70
      $email->setEmail($email);
71
      $this->manager->persist($email);
72
    }
73
74
    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...
75
76
      $lang = $this->manager->getRepository(Language::class)->findOneBy(['language' => 'pt-BR']);
77
      $people = new People();
78
      $people->setAlias($firstName);
79
      $people->setName($lastName);
80
      $people->setLanguage($lang);
81
      //$people->setBilling(0);
82
      //$people->setBillingDays('daily');
83
      //$people->setPaymentTerm(1);
84
      //$people->setIcms(0);
85
      $email->setPeople($people);
86
      $this->manager->persist($email);
87
    }
88
89
    $this->manager->persist($people);
90
    $this->manager->flush();
91
    return $people;
92
  }
93
94
  public function createUser(People $people, $username, $password)
95
  {
96
    if (!$this->getPermission())
97
      throw new Exception("You should not pass!!!", 301);
98
99
    $user = new User();
100
    $user->setPeople($people);
101
    $user->setHash($this->encoder->encodePassword($user, $password));
102
    $user->setUsername($username);
103
104
    $this->manager->persist($user);
105
    $this->manager->flush();
106
    return $user;
107
  }
108
109
110
  public function getCompany(User $user)
111
  {
112
    $peopleLink = $user->getPeople()->getLink()->first();
113
114
    if ($peopleLink !== false && $peopleLink->getCompany() instanceof People)
115
      return $peopleLink->getCompany();
116
  }
117
118
  public function getCompanyId(User $user)
119
  {
120
    $company = $this->getCompany($user);
121
    return $company ? $company->getId() : null;
122
  }
123
124
  /**
125
   * @todo arrumar 
126
   */
127
  private function getPermission()
128
  {
129
    return true;
130
  }
131
}
132