From b3037cc738eb0b244609e5fb772a4860f60e224a Mon Sep 17 00:00:00 2001 From: toni-suarez Date: Wed, 19 Jun 2024 07:35:57 +0200 Subject: [PATCH] - code improvements - added clear method - updated tests - updated readme --- .github/FUNDING.yml | 1 + README.md | 1 + src/Middleware/UtmParameters.php | 5 +- src/Providers/UtmParameterServiceProvider.php | 4 +- src/UtmParameter.php | 62 ++++++++--- tests/UtmParameterTest.php | 104 ++++++++++++------ 6 files changed, 120 insertions(+), 57 deletions(-) create mode 100644 .github/FUNDING.yml diff --git a/.github/FUNDING.yml b/.github/FUNDING.yml new file mode 100644 index 0000000..d8e6577 --- /dev/null +++ b/.github/FUNDING.yml @@ -0,0 +1 @@ +github: [toni-suarez] diff --git a/README.md b/README.md index 2ea0c3f..46ce1b2 100644 --- a/README.md +++ b/README.md @@ -4,6 +4,7 @@ [![Test PHP 8.x](https://github.com/toni-suarez/laravel-utm-parameter/actions/workflows/tests-php8.yml/badge.svg?branch=main)](https://github.com/toni-suarez/laravel-utm-parameter/actions/workflows/tests-php8.yml) [![Packagist Downloads](https://img.shields.io/packagist/dt/suarez/laravel-utm-parameter)](https://packagist.org/packages/suarez/laravel-utm-parameter) ![GitHub](https://img.shields.io/github/license/toni-suarez/laravel-utm-parameter) +[![Statamic Addon](https://img.shields.io/badge/https%3A%2F%2Fstatamic.com%2Faddons%2Ftoni-suarez%2Futm-parameter?style=flat-square&logo=statamic&logoColor=rgb(255%2C%2038%2C%20158)&label=Statamic&link=https%3A%2F%2Fstatamic.com%2Faddons%2Ftoni-suarez%2Futm-parameter)](https://statamic.com/addons/toni-suarez/utm-parameter) A lightweight way to handle UTM parameters session-based in your Laravel Application. diff --git a/src/Middleware/UtmParameters.php b/src/Middleware/UtmParameters.php index c4da511..8b3dd60 100644 --- a/src/Middleware/UtmParameters.php +++ b/src/Middleware/UtmParameters.php @@ -14,7 +14,7 @@ class UtmParameters * @param \Illuminate\Http\Request $request * @param \Closure $next * - * @return mixed + * @return \Closure */ public function handle(Request $request, Closure $next) { @@ -29,9 +29,8 @@ public function handle(Request $request, Closure $next) * Determines whether the given request/response pair should accept UTM-Parameters. * * @param \Illuminate\Http\Request $request - * @param \Illuminate\Http\Response $response * - * @return \Illuminate\Http\Request + * @return bool */ protected function shouldAcceptUtmParameter(Request $request) { diff --git a/src/Providers/UtmParameterServiceProvider.php b/src/Providers/UtmParameterServiceProvider.php index dc72e12..0063d52 100644 --- a/src/Providers/UtmParameterServiceProvider.php +++ b/src/Providers/UtmParameterServiceProvider.php @@ -15,9 +15,7 @@ class UtmParameterServiceProvider extends ServiceProvider */ public function register() { - $this->app->singleton(UtmParameter::class, function () { - return new UtmParameter(); - }); + $this->app->singleton(UtmParameter::class, fn () => new UtmParameter()); } /** diff --git a/src/UtmParameter.php b/src/UtmParameter.php index 7c21669..75abced 100644 --- a/src/UtmParameter.php +++ b/src/UtmParameter.php @@ -11,11 +11,9 @@ class UtmParameter */ public $parameters; - public function __construct($parameters = []) + public function __construct(array $parameters = []) { - $this->parameters = is_string($parameters) - ? json_decode($parameters, true) - : $parameters; + $this->parameters = $parameters; } /** @@ -32,9 +30,7 @@ public function boot($parameters = null) session(['utm' => $parameters]); } - $this->parameters = is_string($parameters) - ? json_decode($parameters, true) - : $parameters; + $this->parameters = $parameters; return app(UtmParameter::class, $parameters); } @@ -59,10 +55,7 @@ public static function all() public static function get($key) { $parameters = self::all(); - - if (strpos($key, 'utm_') === false) { - $key = 'utm_'.$key; - } + $key = self::ensureUtmPrefix($key); if (!array_key_exists($key, $parameters)) { return null; @@ -82,10 +75,7 @@ public static function get($key) public static function has($key, $value = null) { $parameters = self::all(); - - if (strpos($key, 'utm_') === false) { - $key = 'utm_'.$key; - } + $key = self::ensureUtmPrefix($key); if (!array_key_exists($key, $parameters)) { return false; @@ -98,6 +88,37 @@ public static function has($key, $value = null) return true; } + /** + * Determine if a value contains inside the key. + * + * @param string $key + * @param string $value + * @return bool + */ + public static function contains($key, $value) + { + $parameters = self::all(); + $key = self::ensureUtmPrefix($key); + + if (!array_key_exists($key, $parameters) || !is_string($value)) { + return false; + } + + return str_contains(self::get($key), $value); + } + + /** + * Clear and remove utm session. + * + * @return bool + */ + public static function clear() + { + app(UtmParameter::class)->parameters = null; + session()->forget('utm'); + return true; + } + /** * Retrieve all UTM-Parameter from the URI. * @@ -110,4 +131,15 @@ protected static function getParameter() ->map(fn ($value) => htmlspecialchars($value, ENT_QUOTES, 'UTF-8')) ->toArray(); } + + /** + * Ensure the key to start with 'utm_'. + * + * @param string $key + * @return string + */ + protected static function ensureUtmPrefix(string $key): string + { + return str_starts_with($key, 'utm_') ? $key : 'utm_' . $key; + } } diff --git a/tests/UtmParameterTest.php b/tests/UtmParameterTest.php index ffb33ac..6e4bddc 100644 --- a/tests/UtmParameterTest.php +++ b/tests/UtmParameterTest.php @@ -2,7 +2,7 @@ namespace Suarez\UtmParameter\Tests; -use PHPUnit\Framework\TestCase; +use Orchestra\Testbench\TestCase; use Suarez\UtmParameter\UtmParameter; class UtmParameterTest extends TestCase @@ -19,13 +19,17 @@ public function setUp(): void 'utm_term' => '{targetid}', ]; - app()->bind(UtmParameter::class, function () use ($parameters) { - return new UtmParameter($parameters); - }); + app()->singleton(UtmParameter::class, fn () => new UtmParameter()); + app(UtmParameter::class)->boot($parameters); } - /** @test */ - public function it_should_have_an_utm_attribute_bag() + public function test_it_should_be_bound_in_the_app() + { + $utm = app(UtmParameter::class); + $this->assertInstanceOf(UtmParameter::class, $utm); + } + + public function test_it_should_have_an_utm_attribute_bag() { $utm = UtmParameter::all(); $this->assertIsArray($utm); @@ -33,123 +37,151 @@ public function it_should_have_an_utm_attribute_bag() $this->assertArrayHasKey('utm_source', $utm); } - /** @test */ - public function it_should_have_a_source_parameter() + public function test_it_should_have_a_source_parameter() { $source = UtmParameter::get('source'); $this->assertNotEmpty($source); $this->assertEquals('google', $source); } - /** @test */ - public function it_should_have_work_with_utm_inside_key() + public function test_it_should_have_work_with_utm_inside_key() { $source = UtmParameter::get('utm_source'); $this->assertNotEmpty($source); $this->assertEquals('google', $source); } - /** @test */ - public function it_should_have_a_medium_parameter() + public function test_it_should_have_a_medium_parameter() { $medium = UtmParameter::get('medium'); $this->assertNotEmpty($medium); $this->assertEquals('cpc', $medium); } - /** @test */ - public function it_should_have_a_campaign_parameter() + public function test_it_should_have_a_campaign_parameter() { $campaign = UtmParameter::get('campaign'); $this->assertNotEmpty($campaign); $this->assertEquals('{campaignid}', $campaign); } - /** @test */ - public function it_should_have_a_content_parameter() + public function test_it_should_have_a_content_parameter() { $content = UtmParameter::get('content'); $this->assertNotEmpty($content); $this->assertEquals('{adgroupid}', $content); } - /** @test */ - public function it_should_have_a_term_parameter() + public function test_it_should_have_a_term_parameter() { $term = UtmParameter::get('term'); $this->assertNotEmpty($term); $this->assertEquals('{targetid}', $term); } - /** @test */ - public function it_should_get_a_utm_parameter_via_helper() + public function test_it_should_get_a_utm_parameter_via_helper() { $source = get_utm('source'); $this->assertNotEmpty($source); $this->assertEquals('google', $source); } - /** @test */ - public function it_should_determine_if_utm_has_key() + public function test_it_should_determine_if_utm_has_key() { $hasSource = UtmParameter::has('source'); $this->assertIsBool($hasSource); $this->assertTrue($hasSource); } - /** @test */ - public function it_should_determine_if_utm_has_not_key() + public function test_it_should_determine_if_utm_has_not_key() { $hasRandomKey = UtmParameter::has('random-key'); $this->assertIsBool($hasRandomKey); $this->assertFalse($hasRandomKey); } - /** @test */ - public function it_should_determine_if_utm_has_key_and_value() + public function test_it_should_determine_if_utm_has_key_and_value() { $hasGoogleSource = UtmParameter::has('utm_source', 'google'); $this->assertIsBool($hasGoogleSource); $this->assertTrue($hasGoogleSource); } - /** @test */ - public function it_should_determine_if_utm_has_not_key_and_value() + public function test_it_should_determine_if_utm_has_not_key_and_value() { $hasRandomSource = UtmParameter::has('random-source', 'random-value'); $this->assertIsBool($hasRandomSource); $this->assertFalse($hasRandomSource); } - /** @test */ - public function it_should_determine_if_a_key_exists_for_utm_parameters() + public function test_it_should_determine_if_a_key_exists_for_utm_parameters() { $hasSource = has_utm('source'); $this->assertIsBool($hasSource); $this->assertTrue($hasSource); } - /** @test */ - public function it_should_determine_if_a_utm_parameter_equals_a_value() + public function test_it_should_determine_if_a_utm_parameter_equals_a_value() { $isGoogle = has_utm('source', 'google'); $this->assertIsBool($isGoogle); $this->assertTrue($isGoogle); } - /** @test */ - public function it_should_determine_if_a_key_does_not_exists_for_utm_parameters() + public function test_it_should_determine_if_a_key_does_not_exists_for_utm_parameters() { $hasRandomKey = has_not_utm('random-key'); $this->assertIsBool($hasRandomKey); $this->assertTrue($hasRandomKey); } - /** @test */ - public function it_should_determine_if_a_utm_parameter_not_equals_a_value() + public function test_it_should_determine_if_a_utm_parameter_not_equals_a_value() { $isRandomSource = has_not_utm('source', 'random'); $this->assertIsBool($isRandomSource); $this->assertTrue($isRandomSource); } + + public function test_it_should_determine_if_an_utm_contains_a_value() + { + $campaign = UtmParameter::contains('utm_campaign', 'campaign'); + $this->assertIsBool($campaign); + $this->assertTrue($campaign); + } + + public function test_it_should_determine_if_an_utm_contains_not_a_value() + { + $hasRandomCampaign = UtmParameter::contains('utm_campaign', 'some-thing'); + $this->assertIsBool($hasRandomCampaign); + $this->assertFalse($hasRandomCampaign); + } + + public function test_it_should_determine_if_an_utm_contains_a_non_string_value() + { + $campaign = UtmParameter::contains('utm_campaign', null); + $this->assertIsBool($campaign); + $this->assertFalse($campaign); + + $term = UtmParameter::contains('utm_term', false); + $this->assertIsBool($term); + $this->assertFalse($term); + + $content = UtmParameter::contains('utm_content', []); + $this->assertIsBool($content); + $this->assertFalse($content); + + $medium = UtmParameter::contains('utm_medium', 1); + $this->assertIsBool($medium); + $this->assertFalse($medium); + } + + public function test_it_should_clear_and_remove_the_utm_parameter_again() + { + $source = UtmParameter::get('source'); + $this->assertEquals('google', $source); + + UtmParameter::clear(); + $emptySource = UtmParameter::get('source'); + $this->assertNull($emptySource); + } }