Files
kupshop/bundles/KupShop/UserOauthBundle/ResourceOwner/SeznamResourceOwner.php
2025-08-02 16:30:27 +02:00

114 lines
3.4 KiB
PHP

<?php
declare(strict_types=1);
namespace KupShop\UserOauthBundle\ResourceOwner;
use HWI\Bundle\OAuthBundle\OAuth\RequestDataStorageInterface;
use HWI\Bundle\OAuthBundle\OAuth\ResourceOwner\GenericOAuth2ResourceOwner;
use HWI\Bundle\OAuthBundle\OAuth\Response\UserResponseInterface;
use KupShop\KupShopBundle\Config;
use KupShop\KupShopBundle\Context\DomainContext;
use KupShop\KupShopBundle\Util\Contexts;
use Query\Operator;
use Symfony\Component\Security\Http\HttpUtils;
use Symfony\Contracts\HttpClient\HttpClientInterface;
class SeznamResourceOwner extends GenericOAuth2ResourceOwner implements ICustomResourceOwner
{
public const URL = 'https://login.szn.cz/api/v1/';
public function __construct(
HttpClientInterface $httpClient,
HttpUtils $httpUtils,
array $options,
string $name,
RequestDataStorageInterface $storage,
) {
if (!isLocalDevelopment()) {
$options['client_id'] = $this->getClientId();
$options['client_secret'] = $this->getClientSecret();
}
$options['access_token_url'] = $this->getUrl('oauth/token');
$options['authorization_url'] = $this->getUrl('oauth/auth');
$options['infos_url'] = $this->getUrl('user');
parent::__construct($httpClient, $httpUtils, $options, $name, $storage);
}
public function updateUserData(\User $user, UserResponseInterface $response)
{
$data = [];
if ($firstName = $response->getData()['firstname']) {
$data['name'] = $firstName;
}
if ($lastName = $response->getData()['lastname']) {
$data['surname'] = $lastName;
}
if (!empty($data)) {
sqlQueryBuilder()->update('users')
->directValues($data)
->where(Operator::equals(['id' => $user->id]))
->execute();
}
}
protected function getClientId(): string
{
if (isLocalDevelopment()) {
return $this->options['client_id'];
}
$settings = \Settings::getDefault();
return $settings['oauth']['seznam']['client_id'] ?? '';
}
protected function getClientSecret(): string
{
if (isLocalDevelopment()) {
return $this->options['client_secret'];
}
$settings = \Settings::getDefault();
return $settings['oauth']['seznam']['client_secret'] ?? '';
}
protected function getUrl(string $path): string
{
return self::URL.$path;
}
protected function getRedirectUri(): string
{
if (isDevelopment()) {
return Config::get()['Addr']['full'].'login/check-seznam/';
}
/** @var DomainContext $domainContext */
$domainContext = Contexts::get(DomainContext::class);
return $domainContext->getActiveWithScheme().'/login/check-seznam/';
}
public function getAuthorizationUrl($redirectUri, array $extraParameters = [])
{
$extraParameters['redirect_uri'] = $this->getRedirectUri();
return parent::getAuthorizationUrl($redirectUri, $extraParameters);
}
protected function doGetTokenRequest($url, array $parameters = [])
{
$parameters['client_id'] = $this->getClientId();
$parameters['client_secret'] = $this->getClientSecret();
$parameters['redirect_uri'] = $this->getRedirectUri();
return $this->httpRequest($url, http_build_query($parameters, '', '&'));
}
}