Skip to content

Commit

Permalink
Merge branch 'master' into PIM-10485
Browse files Browse the repository at this point in the history
  • Loading branch information
Antoine Trouve committed Jun 23, 2022
2 parents d07ec45 + 7053476 commit 01b5d58
Show file tree
Hide file tree
Showing 22 changed files with 379 additions and 147 deletions.
8 changes: 7 additions & 1 deletion .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# This is an example with only required properties:
version: 2
updates:
# Default branch (master)
Expand All @@ -8,27 +7,31 @@ updates:
interval: "daily"
allow:
- dependency-type: "production"
open-pull-requests-limit: 0

- package-ecosystem: "npm"
directory: "/front-packages/akeneo-design-system"
schedule:
interval: "daily"
allow:
- dependency-type: "production"
open-pull-requests-limit: 0

- package-ecosystem: "npm"
directory: "/front-packages/shared"
schedule:
interval: "daily"
allow:
- dependency-type: "production"
open-pull-requests-limit: 0

- package-ecosystem: "npm"
directory: "/front-packages/measurement"
schedule:
interval: "daily"
allow:
- dependency-type: "production"
open-pull-requests-limit: 0

- package-ecosystem: "composer"
directory: "/"
Expand All @@ -45,6 +48,7 @@ updates:
target-branch: "5.0"
allow:
- dependency-type: "production"
open-pull-requests-limit: 0

- package-ecosystem: "npm"
directory: "/front-packages/akeneo-design-system"
Expand All @@ -53,6 +57,7 @@ updates:
target-branch: "5.0"
allow:
- dependency-type: "production"
open-pull-requests-limit: 0

- package-ecosystem: "composer"
directory: "/"
Expand All @@ -70,6 +75,7 @@ updates:
target-branch: "4.0"
allow:
- dependency-type: "production"
open-pull-requests-limit: 0

- package-ecosystem: "composer"
directory: "/"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,11 @@ public function handle(SendBusinessEventToWebhooksCommand $command): void
foreach ($webhooks as $webhook) {
$user = $this->webhookUserAuthenticator->authenticate($webhook->userId());

$filteredPimEventBulk = $this->filterConnectionOwnEvents($webhook, $user->getUsername(), $pimEventBulk);
$filteredPimEventBulk = $this->filterConnectionOwnEvents(
$webhook,
$user->getUserIdentifier(),
$pimEventBulk
);
if (null === $filteredPimEventBulk) {
continue;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,5 +78,9 @@ public function load(array $configs, ContainerBuilder $container): void
$loader->load('Webhook/validators.yml');

$loader->load('services.yml');

if ('test' === $container->getParameter('kernel.environment')) {
$loader->load('Webhook/services_test.yml');
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,12 @@ services:
- '@Akeneo\Connectivity\Connection\Infrastructure\Webhook\EventsApiDebug\Persistence\PurgeEventsApiErrorLogsQuery'
tags:
- {name: 'console.command'}

Akeneo\Connectivity\Connection\Infrastructure\Webhook\Command\SendBusinessEventToWebhooks:
arguments:
- '@Akeneo\Platform\Component\EventQueue\BulkEventNormalizer'
- '@Akeneo\Connectivity\Connection\Application\Webhook\Command\SendBusinessEventToWebhooksHandler'
- '@event_dispatcher'
tags:
- { name: console.command }

Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
services:
Akeneo\Connectivity\Connection\Infrastructure\Webhook\MessageHandler\BusinessEventHandler:
arguments:
- '@Akeneo\Connectivity\Connection\Application\Webhook\Command\SendBusinessEventToWebhooksHandler'
- '@event_dispatcher'
- '%kernel.project_dir%'
- '@logger'
- '@Akeneo\Platform\Component\EventQueue\BulkEventNormalizer'
tags: [messenger.message_handler]
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
services:
Akeneo\Connectivity\Connection\Tests\EndToEnd\GuzzleJsonHistory:
arguments:
- '%kernel.project_dir%/var/cache/test/guzzle_history.json'

akeneo_connectivity.connection.webhook.guzzle_handler:
class: 'Akeneo\Connectivity\Connection\Tests\EndToEnd\GuzzleMockHandlerStack'
factory: ['Akeneo\Connectivity\Connection\Tests\EndToEnd\GuzzleMockHandlerStack', 'createWithHistoryContainer']
arguments:
- '@Akeneo\Connectivity\Connection\Tests\EndToEnd\GuzzleJsonHistory'
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

declare(strict_types=1);

namespace Akeneo\Connectivity\Connection\Infrastructure\Webhook\Command;

use Akeneo\Connectivity\Connection\Application\Webhook\Command\SendBusinessEventToWebhooksCommand;
use Akeneo\Connectivity\Connection\Application\Webhook\Command\SendBusinessEventToWebhooksHandler;
use Akeneo\Connectivity\Connection\Domain\Webhook\Event\MessageProcessedEvent;
use Akeneo\Platform\Component\EventQueue\BulkEvent;
use Akeneo\Platform\Component\EventQueue\BulkEventNormalizer;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;

class SendBusinessEventToWebhooks extends Command
{
protected static $defaultName = 'akeneo:connectivity:send-business-event';
protected static $defaultDescription = 'Send business event to webhooks';

public function __construct(
private BulkEventNormalizer $bulkEventNormalizer,
private SendBusinessEventToWebhooksHandler $commandHandler,
private EventDispatcherInterface $eventDispatcher
) {
parent::__construct();
}

protected function configure()
{
$this
->setHidden(true)
->addArgument(
'message',
InputArgument::REQUIRED,
'Symfony Messenger serialized message'
);
}

/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output): int
{
$message = \json_decode($input->getArgument('message'), true);
$event = $this->bulkEventNormalizer->denormalize($message, BulkEvent::class);

$this->commandHandler->handle(new SendBusinessEventToWebhooksCommand($event));
$this->eventDispatcher->dispatch(new MessageProcessedEvent());

return Command::SUCCESS;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,65 @@

namespace Akeneo\Connectivity\Connection\Infrastructure\Webhook\MessageHandler;

use Akeneo\Connectivity\Connection\Application\Webhook\Command\SendBusinessEventToWebhooksCommand;
use Akeneo\Connectivity\Connection\Application\Webhook\Command\SendBusinessEventToWebhooksHandler;
use Akeneo\Connectivity\Connection\Domain\Webhook\Event\MessageProcessedEvent;
use Akeneo\Connectivity\Connection\Infrastructure\Webhook\Command\SendBusinessEventToWebhooks;
use Akeneo\Platform\Component\EventQueue\BulkEventInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Akeneo\Platform\Component\EventQueue\BulkEventNormalizer;
use Psr\Log\LoggerInterface;
use Symfony\Component\Messenger\Handler\MessageSubscriberInterface;
use Symfony\Component\Process\Process;

class BusinessEventHandler implements MessageSubscriberInterface
{
private SendBusinessEventToWebhooksHandler $commandHandler;
private EventDispatcherInterface $eventDispatcher;

public function __construct(
SendBusinessEventToWebhooksHandler $commandHandler,
EventDispatcherInterface $eventDispatcher
private string $projectDir,
private LoggerInterface $logger,
private BulkEventNormalizer $normalizer
) {
$this->commandHandler = $commandHandler;
$this->eventDispatcher = $eventDispatcher;
}

public static function getHandledMessages(): iterable
{
yield BulkEventInterface::class => [
'from_transport' => 'webhook'
'from_transport' => 'webhook',
];
}

public function __invoke(BulkEventInterface $event): void
{
$this->commandHandler->handle(new SendBusinessEventToWebhooksCommand($event));
$this->eventDispatcher->dispatch(new MessageProcessedEvent());
try {
$processArguments = $this->buildBatchCommand($event);

$env = [
'SYMFONY_DOTENV_VARS' => false,
];

if ($event->getTenantId()) {
$env['APP_TENANT_ID'] = $event->getTenantId();
}

$process = new Process($processArguments, null, $env);
$process->setTimeout(null);

$this->logger->debug(\sprintf('Command line: "%s"', $process->getCommandLine()));

$process->run(function ($type, $buffer): void {
\fwrite(Process::ERR === $type ? \STDERR : \STDOUT, $buffer);
});
} catch (\Throwable $t) {
$this->logger->error(
\sprintf('An error occurred: %s', $t->getMessage()),
['exception' => $t]
);
}
}

private function buildBatchCommand(BulkEventInterface $event): array
{
$message = \json_encode($this->normalizer->normalize($event));
return [
\sprintf('%s/bin/console', $this->projectDir),
SendBusinessEventToWebhooks::getDefaultName(),
$message,
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php

declare(strict_types=1);

namespace Akeneo\Connectivity\Connection\Tests\EndToEnd;

use GuzzleHttp\Psr7\Message;
use Webmozart\Assert\Assert;

/**
* @author JMLeroux <jean-marie.leroux@akeneo.com>
* @copyright 2022 Akeneo SAS (http://www.akeneo.com)
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
*
* A filesystem container to store Guzzle history in a json file.
* We need this filesytem json file to share it between process and keep track of the history in the tests
*/
class GuzzleJsonHistory implements \ArrayAccess, \Countable
{
public function __construct(private string $filepath)
{
}

public function resetHistory(): void
{
if (\file_exists($this->filepath)) {
\unlink($this->filepath);
}
}

public function offsetExists(mixed $offset)
{
$history = $this->readFile();

return isset($history[$offset]);
}

public function offsetGet(mixed $offset)
{
$history = $this->readFile();
Assert::isArray($history);

return $history[$offset];
}

public function offsetSet(mixed $offset, mixed $value)
{
$history = $this->readFile();
$history[] = [
'request' => Message::toString($value['request']),
'response' => $value['response'] ? Message::toString($value['response']) : null,
];
$this->writeFile($history);
}

public function offsetUnset(mixed $offset)
{
$history = $this->readFile();
unset($history[$offset]);
$this->writeFile($history);
}

public function count(): int
{
$history = $this->readFile();

return \count($history);
}

private function readFile(): array
{
if (!\file_exists($this->filepath)) {
return [];
}

return \json_decode(\file_get_contents($this->filepath), true);
}

private function writeFile(array $history): void
{
\file_put_contents($this->filepath, \json_encode($history));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

declare(strict_types=1);

namespace Akeneo\Connectivity\Connection\Tests\EndToEnd;

use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Middleware;
use GuzzleHttp\Psr7\Response;

/**
* @author JMLeroux <jean-marie.leroux@akeneo.com>
* @copyright 2022 Akeneo SAS (http://www.akeneo.com)
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
*
* A sub class of the Guzzle handler stack providing a JSON filesystem history.
* The purpose of this handler is to share its history between a parent process and subprocess. It is useful to keep track of the history of the webhook call in the tests. Indeed, the webhook call is performed in a subprocess.
*/
class GuzzleMockHandlerStack extends HandlerStack
{
private ?GuzzleJsonHistory $container;

public function historyContainer(): GuzzleJsonHistory
{
return $this->container;
}

public static function createWithHistoryContainer(GuzzleJsonHistory $historyContainer)
{
$stack = new self(new MockHandler([new Response(200)]));
$stack->push(Middleware::httpErrors(), 'http_errors');
$stack->push(Middleware::redirect(), 'allow_redirects');
$stack->push(Middleware::cookies(), 'cookies');
$stack->push(Middleware::prepareBody(), 'prepare_body');

$historyContainer->resetHistory();
$history = Middleware::history($historyContainer);
$stack->push($history);

$stack->container = $historyContainer;

return $stack;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ abstract class WebTestCase extends TestCase
protected function setUp(): void
{
parent::setUp();
$this->client = self::$container->get('test.client');
$this->client = static::getContainer()->get('test.client');
}

protected function createConnection(string $code, string $label, string $flowType, bool $auditable): ConnectionWithCredentials
Expand Down

0 comments on commit 01b5d58

Please sign in to comment.