<?php
namespace App\Entity;
use JsonException;
use Lexik\Bundle\JWTAuthenticationBundle\Security\User\JWTUserInterface;
use Symfony\Component\HttpClient\HttpClient;
use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
/**
* @ORM\Entity
* @ORM\Table(name="user")
*/
final class User implements JWTUserInterface
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private int $id;
/**
* @ORM\Column(type="string", unique=true)
*/
private string $username;
/**
* @ORM\Column(type="string", unique=true)
*/
private string $email;
/**
* @ORM\Column(type="string")
*/
private string $password;
/**
* @ORM\Column(type="json_array")
*/
private array $roles;
/**
* @ORM\Column(type="string", unique=true)
*/
private string $apiToken;
/**
* @param string $username
* @param array $roles
* @param string $email
* @param string $password
*/
public function __construct(string $username, array $roles, string $email, string $password)
{
$this->username = $username;
$this->roles = $roles;
$this->email = $email;
$this->password = $password;
}
/**
* @param $username
* @param array $payload
*
* @return User
*/
public static function createFromPayload($username, array $payload): User
{
return new self(
$username,
$payload['roles'],
$payload['email'],
$payload['password']
);
}
/**
* @return int
*/
public function getId(): int
{
return $this->id;
}
/**
* @return string
*/
public function getUsername(): string
{
return $this->username;
}
/**
* @param $username
*
* @return $this
*/
public function setUsername($username): User
{
$this->username = $username;
return $this;
}
/**
* @return string|null
*/
public function getEmail(): ?string
{
return $this->email;
}
/**
* @param $email
*
* @return $this
*/
public function setEmail($email): User
{
$this->email = $email;
return $this;
}
/**
* @return string|null
*/
public function getPassword(): ?string
{
return $this->password;
}
/**
* @param $password
*
* @return $this
*/
public function setPassword($password): User
{
$this->password = $password;
return $this;
}
/**
* Returns the roles or permissions granted to the user for security.
*/
public function getRoles(): array
{
$roles = $this->roles;
if (empty($roles)) {
$roles[] = 'User';
}
return array_unique($roles);
}
/**
* @param $roles
*
* @return $this
*/
public function setRoles($roles): User
{
$this->roles = $roles;
return $this;
}
/**
* Returns the salt that was originally used to encode the password.
*/
public function getSalt(): User
{
return $this;
}
/**
* Removes sensitive data from the user.
*/
public function eraseCredentials(): void
{
// if you had a plainPassword property, you'd nullify it here
// $this->plainPassword = null;
}
/**
* @param string $apiToken
*
* @return User
*/
public function setApiToken(string $apiToken): User
{
$this->apiToken = $apiToken;
return $this;
}
/**
* @return string
*/
public function getApiToken(): string
{
return $this->apiToken;
}
/**
* Returns the identifier for this user (e.g. its username or email address).
*/
public function getUserIdentifier(): string
{
return $this->email;
}
/**
* @param string $apiUrl
* @param string $username
* @param string $password
* @param string $clientId
* @param string $secretKey
* @param string $method
* @param string $contentType
* @param string $role
*
* @return array
* @throws JsonException
* @throws TransportExceptionInterface
*/
public function callUserApi(
string $apiUrl,
string $username,
string $password,
string $clientId,
string $secretKey,
string $method = 'POST',
string $contentType = 'application/x-www-form-urlencoded',
string $role = 'User'
): array {
$params = [
'role' => $role,
'client_id' => $clientId,
'client_secret' => $secretKey,
'grant_type' => 'facticity',
'username' => $username,
'password' => $password,
];
$http_client = HttpClient::create([ 'headers' => [ 'Content-Type: ' . $contentType ] ]);
$response = $http_client->request(
$method,
$apiUrl,
[ 'body' => $params ]
);
try {
$content = json_decode($response->getContent(), true, 512, JSON_THROW_ON_ERROR);
if (array_key_exists('error', $content)) {
return [ false, $content['error'] ];
}
return $content;
} catch (
ClientExceptionInterface | RedirectionExceptionInterface |
ServerExceptionInterface | TransportExceptionInterface $e
) {
return [ false, $e->getMessage() ];
}
}
/**
* @param string $apiUrl
* @param array $params
* @param string $method
* @param string $contentType
*
* @return array
* @throws JsonException
* @throws TransportExceptionInterface
*/
public function callDataApi(
string $apiUrl,
array $params,
string $method = 'POST',
string $contentType = 'application/x-www-form-urlencoded'
): array {
$http_client = HttpClient::create([ 'headers' => [ 'Content-Type: ' . $contentType ] ]);
$response = $http_client->request(
$method,
$apiUrl,
[ 'body' => $params ]
);
try {
$content = json_decode($response->getContent(), true, 512, JSON_THROW_ON_ERROR);
if (array_key_exists('error', $content)) {
return [ false, $content['error'] ];
}
return $content;
} catch (
ClientExceptionInterface | RedirectionExceptionInterface |
ServerExceptionInterface | TransportExceptionInterface $e
) {
return [ false, $e->getMessage() ];
}
}
}