diff --git a/.env b/.env index 4c6ee4ca6..cc23653bd 100644 --- a/.env +++ b/.env @@ -88,3 +88,7 @@ REDIS_HOST=redis REDIS_PORT=6379 REDIS_URL=redis://${REDIS_HOST}:${REDIS_PORT} ###< chill-project/chill-bundles ### + +###> symfony/ovh-cloud-notifier ### +# OVHCLOUD_DSN=ovhcloud://APPLICATION_KEY:APPLICATION_SECRET@default?consumer_key=CONSUMER_KEY&service_name=SERVICE_NAME +###< symfony/ovh-cloud-notifier ### diff --git a/composer.json b/composer.json index 18ff0f222..3ef287196 100644 --- a/composer.json +++ b/composer.json @@ -60,6 +60,7 @@ "symfony/monolog-bundle": "^3.5", "symfony/notifier": "^5.4", "symfony/options-resolver": "^5.4", + "symfony/ovh-cloud-notifier": "^5.4", "symfony/process": "^5.4", "symfony/property-access": "^5.4", "symfony/property-info": "^5.4", diff --git a/config/packages/notifier.yaml b/config/packages/notifier.yaml index 968b8aa7f..1b474455a 100644 --- a/config/packages/notifier.yaml +++ b/config/packages/notifier.yaml @@ -2,7 +2,7 @@ framework: notifier: texter_transports: #ovhcloud: '%env(OVHCLOUD_DSN)%' - ovhcloud: '%env(SHORT_MESSAGE_DSN)%' + #ovhcloud: '%env(SHORT_MESSAGE_DSN)%' channel_policy: # use chat/slack, chat/telegram, sms/twilio or sms/nexmo urgent: ['email'] diff --git a/rector.php b/rector.php index 126c814e9..3923d37e4 100644 --- a/rector.php +++ b/rector.php @@ -20,6 +20,10 @@ return static function (RectorConfig $rectorConfig): void { __DIR__ . '/src', ]); + $rectorConfig->skip([ + \Rector\Php55\Rector\String_\StringClassNameToClassConstantRector::class => __DIR__ . 'src/Bundle/ChillMainBundle/Service/Notifier/LegacyOvhCloudFactory.php' + ]); + $rectorConfig->symfonyContainerXml(__DIR__ . '/var/cache/dev/test/App_KernelTestDebugContainer.xml '); $rectorConfig->symfonyContainerPhp(__DIR__ . '/tests/symfony-container.php'); diff --git a/src/Bundle/ChillMainBundle/ChillMainBundle.php b/src/Bundle/ChillMainBundle/ChillMainBundle.php index ff721784a..bca92b85c 100644 --- a/src/Bundle/ChillMainBundle/ChillMainBundle.php +++ b/src/Bundle/ChillMainBundle/ChillMainBundle.php @@ -18,7 +18,6 @@ use Chill\MainBundle\DependencyInjection\CompilerPass\ExportsCompilerPass; use Chill\MainBundle\DependencyInjection\CompilerPass\MenuCompilerPass; use Chill\MainBundle\DependencyInjection\CompilerPass\NotificationCounterCompilerPass; use Chill\MainBundle\DependencyInjection\CompilerPass\SearchableServicesCompilerPass; -use Chill\MainBundle\DependencyInjection\CompilerPass\ShortMessageCompilerPass; use Chill\MainBundle\DependencyInjection\CompilerPass\TimelineCompilerClass; use Chill\MainBundle\DependencyInjection\CompilerPass\WidgetsCompilerPass; use Chill\MainBundle\DependencyInjection\ConfigConsistencyCompilerPass; @@ -73,6 +72,5 @@ class ChillMainBundle extends Bundle $container->addCompilerPass(new MenuCompilerPass(), \Symfony\Component\DependencyInjection\Compiler\PassConfig::TYPE_BEFORE_OPTIMIZATION, 0); $container->addCompilerPass(new ACLFlagsCompilerPass(), \Symfony\Component\DependencyInjection\Compiler\PassConfig::TYPE_BEFORE_OPTIMIZATION, 0); $container->addCompilerPass(new CRUDControllerCompilerPass(), \Symfony\Component\DependencyInjection\Compiler\PassConfig::TYPE_BEFORE_OPTIMIZATION, 0); - $container->addCompilerPass(new ShortMessageCompilerPass(), \Symfony\Component\DependencyInjection\Compiler\PassConfig::TYPE_BEFORE_OPTIMIZATION, 0); } } diff --git a/src/Bundle/ChillMainBundle/DependencyInjection/ChillMainExtension.php b/src/Bundle/ChillMainBundle/DependencyInjection/ChillMainExtension.php index 9a005be4a..2e9b7c029 100644 --- a/src/Bundle/ChillMainBundle/DependencyInjection/ChillMainExtension.php +++ b/src/Bundle/ChillMainBundle/DependencyInjection/ChillMainExtension.php @@ -234,6 +234,8 @@ class ChillMainExtension extends Extension implements public function prepend(ContainerBuilder $container) { + $this->prependNotifierTexterWithLegacyData($container); + // add installation_name and date_format to globals $chillMainConfig = $container->getExtensionConfig($this->getAlias()); $config = $this->processConfiguration($this @@ -357,6 +359,44 @@ class ChillMainExtension extends Extension implements // Note: the controller are loaded inside compiler pass } + /** + * This method prepend framework configuration with legacy configuration from "ovhCloudTransporter". + * + * It can be safely removed when the option chill_main.short_message.dsn will be removed. + */ + private function prependNotifierTexterWithLegacyData(ContainerBuilder $container): void + { + $configs = $container->getExtensionConfig('chill_main'); + $notifierSet = false; + foreach (array_reverse($configs) as $config) { + if (!array_key_exists('short_messages', $config)) { + continue; + } + + if (array_key_exists('dsn', $config['short_messages'])) { + $container->prependExtensionConfig('framework', [ + 'notifier' => [ + 'texter_transports' => [ + 'ovh_legacy' => $config['short_messages']['dsn'], + ], + ], + ]); + $notifierSet = true; + } + } + if (!$notifierSet) { + $container->prependExtensionConfig('framework', [ + 'notifier' => [ + 'texter_transports' => [ + 'dummy' => 'null://null', + ], + ], + ]); + } + + + } + protected function prependCruds(ContainerBuilder $container) { $container->prependExtensionConfig('chill_main', [ diff --git a/src/Bundle/ChillMainBundle/DependencyInjection/CompilerPass/ShortMessageCompilerPass.php b/src/Bundle/ChillMainBundle/DependencyInjection/CompilerPass/ShortMessageCompilerPass.php deleted file mode 100644 index 5a974c926..000000000 --- a/src/Bundle/ChillMainBundle/DependencyInjection/CompilerPass/ShortMessageCompilerPass.php +++ /dev/null @@ -1,92 +0,0 @@ -resolveEnvPlaceholders($container->getParameter('chill_main.short_messages'), true); - // weird fix for special characters - $config['dsn'] = str_replace(['%%'], ['%'], (string) $config['dsn']); - $dsn = parse_url($config['dsn']); - parse_str($dsn['query'] ?? '', $dsn['queries']); - - if ('null' === $dsn['scheme'] || false === $config['enabled']) { - $defaultTransporter = new Reference(NullShortMessageSender::class); - } elseif ('ovh' === $dsn['scheme']) { - if (!class_exists('\\'.\Ovh\Api::class)) { - throw new RuntimeException('Class \Ovh\Api not found'); - } - - foreach (['user', 'host', 'pass'] as $component) { - if (!\array_key_exists($component, $dsn)) { - throw new RuntimeException(sprintf('The component %s does not exist in dsn. Please provide a dsn like ovh://applicationKey:applicationSecret@endpoint?consumerKey=xxxx&sender=yyyy&service_name=zzzz', $component)); - } - - $container->setParameter('chill_main.short_messages.ovh_config_'.$component, $dsn[$component]); - } - - foreach (['consumer_key', 'sender', 'service_name'] as $param) { - if (!\array_key_exists($param, $dsn['queries'])) { - throw new RuntimeException(sprintf('The parameter %s does not exist in dsn. Please provide a dsn like ovh://applicationKey:applicationSecret@endpoint?consumerKey=xxxx&sender=yyyy&service_name=zzzz', $param)); - } - $container->setParameter('chill_main.short_messages.ovh_config_'.$param, $dsn['queries'][$param]); - } - - $ovh = new Definition(); - $ovh - ->setClass('\\'.\Ovh\Api::class) - ->setArgument(0, $dsn['user']) - ->setArgument(1, $dsn['pass']) - ->setArgument(2, $dsn['host']) - ->setArgument(3, $dsn['queries']['consumer_key']); - $container->setDefinition(\Ovh\Api::class, $ovh); - - $ovhSender = new Definition(); - $ovhSender - ->setClass(OvhShortMessageSender::class) - ->setArgument(0, new Reference(\Ovh\Api::class)) - ->setArgument(1, $dsn['queries']['service_name']) - ->setArgument(2, $dsn['queries']['sender']) - ->setArgument(3, new Reference(LoggerInterface::class)) - ->setArgument(4, new Reference(PhoneNumberUtil::class)); - $container->setDefinition(OvhShortMessageSender::class, $ovhSender); - - $defaultTransporter = new Reference(OvhShortMessageSender::class); - } else { - throw new RuntimeException(sprintf('Cannot find a sender for this dsn: %s', $config['dsn'])); - } - - $container->getDefinition(ShortMessageTransporter::class) - ->setArgument(0, $defaultTransporter); - } -} diff --git a/src/Bundle/ChillMainBundle/DependencyInjection/Configuration.php b/src/Bundle/ChillMainBundle/DependencyInjection/Configuration.php index b1bb3951f..6625488dd 100644 --- a/src/Bundle/ChillMainBundle/DependencyInjection/Configuration.php +++ b/src/Bundle/ChillMainBundle/DependencyInjection/Configuration.php @@ -123,6 +123,7 @@ class Configuration implements ConfigurationInterface ->end() ->end() ->arrayNode('short_messages') + ->setDeprecated('chill-project/chill-bundles', '3.7.0', 'Since 3.7.0, Chill use the Notifier component to send message. Configure the notifier instead. In the meantime, the previous available OVH configuration will be append to the notifier component.') ->canBeEnabled() ->children() ->scalarNode('dsn')->cannotBeEmpty()->defaultValue('null://null') diff --git a/src/Bundle/ChillMainBundle/Service/Notifier/LegacyOvhCloudFactory.php b/src/Bundle/ChillMainBundle/Service/Notifier/LegacyOvhCloudFactory.php new file mode 100644 index 000000000..5b5a5faf3 --- /dev/null +++ b/src/Bundle/ChillMainBundle/Service/Notifier/LegacyOvhCloudFactory.php @@ -0,0 +1,54 @@ +getScheme(); + + if ('ovh' !== $scheme) { + throw new UnsupportedSchemeException($dsn, 'ovh', $this->getSupportedSchemes()); + } + + if (!class_exists($class = '\Symfony\Component\Notifier\Bridge\OvhCloud\OvhCloudTransport')) { + throw new \RuntimeException(sprintf('The class %s is missing, please add the dependency with "composer require symfony/ovh-cloud-notifier".', $class)); + } + + $applicationKey = $this->getUser($dsn); + $applicationSecret = $this->getPassword($dsn); + $consumerKey = $dsn->getRequiredOption('consumer_key'); + $serviceName = $dsn->getRequiredOption('service_name'); + $sender = $dsn->getOption('sender'); + $host = null; + $port = $dsn->getPort(); + + return (new $class($applicationKey, $applicationSecret, $consumerKey, $serviceName, $this->client, $this->dispatcher)) + ->setHost($host)->setPort($port)->setSender($sender); + } +} diff --git a/src/Bundle/ChillMainBundle/Service/ShortMessage/NullShortMessageSender.php b/src/Bundle/ChillMainBundle/Service/ShortMessage/NullShortMessageSender.php deleted file mode 100644 index 16bc87790..000000000 --- a/src/Bundle/ChillMainBundle/Service/ShortMessage/NullShortMessageSender.php +++ /dev/null @@ -1,24 +0,0 @@ -phoneNumberUtil = PhoneNumberUtil::getInstance(); + } public function __invoke(ShortMessage $message): void { - $this->messageTransporter->send($message); + trigger_deprecation('Chill-project/chill-bundles', '3.7.0', 'Send message using Notifier component'); + + $this->texter->send( + new SmsMessage( + $this->phoneNumberUtil->format($message->getPhoneNumber(), PhoneNumberFormat::E164), + $message->getContent(), + ), + ); } } diff --git a/src/Bundle/ChillMainBundle/Service/ShortMessage/ShortMessageSenderInterface.php b/src/Bundle/ChillMainBundle/Service/ShortMessage/ShortMessageSenderInterface.php deleted file mode 100644 index 00e003709..000000000 --- a/src/Bundle/ChillMainBundle/Service/ShortMessage/ShortMessageSenderInterface.php +++ /dev/null @@ -1,24 +0,0 @@ -sender->send($shortMessage); - } -} diff --git a/src/Bundle/ChillMainBundle/Service/ShortMessage/ShortMessageTransporterInterface.php b/src/Bundle/ChillMainBundle/Service/ShortMessage/ShortMessageTransporterInterface.php deleted file mode 100644 index b8832f507..000000000 --- a/src/Bundle/ChillMainBundle/Service/ShortMessage/ShortMessageTransporterInterface.php +++ /dev/null @@ -1,24 +0,0 @@ -phoneNumberUtil->format($shortMessage->getPhoneNumber(), PhoneNumberFormat::E164); - - $response = $this->api->post( - strtr('/sms/{serviceName}/jobs', ['{serviceName}' => $this->serviceName]), - [ - 'message' => $shortMessage->getContent(), - 'receivers' => [$receiver], - 'sender' => $this->sender, - 'noStopClause' => true, - 'coding' => '7bit', - 'charset' => 'UTF-8', - 'priority' => $shortMessage->getPriority(), - ] - ); - - $improved = array_merge([ - 'validReceiversI' => implode(',', $response['validReceivers']), - 'idsI' => implode(',', $response['ids']), - ], $response); - - $this->logger->warning('[sms] a sms was sent', $improved); - } -} diff --git a/symfony.lock b/symfony.lock index 7d33810ea..5aac276ab 100644 --- a/symfony.lock +++ b/symfony.lock @@ -308,6 +308,15 @@ "config/packages/notifier.yaml" ] }, + "symfony/ovh-cloud-notifier": { + "version": "5.4", + "recipe": { + "repo": "github.com/symfony/recipes", + "branch": "main", + "version": "5.1", + "ref": "fe2e382c22d60eae9ad54cb22862b1c15291fdf8" + } + }, "symfony/phpunit-bridge": { "version": "7.1", "recipe": {