Skip to content

Commit

Permalink
Add a constant with the events available in this plugin to make it ea…
Browse files Browse the repository at this point in the history
…sier to reference them
  • Loading branch information
loevgaard committed Jul 12, 2023
1 parent fbbfb15 commit 7354e21
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 52 deletions.
41 changes: 24 additions & 17 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down
18 changes: 5 additions & 13 deletions src/DependencyInjection/SetonoSyliusAnalyticsExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
30 changes: 8 additions & 22 deletions tests/DependencyInjection/SetonoSyliusAnalyticsExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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);
}

Expand All @@ -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));
}
}
Expand Down

0 comments on commit 7354e21

Please sign in to comment.