-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ACP-3963 Algolia Event-Based Features. (#11158)
ACP-3963 Algolia Event-Based Features.
- Loading branch information
Showing
18 changed files
with
409 additions
and
2 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
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
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
52 changes: 52 additions & 0 deletions
52
...er/Yves/Customer/Plugin/EventDispatcher/AnonymousIdSessionAssignEventDispatcherPlugin.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,52 @@ | ||
<?php | ||
|
||
/** | ||
* Copyright © 2016-present Spryker Systems GmbH. All rights reserved. | ||
* Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file. | ||
*/ | ||
|
||
namespace Spryker\Yves\Customer\Plugin\EventDispatcher; | ||
|
||
use Spryker\Service\Container\ContainerInterface; | ||
use Spryker\Shared\Customer\CustomerConfig; | ||
use Spryker\Shared\EventDispatcher\EventDispatcherInterface; | ||
use Spryker\Shared\EventDispatcherExtension\Dependency\Plugin\EventDispatcherPluginInterface; | ||
use Spryker\Yves\Kernel\AbstractPlugin; | ||
use Symfony\Component\HttpKernel\Event\RequestEvent; | ||
use Symfony\Component\HttpKernel\KernelEvents; | ||
|
||
/** | ||
* @method \Spryker\Yves\Customer\CustomerFactory getFactory() | ||
*/ | ||
class AnonymousIdSessionAssignEventDispatcherPlugin extends AbstractPlugin implements EventDispatcherPluginInterface | ||
{ | ||
/** | ||
* @var int | ||
*/ | ||
protected const LISTENER_PRIORITY = 8; | ||
|
||
/** | ||
* @param \Spryker\Shared\EventDispatcher\EventDispatcherInterface $eventDispatcher | ||
* @param \Spryker\Service\Container\ContainerInterface $container | ||
* | ||
* @return \Spryker\Shared\EventDispatcher\EventDispatcherInterface | ||
*/ | ||
public function extend(EventDispatcherInterface $eventDispatcher, ContainerInterface $container): EventDispatcherInterface | ||
{ | ||
$eventDispatcher->addListener(KernelEvents::REQUEST, function (RequestEvent $event) { | ||
if (!$event->isMainRequest()) { | ||
return; | ||
} | ||
|
||
$request = $event->getRequest(); | ||
$session = $request->getSession(); | ||
|
||
$anonymousId = $session->get(CustomerConfig::ANONYMOUS_SESSION_KEY); | ||
if ($anonymousId === null) { | ||
$session->set(CustomerConfig::ANONYMOUS_SESSION_KEY, $this->getFactory()->createAnonymousIdProvider()->generateUniqueId()); | ||
} | ||
}, static::LISTENER_PRIORITY); | ||
|
||
return $eventDispatcher; | ||
} | ||
} |
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,31 @@ | ||
<?php | ||
|
||
/** | ||
* Copyright © 2016-present Spryker Systems GmbH. All rights reserved. | ||
* Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file. | ||
*/ | ||
|
||
namespace Spryker\Yves\Customer\Session; | ||
|
||
use Spryker\Service\UtilText\UtilTextServiceInterface; | ||
|
||
class AnonymousIdProvider implements AnonymousIdProviderInterface | ||
{ | ||
protected UtilTextServiceInterface $utilTextService; | ||
|
||
/** | ||
* @param \Spryker\Service\UtilText\UtilTextServiceInterface $utilTextService | ||
*/ | ||
public function __construct(UtilTextServiceInterface $utilTextService) | ||
{ | ||
$this->utilTextService = $utilTextService; | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function generateUniqueId(): string | ||
{ | ||
return 'anonymous-' . $this->utilTextService->generateRandomString(16); | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
src/Spryker/Yves/Customer/Session/AnonymousIdProviderInterface.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,16 @@ | ||
<?php | ||
|
||
/** | ||
* Copyright © 2016-present Spryker Systems GmbH. All rights reserved. | ||
* Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file. | ||
*/ | ||
|
||
namespace Spryker\Yves\Customer\Session; | ||
|
||
interface AnonymousIdProviderInterface | ||
{ | ||
/** | ||
* @return string | ||
*/ | ||
public function generateUniqueId(): string; | ||
} |
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,77 @@ | ||
<?php | ||
|
||
/** | ||
* Copyright © 2016-present Spryker Systems GmbH. All rights reserved. | ||
* Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file. | ||
*/ | ||
|
||
namespace SprykerTest\Client\Customer; | ||
|
||
use Codeception\Test\Unit; | ||
use Generated\Shared\Transfer\CustomerTransfer; | ||
use Spryker\Client\Customer\CustomerClient; | ||
use Spryker\Client\Customer\Session\CustomerSession; | ||
use Spryker\Shared\Customer\CustomerConfig; | ||
|
||
/** | ||
* Auto-generated group annotations | ||
* | ||
* @group SprykerTest | ||
* @group Client | ||
* @group Customer | ||
* @group CustomerClientTest | ||
* Add your own group annotations below this line | ||
*/ | ||
class CustomerClientTest extends Unit | ||
{ | ||
/** | ||
* @var \SprykerTest\Client\Customer\CustomerClientTester | ||
*/ | ||
protected $tester; | ||
|
||
/** | ||
* @return void | ||
*/ | ||
public function testGetUserIdentifierReturnsTheAnonymousIdFromTheSession(): void | ||
{ | ||
// Arrange | ||
$this->tester->mockFactoryMethod( | ||
'createSessionCustomerSession', | ||
new CustomerSession( | ||
$this->tester->getSessionClientMock([CustomerConfig::ANONYMOUS_SESSION_KEY => 'anonymous:123']), | ||
), | ||
); | ||
|
||
$customerClient = new CustomerClient(); | ||
$customerClient->setFactory($this->tester->getFactory()); | ||
|
||
// Act | ||
$userIdentifier = $customerClient->getUserIdentifier(); | ||
|
||
// Assert | ||
$this->assertSame('anonymous:123', $userIdentifier); | ||
} | ||
|
||
/** | ||
* @return void | ||
*/ | ||
public function testGetUserIdentifierReturnsTheCustomerReferenceFromTheSession(): void | ||
{ | ||
// Arrange | ||
$this->tester->mockFactoryMethod( | ||
'createSessionCustomerSession', | ||
new CustomerSession( | ||
$this->tester->getSessionClientMock([CustomerSession::SESSION_KEY => (new CustomerTransfer())->setCustomerReference('registered:123')]), | ||
), | ||
); | ||
|
||
$customerClient = new CustomerClient(); | ||
$customerClient->setFactory($this->tester->getFactory()); | ||
|
||
// Act | ||
$userIdentifier = $customerClient->getUserIdentifier(); | ||
|
||
// Assert | ||
$this->assertSame('registered:123', $userIdentifier); | ||
} | ||
} |
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
Oops, something went wrong.