Skip to content

Commit

Permalink
- added facade
Browse files Browse the repository at this point in the history
  • Loading branch information
toni-suarez committed Jun 21, 2024
1 parent 1db3a61 commit 238f3e3
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 32 deletions.
5 changes: 4 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,10 @@
"laravel": {
"providers": [
"Suarez\\UtmParameter\\Providers\\UtmParameterServiceProvider"
]
],
"aliases": {
"UtmParameter": "Suarez\\UtmParameter\\Facades\\UtmParameter"
}
}
},
"minimum-stability": "stable",
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\UtmParameter\Facades;

use Illuminate\Support\Facades\Facade;

/**
* @method static \Suarez\UtmParameter\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\UtmParameter\UtmParameter
*/
class UtmParameter extends Facade
{
protected static function getFacadeAccessor()
{
return \Suarez\UtmParameter\UtmParameter::class;
}
}
4 changes: 2 additions & 2 deletions src/Middleware/UtmParameters.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

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

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

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

use Illuminate\Support\Facades\Blade;
use Suarez\UtmParameter\UtmParameter;
use Illuminate\Foundation\AliasLoader;
use Illuminate\Support\ServiceProvider;

class UtmParameterServiceProvider extends ServiceProvider
Expand Down Expand Up @@ -43,5 +44,7 @@ public function boot()
Blade::if('containsNotUtm', function (string $key, string|null $value = null) {
return contains_not_utm($key, $value);
});

AliasLoader::getInstance()->alias('UtmParameter', \Suarez\UtmParameter\Facades\UtmParameter::class);
}
}
36 changes: 18 additions & 18 deletions src/UtmParameter.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function boot(Request $request)
*/
public function useRequestOrSession(Request $request)
{
$currentRequestParameter = self::getParameter($request);
$currentRequestParameter = $this->getParameter($request);
$sessionParameter = session($this->sessionKey);

if (!empty($currentRequestParameter) && empty($sessionParameter)) {
Expand All @@ -70,9 +70,9 @@ public function useRequestOrSession(Request $request)
*
* @return array
*/
public static function all()
public function all()
{
return app(UtmParameter::class)->parameters ?? [];
return $this->parameters ?? [];
}

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

if (!array_key_exists($key, $parameters)) {
return null;
Expand All @@ -102,17 +102,17 @@ public static function get(string $key)
*
* @return bool
*/
public static function has(string $key, $value = null)
public function has(string $key, $value = null)
{
$parameters = self::all();
$key = self::ensureUtmPrefix($key);
$parameters = $this->all();
$key = $this->ensureUtmPrefix($key);

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

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

return true;
Expand All @@ -125,27 +125,27 @@ public static function has(string $key, $value = null)
* @param string $value
* @return bool
*/
public static function contains(string $key, string $value)
public function contains(string $key, string $value)
{
$parameters = self::all();
$key = self::ensureUtmPrefix($key);
$parameters = $this->all();
$key = $this->ensureUtmPrefix($key);

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

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

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

Expand Down Expand Up @@ -175,7 +175,7 @@ protected static function getParameter(Request $request)
* @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
2 changes: 1 addition & 1 deletion src/helper.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

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

if (!function_exists('get_all_utm')) {
function get_all_utm()
Expand Down
18 changes: 8 additions & 10 deletions tests/UtmParameterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use Illuminate\Http\Request;
use Orchestra\Testbench\TestCase;
use Suarez\UtmParameter\UtmParameter;
use Suarez\UtmParameter\Facades\UtmParameter;
use Illuminate\Support\Facades\Config;

class UtmParameterTest extends TestCase
Expand All @@ -27,9 +27,7 @@ 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
Expand Down Expand Up @@ -273,7 +271,7 @@ public function test_it_should_overwrite_new_utm_parameter()
];

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

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

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

$id = UtmParameter::get('id');
$this->assertEmpty($id);
Expand All @@ -325,7 +323,7 @@ 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);

$id = UtmParameter::get('id');
$this->assertEmpty($id);
Expand All @@ -340,7 +338,7 @@ public function test_it_should_keep_existing_parameters_while_browsing()

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

$id = UtmParameter::get('id');
$this->assertEmpty($id);
Expand All @@ -367,7 +365,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 @@ -409,7 +407,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 238f3e3

Please sign in to comment.