From 7354e21931319b9bf961c1135a3d0abbf588b35a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joachim=20L=C3=B8vgaard?= Date: Wed, 12 Jul 2023 09:08:15 +0200 Subject: [PATCH] Add a constant with the events available in this plugin to make it easier to reference them --- src/DependencyInjection/Configuration.php | 41 +++++++++++-------- .../SetonoSyliusAnalyticsExtension.php | 18 +++----- .../SetonoSyliusAnalyticsExtensionTest.php | 30 ++++---------- 3 files changed, 37 insertions(+), 52 deletions(-) diff --git a/src/DependencyInjection/Configuration.php b/src/DependencyInjection/Configuration.php index 01f49f2..40683c0 100644 --- a/src/DependencyInjection/Configuration.php +++ b/src/DependencyInjection/Configuration.php @@ -18,33 +18,40 @@ final class Configuration implements ConfigurationInterface { + /** + * Holds the Google Analytics events available in this plugin + */ + public const EVENTS = [ + 'add_payment_info', + 'add_shipping_info', + 'add_to_cart', + 'begin_checkout', + 'purchase', + 'view_cart', + 'view_item_list', + 'view_item', + ]; + public function getConfigTreeBuilder(): TreeBuilder { $treeBuilder = new TreeBuilder('setono_sylius_analytics'); $rootNode = $treeBuilder->getRootNode(); - /** @psalm-suppress MixedMethodCall,PossiblyNullReference,UndefinedInterfaceMethod */ - $rootNode - ->addDefaultsIfNotSet() - ->children() - ->arrayNode('events') - ->addDefaultsIfNotSet() - ->children() - ->booleanNode('add_payment_info')->defaultTrue()->end() - ->booleanNode('add_shipping_info')->defaultTrue()->end() - ->booleanNode('add_to_cart')->defaultTrue()->end() - ->booleanNode('begin_checkout')->defaultTrue()->end() - ->booleanNode('purchase')->defaultTrue()->end() - ->booleanNode('view_cart')->defaultTrue()->end() - ->booleanNode('view_item_list')->defaultTrue()->end() - ->booleanNode('view_item')->defaultTrue()->end() - ; - + $this->addEventsSection($rootNode); $this->addResourcesSection($rootNode); return $treeBuilder; } + private function addEventsSection(ArrayNodeDefinition $node): void + { + $eventsNode = $node->addDefaultsIfNotSet()->children()->arrayNode('events')->addDefaultsIfNotSet()->children(); + + foreach (self::EVENTS as $event) { + $eventsNode->booleanNode($event)->defaultTrue(); + } + } + private function addResourcesSection(ArrayNodeDefinition $node): void { /** @psalm-suppress MixedMethodCall,PossiblyNullReference,UndefinedInterfaceMethod */ diff --git a/src/DependencyInjection/SetonoSyliusAnalyticsExtension.php b/src/DependencyInjection/SetonoSyliusAnalyticsExtension.php index 163a2fa..0510842 100644 --- a/src/DependencyInjection/SetonoSyliusAnalyticsExtension.php +++ b/src/DependencyInjection/SetonoSyliusAnalyticsExtension.php @@ -28,20 +28,12 @@ public function load(array $configs, ContainerBuilder $container): void $loader->load('services.xml'); - $events = [ - 'add_payment_info', - 'add_shipping_info', - 'add_to_cart', - 'begin_checkout', - 'purchase', - 'view_cart', - 'view_item_list', - 'view_item', - ]; - foreach ($events as $event) { - if (isset($config['events'][$event]) && true === $config['events'][$event]) { - $loader->load('services/conditional/events/' . $event . '.xml'); + foreach ($config['events'] as $event => $enabled) { + if (!$enabled) { + continue; } + + $loader->load('services/conditional/events/' . $event . '.xml'); } $this->registerResources( diff --git a/tests/DependencyInjection/SetonoSyliusAnalyticsExtensionTest.php b/tests/DependencyInjection/SetonoSyliusAnalyticsExtensionTest.php index 4e0ec02..6101a38 100644 --- a/tests/DependencyInjection/SetonoSyliusAnalyticsExtensionTest.php +++ b/tests/DependencyInjection/SetonoSyliusAnalyticsExtensionTest.php @@ -5,6 +5,7 @@ namespace Tests\Setono\SyliusAnalyticsPlugin\DependencyInjection; use Matthias\SymfonyDependencyInjectionTest\PhpUnit\AbstractExtensionTestCase; +use Setono\SyliusAnalyticsPlugin\DependencyInjection\Configuration; use Setono\SyliusAnalyticsPlugin\DependencyInjection\SetonoSyliusAnalyticsExtension; use Sylius\Bundle\ResourceBundle\SyliusResourceBundle; @@ -24,16 +25,12 @@ public function after_loading_the_correct_parameter_has_been_set(): void { $this->load(); - $this->assertContainerBuilderHasParameter('setono_sylius_analytics.events', [ - 'add_payment_info' => true, - 'add_shipping_info' => true, - 'add_to_cart' => true, - 'begin_checkout' => true, - 'purchase' => true, - 'view_cart' => true, - 'view_item_list' => true, - 'view_item' => true, - ]); + $events = []; + foreach (Configuration::EVENTS as $event) { + $events[$event] = true; + } + + $this->assertContainerBuilderHasParameter('setono_sylius_analytics.events', $events); $this->assertContainerBuilderHasParameter('setono_sylius_analytics.driver', SyliusResourceBundle::DRIVER_DOCTRINE_ORM); } @@ -44,18 +41,7 @@ public function event_subscribers_are_registered_by_default(): void { $this->load(); - $events = [ - 'add_payment_info', - 'add_shipping_info', - 'add_to_cart', - 'begin_checkout', - 'purchase', - 'view_cart', - 'view_item_list', - 'view_item', - ]; - - foreach ($events as $event) { + foreach (Configuration::EVENTS as $event) { $this->assertContainerBuilderHasService(sprintf('setono_sylius_analytics.event_subscriber.%s', $event)); } }