generated from Setono/SyliusPluginSkeleton
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
We are getting there. Missing the callback action
- Loading branch information
Showing
31 changed files
with
653 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Setono\SyliusAgeVerificationPlugin\Checker; | ||
|
||
use Setono\SyliusAgeVerificationPlugin\Model\AgeAwareProductInterface; | ||
use Setono\SyliusAgeVerificationPlugin\Model\MinimumAge; | ||
use Sylius\Component\Core\Model\OrderInterface; | ||
|
||
final class MinimumAgeChecker implements MinimumAgeCheckerInterface | ||
{ | ||
public function __construct( | ||
/** @var list<string> $enabledCountries */ | ||
private readonly array $enabledCountries, | ||
) { | ||
} | ||
|
||
public function check(OrderInterface $order): ?MinimumAge | ||
{ | ||
$countryCode = $order->getShippingAddress()?->getCountryCode(); | ||
if (null === $countryCode || !in_array($countryCode, $this->enabledCountries, true)) { | ||
return null; | ||
} | ||
|
||
$minimumAge = null; | ||
|
||
foreach ($order->getItems() as $item) { | ||
$product = $item->getProduct(); | ||
if (!$product instanceof AgeAwareProductInterface) { | ||
continue; | ||
} | ||
|
||
$productMinimumAge = $product->getMinimumAge(); | ||
|
||
if (null === $productMinimumAge) { | ||
continue; | ||
} | ||
|
||
if (null === $minimumAge || $productMinimumAge > $minimumAge->value) { | ||
$minimumAge = MinimumAge::from($productMinimumAge); | ||
} | ||
} | ||
|
||
return $minimumAge; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Setono\SyliusAgeVerificationPlugin\Checker; | ||
|
||
use Setono\SyliusAgeVerificationPlugin\Model\MinimumAge; | ||
use Sylius\Component\Core\Model\OrderInterface; | ||
|
||
interface MinimumAgeCheckerInterface | ||
{ | ||
/** | ||
* Returns null if the order is not age restricted else returns the minimum age required | ||
*/ | ||
public function check(OrderInterface $order): ?MinimumAge; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Setono\SyliusAgeVerificationPlugin\Controller; | ||
|
||
use Setono\SyliusAgeVerificationPlugin\OpenIdConfiguration\OpenIdConfiguration; | ||
use Symfony\Component\HttpClient\HttpClient; | ||
use Symfony\Component\HttpFoundation\RedirectResponse; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\Routing\Generator\UrlGeneratorInterface; | ||
|
||
final class CriiptoCallbackAction | ||
{ | ||
public function __construct( | ||
private readonly OpenIdConfiguration $openIdConfiguration, | ||
private readonly UrlGeneratorInterface $urlGenerator, | ||
private readonly string $clientId, | ||
private readonly string $clientSecret, | ||
) { | ||
} | ||
|
||
public function __invoke(Request $request): RedirectResponse | ||
{ | ||
$client = HttpClient::create(); | ||
$response = $client->request('POST', $this->openIdConfiguration->tokenEndpoint, [ | ||
'headers' => [ | ||
'Authorization' => 'Basic ' . base64_encode(sprintf('%s:%s', urlencode($this->clientId), urlencode($this->clientSecret))), | ||
], | ||
'body' => [ | ||
'grant_type' => 'authorization_code', | ||
'code' => $request->query->get('code'), | ||
'client_id' => $this->clientId, | ||
'redirect_uri' => $this->urlGenerator->generate( | ||
name: 'setono_sylius_age_verification_shop_criipto_callback', | ||
referenceType: UrlGeneratorInterface::ABSOLUTE_URL, | ||
), | ||
], | ||
]); | ||
|
||
dd($response->getContent(false)); | ||
Check failure on line 41 in src/Controller/CriiptoCallbackAction.php GitHub Actions / Static Code Analysis (PHP8.1 | Deps: lowest | SF~6.4.0)ForbiddenCode
Check failure on line 41 in src/Controller/CriiptoCallbackAction.php GitHub Actions / Static Code Analysis (PHP8.2 | Deps: lowest | SF~6.4.0)ForbiddenCode
Check failure on line 41 in src/Controller/CriiptoCallbackAction.php GitHub Actions / Static Code Analysis (PHP8.2 | Deps: highest | SF~6.4.0)ForbiddenCode
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Setono\SyliusAgeVerificationPlugin\Model; | ||
|
||
interface AgeAwareCustomerInterface | ||
{ | ||
public function isOver(int $age): bool; | ||
|
||
public function setIsOverAgeCheckedAt(\DateTimeInterface $ageCheckedAt): void; | ||
|
||
public function setIsOver(int $age): void; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Setono\SyliusAgeVerificationPlugin\Model; | ||
|
||
use Doctrine\ORM\Mapping as ORM; | ||
|
||
trait AgeAwareCustomerTrait | ||
{ | ||
/** @ORM\Column(type="datetime", nullable=true) */ | ||
#[ORM\Column(type: 'datetime', nullable: true)] | ||
protected ?\DateTimeInterface $isOverAgeCheckedAt = null; | ||
|
||
/** @ORM\Column(type="integer", nullable=true, options={"unsigned"=true}) */ | ||
#[ORM\Column(type: 'integer', nullable: true, options: ['unsigned' => true])] | ||
protected ?int $isOver = null; | ||
|
||
// todo test this | ||
public function isOver(int $age): bool | ||
{ | ||
if ($this->isOverAgeCheckedAt === null || $this->isOver === null) { | ||
return false; | ||
} | ||
|
||
if ($age <= $this->isOver) { | ||
return true; | ||
} | ||
|
||
$now = new \DateTimeImmutable(); | ||
$diff = $now->diff($this->isOverAgeCheckedAt); | ||
|
||
return $age <= ($this->isOver + $diff->y); | ||
} | ||
|
||
public function setIsOverAgeCheckedAt(?\DateTimeInterface $isOverAgeCheckedAt): void | ||
{ | ||
$this->isOverAgeCheckedAt = $isOverAgeCheckedAt; | ||
} | ||
|
||
public function setIsOver(?int $age): void | ||
{ | ||
$this->isOver = $age; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Setono\SyliusAgeVerificationPlugin\Model; | ||
|
||
enum MinimumAge: int | ||
{ | ||
case MINIMUM_15 = 15; | ||
case MINIMUM_16 = 16; | ||
case MINIMUM_18 = 18; | ||
case MINIMUM_21 = 21; | ||
|
||
public function asScope(): string | ||
{ | ||
return 'is_over_' . $this->value; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Setono\SyliusAgeVerificationPlugin\OpenIdConfiguration; | ||
|
||
/** | ||
* @final | ||
* | ||
* This is non-final because we define it as a lazy service | ||
*/ | ||
class OpenIdConfiguration | ||
{ | ||
public function __construct(public readonly string $authorizationEndpoint, public readonly string $tokenEndpoint) | ||
{ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Setono\SyliusAgeVerificationPlugin\OpenIdConfiguration; | ||
|
||
use Symfony\Component\HttpClient\HttpClient; | ||
use Webmozart\Assert\Assert; | ||
|
||
final class OpenIdConfigurationFactory implements OpenIdConfigurationFactoryInterface | ||
{ | ||
public function __construct(private readonly string $verifyDomain) | ||
{ | ||
} | ||
|
||
public function create(): OpenIdConfiguration | ||
{ | ||
$client = HttpClient::create(); | ||
$openIdConfiguration = $client | ||
->request('GET', sprintf('https://%s/.well-known/openid-configuration', $this->verifyDomain)) | ||
->toArray() | ||
; | ||
|
||
Assert::keyExists($openIdConfiguration, 'token_endpoint'); | ||
Assert::keyExists($openIdConfiguration, 'authorization_endpoint'); | ||
|
||
/** @var mixed $tokenEndpoint */ | ||
$tokenEndpoint = $openIdConfiguration['token_endpoint']; | ||
Assert::stringNotEmpty($tokenEndpoint); | ||
|
||
/** @var mixed $authorizationEndpoint */ | ||
$authorizationEndpoint = $openIdConfiguration['authorization_endpoint']; | ||
Assert::stringNotEmpty($authorizationEndpoint); | ||
|
||
return new OpenIdConfiguration($authorizationEndpoint, $tokenEndpoint); | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
src/OpenIdConfiguration/OpenIdConfigurationFactoryInterface.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Setono\SyliusAgeVerificationPlugin\OpenIdConfiguration; | ||
|
||
interface OpenIdConfigurationFactoryInterface | ||
{ | ||
public function create(): OpenIdConfiguration; | ||
} |
Oops, something went wrong.