Spaces:
No application file
No application file
File size: 4,001 Bytes
d2897cd |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
<?php
declare(strict_types=1);
namespace Mautic\IntegrationsBundle\Helper;
use Mautic\IntegrationsBundle\Event\KeysDecryptionEvent;
use Mautic\IntegrationsBundle\Event\KeysEncryptionEvent;
use Mautic\IntegrationsBundle\Exception\IntegrationNotFoundException;
use Mautic\IntegrationsBundle\Facade\EncryptionService;
use Mautic\IntegrationsBundle\Integration\Interfaces\IntegrationInterface;
use Mautic\IntegrationsBundle\IntegrationEvents;
use Mautic\PluginBundle\Entity\Integration;
use Mautic\PluginBundle\Entity\IntegrationRepository;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
class IntegrationsHelper
{
/**
* @var IntegrationInterface[]
*/
private array $integrations = [];
private array $decryptedIntegrationConfigurations = [];
public function __construct(
private IntegrationRepository $integrationRepository,
private EncryptionService $encryptionService,
private EventDispatcherInterface $eventDispatcher
) {
}
public function addIntegration(IntegrationInterface $integration): void
{
$this->integrations[$integration->getName()] = $integration;
}
/**
* @return IntegrationInterface
*
* @throws IntegrationNotFoundException
*/
public function getIntegration(string $integration)
{
if (!isset($this->integrations[$integration])) {
throw new IntegrationNotFoundException("$integration either doesn't exist or has not been tagged with mautic.basic_integration");
}
// Ensure the configuration is hydrated
$this->getIntegrationConfiguration($this->integrations[$integration]);
return $this->integrations[$integration];
}
public function saveIntegrationConfiguration(Integration $configuration): void
{
// Encrypt the keys before saving
$decryptedApiKeys = $configuration->getApiKeys();
// Dispatch event before encryption
$encryptionEvent = new KeysEncryptionEvent($configuration, $decryptedApiKeys);
$this->eventDispatcher->dispatch($encryptionEvent, IntegrationEvents::INTEGRATION_KEYS_BEFORE_ENCRYPTION);
// Encrypt and store the keys
$encryptedApiKeys = $this->encryptionService->encrypt($encryptionEvent->getKeys());
$configuration->setApiKeys($encryptedApiKeys);
// Save
$this->integrationRepository->saveEntity($configuration);
// Restore decrypted for use
$configuration->setApiKeys($decryptedApiKeys);
}
/**
* @throws IntegrationNotFoundException
*/
public function getIntegrationConfiguration(IntegrationInterface $integration): Integration
{
if (!$integration->hasIntegrationConfiguration()) {
/** @var Integration $configuration */
$configuration = $this->integrationRepository->findOneByName($integration->getName());
if (!$configuration) {
throw new IntegrationNotFoundException("{$integration->getName()} doesn't exist in the database");
}
$integration->setIntegrationConfiguration($configuration);
}
// Make sure the keys are decrypted
if (!isset($this->decryptedIntegrationConfigurations[$integration->getName()])) {
$configuration = $integration->getIntegrationConfiguration();
$encryptedApiKeys = $configuration->getApiKeys();
$decryptedApiKeys = $this->encryptionService->decrypt($encryptedApiKeys);
// Dispatch event after decryption
$decryptionEvent = new KeysDecryptionEvent($configuration, $decryptedApiKeys);
$this->eventDispatcher->dispatch($decryptionEvent, IntegrationEvents::INTEGRATION_KEYS_AFTER_DECRYPTION);
$configuration->setApiKeys($decryptionEvent->getKeys());
$this->decryptedIntegrationConfigurations[$integration->getName()] = true;
}
return $integration->getIntegrationConfiguration();
}
}
|