ControleOnline /
api-platform-users
| 1 | <?php |
||
| 2 | |||
| 3 | namespace ControleOnline\Service; |
||
| 4 | |||
| 5 | use ControleOnline\Entity\Email; |
||
|
0 ignored issues
–
show
|
|||
| 6 | use ControleOnline\Entity\Language; |
||
|
0 ignored issues
–
show
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. 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
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. 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
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. 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\PasswordHasher\Hasher\UserPasswordHasherInterface; |
||
|
0 ignored issues
–
show
The type
Symfony\Component\Passwo...PasswordHasherInterface 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. 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( |
||
| 16 | private EntityManagerInterface $manager, |
||
| 17 | private UserPasswordHasherInterface $passwordHasher, |
||
| 18 | private FileService $fileService |
||
|
0 ignored issues
–
show
The type
ControleOnline\Service\FileService 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. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths Loading history...
|
|||
| 19 | ) {} |
||
| 20 | |||
| 21 | public function changePassword(User $user, $password) |
||
| 22 | { |
||
| 23 | if (!$this->getPermission()) { |
||
| 24 | throw new Exception("You should not pass!!!", 301); |
||
| 25 | } |
||
| 26 | |||
| 27 | $hashedPassword = $this->passwordHasher->hashPassword($user, $password); |
||
| 28 | $user->setHash($hashedPassword); |
||
| 29 | |||
| 30 | $this->manager->persist($user); |
||
| 31 | $this->manager->flush(); |
||
| 32 | return $user; |
||
| 33 | } |
||
| 34 | |||
| 35 | public function changeApiKey(User $user) |
||
| 36 | { |
||
| 37 | if (!$this->getPermission()) { |
||
| 38 | throw new Exception("You should not pass!!!", 301); |
||
| 39 | } |
||
| 40 | |||
| 41 | $user->generateApiKey(); |
||
| 42 | |||
| 43 | $this->manager->persist($user); |
||
| 44 | $this->manager->flush(); |
||
| 45 | return $user; |
||
| 46 | } |
||
| 47 | |||
| 48 | public function discoveryUser($email, $password, $firstName, $lastName) |
||
| 49 | { |
||
| 50 | $user = $this->manager->getRepository(User::class) |
||
| 51 | ->findOneBy([ |
||
| 52 | 'username' => $email, |
||
| 53 | ]); |
||
| 54 | |||
| 55 | $people = $this->discoveryPeople($email, $firstName, $lastName); |
||
| 56 | |||
| 57 | if (!$user) { |
||
| 58 | $user = $this->createUser($people, $email, $password); |
||
| 59 | } |
||
| 60 | |||
| 61 | return $user; |
||
| 62 | } |
||
| 63 | |||
| 64 | public function getUserSession(User $user) |
||
| 65 | { |
||
| 66 | $email = ''; |
||
| 67 | $code = ''; |
||
| 68 | $number = ''; |
||
| 69 | |||
| 70 | if ($user->getPeople()->getEmail()->count() > 0) { |
||
| 71 | $email = $user->getPeople()->getEmail()->first()->getEmail(); |
||
| 72 | } |
||
| 73 | |||
| 74 | if ($user->getPeople()->getPhone()->count() > 0) { |
||
| 75 | $phone = $user->getPeople()->getPhone()->first(); |
||
| 76 | $code = $phone->getDdd(); |
||
| 77 | $number = $phone->getPhone(); |
||
| 78 | } |
||
| 79 | |||
| 80 | return [ |
||
| 81 | 'id' => $user->getPeople()->getId(), |
||
| 82 | 'username' => $user->getUsername(), |
||
| 83 | 'roles' => $user->getRoles(), |
||
| 84 | 'api_key' => $user->getApiKey(), |
||
| 85 | 'people' => $user->getPeople()->getId(), |
||
| 86 | 'mycompany' => $this->getCompanyId($user), |
||
| 87 | 'realname' => $this->getUserRealName($user->getPeople()), |
||
| 88 | 'avatar' => $this->fileService->getFileUrl($user->getPeople()), |
||
| 89 | 'email' => $email, |
||
| 90 | 'phone' => sprintf('%s%s', $code, $number), |
||
| 91 | 'active' => (int) $user->getPeople()->getEnabled(), |
||
| 92 | ]; |
||
| 93 | } |
||
| 94 | |||
| 95 | private function getUserRealName(People $people): string |
||
| 96 | { |
||
| 97 | $realName = 'John Doe'; |
||
| 98 | |||
| 99 | if ($people->getPeopleType() == 'J') { |
||
| 100 | $realName = $people->getAlias(); |
||
| 101 | } else { |
||
| 102 | if ($people->getPeopleType() == 'F') { |
||
| 103 | $realName = $people->getName() . ' ' . $people->getAlias(); |
||
| 104 | $realName = trim($realName); |
||
| 105 | } |
||
| 106 | } |
||
| 107 | |||
| 108 | return $realName; |
||
| 109 | } |
||
| 110 | |||
| 111 | public function discoveryPeople($mail, $firstName = '', $lastName = '') |
||
| 112 | { |
||
| 113 | $email = $this->manager->getRepository(Email::class) |
||
| 114 | ->findOneBy([ |
||
| 115 | 'email' => $mail, |
||
| 116 | ]); |
||
| 117 | if ($email) { |
||
| 118 | $people = $email->getPeople(); |
||
| 119 | } else { |
||
| 120 | $email = new Email(); |
||
| 121 | $email->setEmail($mail); |
||
| 122 | $this->manager->persist($email); |
||
| 123 | } |
||
| 124 | |||
| 125 | if (!$people) { |
||
|
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
| 126 | $lang = $this->manager->getRepository(Language::class)->findOneBy(['language' => 'pt-BR']); |
||
| 127 | $people = new People(); |
||
| 128 | $people->setAlias($firstName); |
||
| 129 | $people->setName($lastName); |
||
| 130 | $people->setLanguage($lang); |
||
| 131 | $email->setPeople($people); |
||
| 132 | $this->manager->persist($email); |
||
| 133 | } |
||
| 134 | |||
| 135 | $this->manager->persist($people); |
||
| 136 | $this->manager->flush(); |
||
| 137 | return $people; |
||
| 138 | } |
||
| 139 | |||
| 140 | public function createUser(People $people, $username, $password) |
||
| 141 | { |
||
| 142 | if (!$this->getPermission()) { |
||
| 143 | throw new Exception("You should not pass!!!", 301); |
||
| 144 | } |
||
| 145 | |||
| 146 | $user = $this->manager->getRepository(User::class) |
||
| 147 | ->findOneBy([ |
||
| 148 | 'username' => $username, |
||
| 149 | ]); |
||
| 150 | |||
| 151 | if ($user) { |
||
| 152 | throw new Exception("User already exists", 301); |
||
| 153 | } |
||
| 154 | |||
| 155 | $user = new User(); |
||
| 156 | $user->setPeople($people); |
||
| 157 | $user->setHash($this->passwordHasher->hashPassword($user, $password)); |
||
| 158 | $user->setUsername($username); |
||
| 159 | |||
| 160 | $this->manager->persist($user); |
||
| 161 | $this->manager->flush(); |
||
| 162 | return $user; |
||
| 163 | } |
||
| 164 | |||
| 165 | public function getCompany(User $user) |
||
| 166 | { |
||
| 167 | $peopleLink = $user->getPeople()->getLink()->first(); |
||
| 168 | |||
| 169 | if ($peopleLink !== false && $peopleLink->getCompany() instanceof People) { |
||
| 170 | return $peopleLink->getCompany(); |
||
| 171 | } |
||
| 172 | } |
||
| 173 | |||
| 174 | public function getCompanyId(User $user) |
||
| 175 | { |
||
| 176 | $company = $this->getCompany($user); |
||
| 177 | return $company ? $company->getId() : null; |
||
| 178 | } |
||
| 179 | |||
| 180 | /** |
||
| 181 | * @todo arrumar |
||
| 182 | */ |
||
| 183 | private function getPermission() |
||
| 184 | { |
||
| 185 | return true; |
||
| 186 | } |
||
| 187 | } |
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:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths