Skip to content

Commit

Permalink
- added facade
Browse files Browse the repository at this point in the history
- added more tests
- code rework
- added config
- added more helper-functions
  • Loading branch information
toni-suarez committed Jul 6, 2024
1 parent a834b4c commit 1b82c60
Show file tree
Hide file tree
Showing 8 changed files with 99 additions and 62 deletions.
5 changes: 4 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,10 @@
"laravel": {
"providers": [
"Suarez\\StatamicUtmParameters\\ServiceProvider"
]
],
"aliases": {
"UtmParameter": "Suarez\\UtmParameter\\Facades\\UtmParameter"
}
}
},
"scripts": {
Expand Down
26 changes: 26 additions & 0 deletions src/Facades/UtmParameter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace Suarez\StatamicUtmParameters\Facades;

use Illuminate\Support\Facades\Facade;

/**
* @method static \Suarez\StatamicUtmParameters\UtmParameter boot(\Illuminate\Http\Request $request)
* @method static array useRequestOrSession(\Illuminate\Http\Request $request)
* @method static array all()
* @method static string|null get(string $key)
* @method static bool has(string $key, $value = null)
* @method static bool contains(string $key, string $value)
* @method static bool clear()
* @method static array getParameter(\Illuminate\Http\Request $request)
* @method static string ensureUtmPrefix(string $key)
*
* @see \Suarez\StatamicUtmParameters\UtmParameter
*/
class UtmParameter extends Facade
{
protected static function getFacadeAccessor()
{
return \Suarez\StatamicUtmParameters\UtmParameter::class;
}
}
4 changes: 2 additions & 2 deletions src/Http/Middleware/CheckUtmParameter.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

use Closure;
use Illuminate\Http\Request;
use Suarez\StatamicUtmParameters\UtmParameter;
use Suarez\StatamicUtmParameters\Facades\UtmParameter;

class CheckUtmParameter
{
Expand All @@ -18,7 +18,7 @@ class CheckUtmParameter
public function handle(Request $request, Closure $next)
{
if ($this->shouldAcceptUtmParameter($request)) {
app(UtmParameter::class)->boot($request);
UtmParameter::boot($request);
}

return $next($request);
Expand Down
6 changes: 3 additions & 3 deletions src/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Suarez\StatamicUtmParameters;

use Illuminate\Foundation\AliasLoader;
use Statamic\Providers\AddonServiceProvider;
use Suarez\StatamicUtmParameters\UtmParameter;
use Suarez\StatamicUtmParameters\Http\Middleware\CheckUtmParameter;
Expand All @@ -20,8 +21,7 @@ class ServiceProvider extends AddonServiceProvider

public function bootAddon()
{
$this->app->singleton(UtmParameter::class, function () {
return new UtmParameter();
});
$this->app->singleton(UtmParameter::class, fn () => new UtmParameter());
AliasLoader::getInstance()->alias('UtmParameter', \Suarez\StatamicUtmParameters\Facades\UtmParameter::class);
}
}
4 changes: 2 additions & 2 deletions src/Tags/Utm.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
namespace Suarez\StatamicUtmParameters\Tags;

use Statamic\Tags\Tags;
use Suarez\StatamicUtmParameters\UtmParameter;
use Suarez\StatamicUtmParameters\Facades\UtmParameter;

class Utm extends Tags
{
/**
* Default index.
*
* @return array
* @return string
*/
public function index()
{
Expand Down
74 changes: 35 additions & 39 deletions src/UtmParameter.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class UtmParameter
*
* @var array
*/
public $parameters;
public array|null $parameters;

/**
* Utm Parameter Session Key.
Expand All @@ -20,6 +20,7 @@ class UtmParameter
*/
public string $sessionKey;


public function __construct(array $parameters = [])
{
$this->sessionKey = config('statamic-utm-parameter.session_key');
Expand All @@ -33,7 +34,7 @@ public function __construct(array $parameters = [])
*
* @return UtmParameter
*/
public function boot(Request $request)
public function boot(Request $request) : self
{
$this->parameters = $this->useRequestOrSession($request);
return $this;
Expand All @@ -45,9 +46,9 @@ public function boot(Request $request)
* @param \Illuminate\Http\Request $request
* @return array
*/
public function useRequestOrSession(Request $request)
public function useRequestOrSession(Request $request) : array
{
$currentRequestParameter = self::getParameter($request);
$currentRequestParameter = $this->getParameter($request);
$sessionParameter = session($this->sessionKey);

if (!empty($currentRequestParameter) && empty($sessionParameter)) {
Expand All @@ -61,17 +62,17 @@ public function useRequestOrSession(Request $request)
return $mergedParameters;
}

return $sessionParameter;
return $sessionParameter ?? [];
}

/**
* Retrieve all UTM-Parameter.
* Retrieve all statamic-utm-parameter.
*
* @return array
*/
public static function all() : array
public function all(): array
{
return app(UtmParameter::class)->parameters ?? [];
return session($this->sessionKey) ?? [];
}

/**
Expand All @@ -81,71 +82,66 @@ public static function all() : array
*
* @return string|null
*/
public static function get(string $key)
public function get(string $key): ?string
{
$parameters = self::all();
$key = self::ensureUtmPrefix($key);
$parameters = $this->all();
$key = $this->ensureUtmPrefix($key);

if (!array_key_exists($key, $parameters)) {
return null;
}

return $parameters[$key];
return $parameters[$key] ?? null;
}


/**
* Determine if a value contains inside the key.
* Determine if a UTM-Parameter exists.
*
* @param string $key
* @param string $value
* @param string|null $value
*
* @return bool
*/
public static function contains(string $key, string $value)
public function has(string $key, ?string $value = null): bool
{
$parameters = self::all();
$key = self::ensureUtmPrefix($key);
$parameters = $this->all();
$key = $this->ensureUtmPrefix($key);

if (!array_key_exists($key, $parameters) || !is_string($value)) {
if (!array_key_exists($key, $parameters)) {
return false;
}

return str_contains(self::get($key), $value);
return $value === null || $this->get($key) === $value;
}

/**
* Determine if a UTM-Parameter exists.
* Determine if a value contains inside the key.
*
* @param string $key
* @param string $value
*
* @return bool
*/
public static function has(string $key, $value = null)
public function contains(string $key, ?string $value): bool
{
$parameters = self::all();
$key = self::ensureUtmPrefix($key);

if (!array_key_exists($key, $parameters)) {
if ($value === null) {
return false;
}

if (array_key_exists($key, $parameters) && $value !== null) {
return self::get($key) === $value;
$parameters = $this->all();
$key = $this->ensureUtmPrefix($key);

if (!array_key_exists($key, $parameters) || !is_string($value)) {
return false;
}

return true;
return str_contains($this->get($key), $value);
}

/**
* Clear and remove utm session.
*
* @return bool
*/
public static function clear()
public function clear(): bool
{
session()->forget(app(UtmParameter::class)->sessionKey);
app(UtmParameter::class)->parameters = null;
session()->forget($this->sessionKey);
$this->parameters = null;
return true;
}

Expand All @@ -154,7 +150,7 @@ public static function clear()
*
* @return array
*/
protected static function getParameter(Request $request) : array
protected function getParameter(Request $request): array
{
$allowedKeys = config('statamic-utm-parameter.allowed_utm_parameters', [
'utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content'
Expand All @@ -175,7 +171,7 @@ protected static function getParameter(Request $request) : array
* @param string $key
* @return string
*/
protected static function ensureUtmPrefix(string $key): string
protected function ensureUtmPrefix(string $key): string
{
return str_starts_with($key, 'utm_') ? $key : 'utm_' . $key;
}
Expand Down
18 changes: 17 additions & 1 deletion src/helper.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

use Suarez\StatamicUtmParameters\UtmParameter;
use Suarez\StatamicUtmParameters\Facades\UtmParameter;

if (!function_exists('get_all_utm')) {
function get_all_utm()
Expand Down Expand Up @@ -29,3 +29,19 @@ function has_not_utm($key, $value = null)
return !UtmParameter::has($key, $value);
}
}



if (!function_exists('contains_utm')) {
function contains_utm($key, $value)
{
return UtmParameter::contains($key, $value);
}
}

if (!function_exists('contains_not_utm')) {
function contains_not_utm($key, $value)
{
return !UtmParameter::contains($key, $value);
}
}
24 changes: 10 additions & 14 deletions tests/UtmParametersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@

use Illuminate\Http\Request;
use Statamic\Facades\Config;
use Suarez\StatamicUtmParameters\UtmParameter;
use Suarez\StatamicUtmParameters\Tests\TestCase;
use Suarez\StatamicUtmParameters\Facades\UtmParameter;
use Suarez\StatamicUtmParameters\UtmParameter as UtmParameterClass;

class UtmParametersTest extends TestCase
{
Expand All @@ -28,18 +29,13 @@ public function setUp(): void

$request = Request::create('/test', 'GET', $parameters);

app()->singleton(UtmParameter::class, fn () => new UtmParameter());
app(UtmParameter::class)->boot($request);
session([$this->sessionKey => $parameters]);
UtmParameter::boot($request);

}

public function tearDown() : void
{
session()->forget($this->sessionKey);

Config::set('statamic-utm-parameter.override_utm_parameters', null);
Config::set('statamic-utm-parameter.session_key', null);

parent::tearDown();
}

Expand Down Expand Up @@ -233,7 +229,7 @@ public function test_it_should_overwrite_new_utm_parameter()
];

$request = Request::create('/test', 'GET', $parameters);
app(UtmParameter::class)->boot($request);
UtmParameter::boot($request);

$source = UtmParameter::get('source');
$this->assertEquals('newsletter', $source);
Expand All @@ -258,7 +254,7 @@ public function test_it_should_keep_existing_parameters()
];

$request = Request::create('/test', 'GET', $parameters);
app(UtmParameter::class)->boot($request);
UtmParameter::boot($request);

$source = UtmParameter::get('source');
$this->assertEquals('google', $source);
Expand All @@ -277,14 +273,14 @@ public function test_it_should_keep_existing_parameters_while_browsing()

$parameters = ['id' => '0123456789', 'sorting' => 'relevance'];
$request = Request::create('/new-page', 'GET', $parameters);
app(UtmParameter::class)->boot($request);
UtmParameter::boot($request);

$source = UtmParameter::get('source');
$this->assertEquals('google', $source);

$parameters = [];
$request = Request::create('/second-page', 'GET', $parameters);
app(UtmParameter::class)->boot($request);
UtmParameter::boot($request);

$source = UtmParameter::get('source');
$this->assertEquals('google', $source);
Expand All @@ -303,7 +299,7 @@ public function test_it_should_only_use_utm_parameters_in_the_allowed_list()
];

$request = Request::create('/test', 'GET', $parameters);
app(UtmParameter::class)->boot($request);
UtmParameter::boot($request);

$source = UtmParameter::get('source');
$this->assertEquals('newsletter', $source);
Expand Down Expand Up @@ -332,7 +328,7 @@ public function test_it_should_sanitize_utm_parameter()
];

$request = Request::create('/test', 'GET', $parameters);
app(UtmParameter::class)->boot($request);
UtmParameter::boot($request);

$source = UtmParameter::get('source');
$this->assertEquals('&lt;span onclick=&quot;alert(&#039;alert&#039;)&quot;&gt;google&lt;/span&gt;', $source);
Expand Down

0 comments on commit 1b82c60

Please sign in to comment.