Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trying to apply some DDD concept #441

Draft
wants to merge 8 commits into
base: ACCOUNT-2595/feat/create-identity-on-installation
Choose a base branch
from
5 changes: 2 additions & 3 deletions config/command.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@ services:
arguments:
- '@PrestaShop\Module\PsAccounts\Api\Client\AccountsClient'
- '@PrestaShop\Module\PsAccounts\Provider\ShopProvider'
- '@PrestaShop\Module\PsAccounts\Provider\OAuth2\Oauth2Client'
- '@PrestaShop\Module\PsAccounts\Account\ShopIdentity'
- '@PrestaShop\Module\PsAccounts\Identity\Domain\IdentityManager'

PrestaShop\Module\PsAccounts\Account\CommandHandler\CreateIdentitiesHandler:
class: PrestaShop\Module\PsAccounts\Account\CommandHandler\CreateIdentitiesHandler
Expand All @@ -58,6 +57,6 @@ services:
arguments:
- '@PrestaShop\Module\PsAccounts\Api\Client\AccountsClient'
- '@PrestaShop\Module\PsAccounts\Provider\ShopProvider'
- '@PrestaShop\Module\PsAccounts\Provider\OAuth2\Oauth2Client'
- '@PrestaShop\Module\PsAccounts\Account\Session\Firebase\ShopSession'
- '@PrestaShop\Module\PsAccounts\Identity\Domain\IdentityManager'
- '@PrestaShop\Module\PsAccounts\Account\ManageProof'
7 changes: 7 additions & 0 deletions config/common.yml
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,13 @@ services:
arguments:
- '@PrestaShop\Module\PsAccounts\Account\Session\Firebase\OwnerSession'

PrestaShop\Module\PsAccounts\Identity\Domain\IdentityManager:
class: PrestaShop\Module\PsAccounts\Identity\Infrastructure\ConfigurationIdentityManager
public: true
arguments:
- '@PrestaShop\Module\PsAccounts\Context\ShopContext'
- '@PrestaShop\Module\PsAccounts\Repository\ConfigurationRepository'

#####################
# presenters

Expand Down
37 changes: 16 additions & 21 deletions src/Account/CommandHandler/CreateIdentityHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@
namespace PrestaShop\Module\PsAccounts\Account\CommandHandler;

use PrestaShop\Module\PsAccounts\Account\Command\CreateIdentityCommand;
use PrestaShop\Module\PsAccounts\Account\ShopIdentity;
use PrestaShop\Module\PsAccounts\Api\Client\AccountsClient;
use PrestaShop\Module\PsAccounts\Provider\OAuth2\Oauth2Client;
use PrestaShop\Module\PsAccounts\Identity\Domain\IdentityManager;
use PrestaShop\Module\PsAccounts\Identity\Domain\OAuth2Client;
use PrestaShop\Module\PsAccounts\Provider\ShopProvider;

class CreateIdentityHandler
Expand All @@ -33,37 +33,29 @@ class CreateIdentityHandler
*/
private $accountsClient;

/**
* @var Oauth2Client
*/
private $oauth2Client;

/**
* @var ShopProvider
*/
private $shopProvider;

/**
* @var ShopIdentity
* @var IdentityManager
*/
private $shopIdentity;
private $identityManager;

/**
* @param AccountsClient $accountsClient
* @param ShopProvider $shopProvider
* @param Oauth2Client $oauth2Client
* @param ShopIdentity $shopIdentity
* @param IdentityManager $identityManager
*/
public function __construct(
AccountsClient $accountsClient,
ShopProvider $shopProvider,
Oauth2Client $oauth2Client,
ShopIdentity $shopIdentity
IdentityManager $identityManager
) {
$this->accountsClient = $accountsClient;
$this->shopProvider = $shopProvider;
$this->oauth2Client = $oauth2Client;
$this->shopIdentity = $shopIdentity;
$this->identityManager = $identityManager;
}

/**
Expand All @@ -79,18 +71,21 @@ public function handle(CreateIdentityCommand $command)
// - identify shop (when ?) -> be sure we send version with it & when to trigger it ?
// - UX associated ?
// - Migrate routes using user token
if (!$this->oauth2Client->exists()) {

$identity = $this->identityManager->get($command->shopId);

if (!$identity->hasOAuth2Client()) {
$response = $this->accountsClient->createShopIdentity(
$this->shopProvider->getUrl($command->shopId)
);

if ($response['status'] === true && isset($response['body'])) {
$body = $response['body'];
// FIXME: should we refactor those kind of "entities" ?
// FIXME: oauthClientRepository->getClientByShopId ?
// FIXME: shopIdentityRepository->getIdentityByShopId ?
$this->oauth2Client->update($body['clientId'], $body['clientSecret']);
$this->shopIdentity->setShopUuid($body['cloudShopId']);

$oauth2Client = new OAuth2Client($body['clientId'], $body['clientSecret']);
$identity->create($body['cloudShopId'], $oauth2Client);

$this->identityManager->save($identity);
} else {
// TODO Add bad request handling here
}
Expand Down
32 changes: 18 additions & 14 deletions src/Account/CommandHandler/VerifyAuthenticityHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
use PrestaShop\Module\PsAccounts\Account\ManageProof;
use PrestaShop\Module\PsAccounts\Account\Session\ShopSession;
use PrestaShop\Module\PsAccounts\Api\Client\AccountsClient;
use PrestaShop\Module\PsAccounts\Provider\OAuth2\Oauth2Client;
use PrestaShop\Module\PsAccounts\Identity\Domain\IdentityManager;
use PrestaShop\Module\PsAccounts\Provider\ShopProvider;

class VerifyAuthenticityHandler
Expand All @@ -34,11 +34,6 @@ class VerifyAuthenticityHandler
*/
private $accountsClient;

/**
* @var Oauth2Client
*/
private $oauth2Client;

/**
* @var ShopProvider
*/
Expand All @@ -49,6 +44,11 @@ class VerifyAuthenticityHandler
*/
private $shopSession;

/**
* @var IdentityManager
*/
private $identityManager;

/**
* @var ManageProof
*/
Expand All @@ -57,21 +57,21 @@ class VerifyAuthenticityHandler
/**
* @param AccountsClient $accountsClient
* @param ShopProvider $shopProvider
* @param Oauth2Client $oauth2Client
* @param ShopSession $shopSession
* @param IdentityManager $identityManager
* @param ManageProof $manageProof
*/
public function __construct(
AccountsClient $accountsClient,
ShopProvider $shopProvider,
Oauth2Client $oauth2Client,
ShopSession $shopSession,
IdentityManager $identityManager,
ManageProof $manageProof
) {
$this->accountsClient = $accountsClient;
$this->shopProvider = $shopProvider;
$this->oauth2Client = $oauth2Client;
$this->shopSession = $shopSession;
$this->identityManager = $identityManager;
$this->manageProof = $manageProof;
}

Expand All @@ -85,7 +85,9 @@ public function __construct(
*/
public function handle(VerifyAuthenticityCommand $command)
{
if (!$this->oauth2Client->exists()) {
$identity = $this->identityManager->get($command->shopId);

if (!$identity->hasOAuth2Client()) {
// TODO: call Create Identity Command ? or just log ? or throw ? or juste remove this condition ?
return;
}
Expand All @@ -95,16 +97,18 @@ public function handle(VerifyAuthenticityCommand $command)

$proof = $this->manageProof->generateProof();

$currentShop = $this->shopProvider->getCurrentShop();

$response = $this->accountsClient->verifyUrlAuthenticity(
$currentShop['uuid'],
$identity->cloudShopId(),
$token,
$this->shopProvider->getUrl($command->shopId),
$proof
);
if ($response['status'] === true && $response['body']) {
// TODO: get the first token with verified scope or clear the token in configuration table ?
$identity->verify();

$this->identityManager->save($identity);

// TODO: or just get the first token with verified scope or clear the token in configuration table ?
} else {
// TODO Add bad request handling here
}
Expand Down
116 changes: 116 additions & 0 deletions src/Identity/Domain/Identity.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
<?php

namespace PrestaShop\Module\PsAccounts\Identity\Domain;

use Error;

class Identity
{
/**
* @var string
*/
private $shopId;

/**
* @var string|null
*/
private $cloudShopId;

/**
* @var Oauth2Client|null
*/
private $oauth2Client;

/**
* Identity constructor
*
* @param string $shopId
* @param string|null $cloudShopId
* @param Oauth2Client|null $oauth2Client
*/
public function __construct($shopId, $cloudShopId = null, Oauth2Client $oauth2Client = null)
{
$this->shopId = $shopId;
$this->cloudShopId = $cloudShopId;
$this->oauth2Client = $oauth2Client;
}

/**
* @return void
*/
public function create($shopId, $cloudShopId, Oauth2Client $oauth2Client)
{
if ($this->cloudShopId && $this->hasOAuth2Client()) {
throw new Error('The store already have an identity');
}

$this->shopId = $shopId;
$this->cloudShopId = $cloudShopId;
$this->oauth2Client = $oauth2Client;

// $this->record(new IdentityCreated($this->id, $this->oauth2Client));
}

/**
* @return void
*/
public function verify()
{
if (!$this->cloudShopId || !$this->hasOAuth2Client()) {
throw new Error('The store does not have an identity');
}

// $this->record(new IdentityVerified($this->id));
}

/**
* @return string
*/
public function shopId()
{
return $this->shopId;
}

/**
* @return string
*/
public function cloudShopId()
{
return $this->cloudShopId;
}

/**
* @return OAuth2Client
*/
public function oauth2Client()
{
return $this->oauth2Client;
}

/**
* @return boolean
*/
public function hasOAuth2Client()
{
return (bool) $this->oauth2Client;
}

/**
* If we want to use domain events, add this to an abstract class
*/

// private array $domainEvents = [];

// public function pullDomainEvents(): array
// {
// $domainEvents = $this->domainEvents;
// $this->domainEvents = [];

// return $domainEvents;
// }

// protected function record(DomainEvent $domainEvent): void
// {
// $this->domainEvents[] = $domainEvent;
// }
}
20 changes: 20 additions & 0 deletions src/Identity/Domain/IdentityManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace PrestaShop\Module\PsAccounts\Identity\Domain;

interface IdentityManager
{
/**
* @param string $shopId
*
* @return Identity
*/
public function get($shopId);

/**
* @param Identity $identity
*
* @return void
*/
public function save(Identity $identity);
}
43 changes: 43 additions & 0 deletions src/Identity/Domain/OAuth2Client.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace PrestaShop\Module\PsAccounts\Identity\Domain;

class OAuth2Client {
/**
* @var string
*/
private $id;

/**
* @var string
*/
private $secret;

/**
* OAuth2Client constructor
*
* @param string $id
* @param string $secret
*/
public function __construct($id, $secret)
{
$this->id = $id;
$this->secret = $secret;
}

/**
* @return string
*/
public function id()
{
return $this->id;
}

/**
* @return string
*/
public function secret()
{
return $this->secret;
}
}
Loading
Loading