Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions UPGRADE.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,12 @@ anything to `framework.messenger`. `SendEvent` is dispatched on your application
`?ConsentContextInterface $consentContext` and `bool $consentEnabled` / `bool $clientSideEnabled` /
`bool $serverSideEnabled` arguments. Adapt subclasses, decorators and custom service definitions.

## Exception types

Enabling client side tracking without the tag bag bundle now throws `\LogicException` instead of Webmozart's
`\InvalidArgumentException`, which is what Symfony uses for "this bundle needs that bundle". Adjust your test if you
asserted on the old type.

## Removed container parameters

`setono_meta_conversions_api.client_side.enabled` and `setono_meta_conversions_api.server_side.enabled` are gone. No
Expand Down
11 changes: 8 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,11 @@
"symfony/config": "^6.4 || ^7.4",
"symfony/dependency-injection": "^6.4 || ^7.4",
"symfony/event-dispatcher": "^6.4 || ^7.4",
"symfony/event-dispatcher-contracts": "^2.5 || ^3.0",
"symfony/event-dispatcher-contracts": "^3.0",
"symfony/http-foundation": "^6.4 || ^7.4",
"symfony/http-kernel": "^6.4 || ^7.4",
"symfony/messenger": "^6.4 || ^7.4",
"symfony/service-contracts": "^2.5 || ^3.0",
"webmozart/assert": "^1.11"
"symfony/service-contracts": "^2.5 || ^3.0"
},
"require-dev": {
"ergebnis/composer-normalize": "^2.50",
Expand All @@ -48,6 +47,12 @@
"symfony/http-client": "^6.4 || ^7.4",
"symfony/twig-bundle": "^6.4 || ^7.4"
},
"suggest": {
"nyholm/psr7": "A PSR-17 implementation, needed by the SDK unless your application already provides one",
"setono/consent-bundle": "Handle cookie/GDPR consent, enabling the consent option",
"setono/tag-bag-bundle": "Render the Meta pixel and fbq() calls in the browser, enabling client side tracking",
"symfony/http-client": "A PSR-18 implementation, needed by the SDK unless your application already provides one"
},
"prefer-stable": true,
"autoload": {
"psr-4": {
Expand Down
10 changes: 1 addition & 9 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

namespace Setono\MetaConversionsApiBundle\DependencyInjection;

use Composer\InstalledVersions;
use Composer\Semver\VersionParser;
use Setono\Consent\DefaultConsents;
use Setono\MetaConversionsApiBundle\Cookie\Cookies;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
Expand Down Expand Up @@ -40,7 +38,7 @@ public function getConfigTreeBuilder(): TreeBuilder
->arrayNode('client_side')
->info('Configuration for client side tracking');

if (self::isTagBagBundleInstalled()) {
if (InstalledBundles::hasTagBagBundle()) {
$clientSide->canBeDisabled();
} else {
$clientSide->canBeEnabled();
Expand Down Expand Up @@ -124,10 +122,4 @@ public function getConfigTreeBuilder(): TreeBuilder

return $treeBuilder;
}

private static function isTagBagBundleInstalled(): bool
{
return InstalledVersions::isInstalled('setono/tag-bag-bundle') &&
InstalledVersions::satisfies(new VersionParser(), 'setono/tag-bag-bundle', '^3.0');
}
}
29 changes: 29 additions & 0 deletions src/DependencyInjection/InstalledBundles.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

namespace Setono\MetaConversionsApiBundle\DependencyInjection;

use Composer\InstalledVersions;
use Composer\Semver\VersionParser;

/**
* The optional packages the bundle adapts to, in one place so the configuration and the extension cannot disagree
* about what counts as installed
*/
final class InstalledBundles
{
public const TAG_BAG_BUNDLE = 'setono/tag-bag-bundle';

public const TAG_BAG_BUNDLE_CONSTRAINT = '^3.0';

public static function hasTagBagBundle(): bool
{
return InstalledVersions::isInstalled(self::TAG_BAG_BUNDLE) &&
InstalledVersions::satisfies(new VersionParser(), self::TAG_BAG_BUNDLE, self::TAG_BAG_BUNDLE_CONSTRAINT);
}

private function __construct()
{
}
}
41 changes: 28 additions & 13 deletions src/DependencyInjection/SetonoMetaConversionsApiExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,40 @@

namespace Setono\MetaConversionsApiBundle\DependencyInjection;

use Composer\InstalledVersions;
use Composer\Semver\VersionParser;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Extension\Extension;
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
use Webmozart\Assert\Assert;

final class SetonoMetaConversionsApiExtension extends Extension
{
/**
* Client side tracking renders its tags through the tag bag, so it cannot work without that bundle
*
* @throws \LogicException if the tag bag bundle is not installed and registered
*/
private static function assertTagBagBundleIsAvailable(ContainerBuilder $container): void
{
$requirement = sprintf(
'You need to install %s %s and register SetonoTagBagBundle to use client side tracking, or set setono_meta_conversions_api.client_side.enabled to false',
InstalledBundles::TAG_BAG_BUNDLE,
InstalledBundles::TAG_BAG_BUNDLE_CONSTRAINT,
);

if (!InstalledBundles::hasTagBagBundle()) {
throw new \LogicException($requirement);
}

if (!$container->hasParameter('kernel.bundles')) {
throw new \LogicException('The kernel.bundles parameter has not been set. Are you not using this in a Symfony application context?');
}

$bundles = $container->getParameter('kernel.bundles');
if (!is_array($bundles) || !array_key_exists('SetonoTagBagBundle', $bundles)) {
throw new \LogicException('The SetonoTagBagBundle is not in the list of enabled bundles. ' . $requirement);
}
}

/**
* @param array<array-key, mixed> $config
*/
Expand Down Expand Up @@ -62,16 +86,7 @@ public function load(array $configs, ContainerBuilder $container): void
}

if ($config['client_side']['enabled']) {
$exceptionMessage = 'You need to install the setono/tag-bag-bundle ^3.0 to use the client side tracking';

Assert::true($container->hasParameter('kernel.bundles'), 'The kernel.bundles parameter has not been set. Are you not using this in a Symfony application context?');

$bundles = $container->getParameter('kernel.bundles');
Assert::isArray($bundles);
Assert::keyExists($bundles, 'SetonoTagBagBundle', 'The SetonoTagBagBundle is not in the list of enabled bundles. ' . $exceptionMessage);

Assert::true(InstalledVersions::isInstalled('setono/tag-bag-bundle'), $exceptionMessage);
Assert::true(InstalledVersions::satisfies(new VersionParser(), 'setono/tag-bag-bundle', '^3.0'), $exceptionMessage);
self::assertTagBagBundleIsAvailable($container);

$loader->load('services/conditional/client_side.xml');
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Integration/SetonoMetaConversionsApiBundleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ protected static function createKernel(array $options = []): KernelInterface
#[Test]
public function it_throws_exception_if_client_side_is_enabled_but_tag_bag_is_not_enabled(): void
{
$this->expectException(\InvalidArgumentException::class);
$this->expectException(\LogicException::class);
self::bootKernel();
}

Expand Down
Loading