Move dynamic method to base presenter and adjust for other model types

This commit is contained in:
Marcus Moore
2026-04-01 15:05:42 -07:00
parent e2b8368f40
commit 2acb38a6a5
7 changed files with 143 additions and 16 deletions
-16
View File
@@ -3,7 +3,6 @@
namespace App\Presenters;
use App\Models\CustomField;
use App\Models\Setting;
use Carbon\CarbonImmutable;
use DateTime;
use Illuminate\Support\Facades\Storage;
@@ -673,21 +672,6 @@ class AssetPresenter extends Presenter
return false;
}
/**
* Used to take user created URL and dynamically fill in the needed values per asset
*
* @return string
*/
public function dynamicUrl($dynamic_url)
{
$url = (str_replace('{LOCALE}', Setting::getSettings()->locale, $dynamic_url));
$url = (str_replace('{SERIAL}', urlencode($this->model->serial), $url));
$url = (str_replace('{MODEL_NAME}', urlencode($this->model->model->name), $url));
$url = (str_replace('{MODEL_NUMBER}', urlencode($this->model->model->model_number), $url));
return $url;
}
/**
* Url to view this item.
*
+27
View File
@@ -2,6 +2,8 @@
namespace App\Presenters;
use App\Models\Asset;
use App\Models\Setting;
use App\Models\SnipeModel;
abstract class Presenter
@@ -98,6 +100,31 @@ abstract class Presenter
return '';
}
/**
* Used to take user created URL and dynamically fill in the needed values per item
*
* @return string
*/
public function dynamicUrl($dynamic_url)
{
$url = (str_replace('{LOCALE}', Setting::getSettings()->locale, $dynamic_url));
if ($this->model instanceof Asset) {
$url = (str_replace('{SERIAL}', urlencode($this->model->serial), $url));
$url = (str_replace('{MODEL_NAME}', urlencode($this->model->model->name), $url));
$url = (str_replace('{MODEL_NUMBER}', urlencode($this->model->model->model_number), $url));
return $url;
}
$url = (str_replace('{SERIAL}', urlencode($this->serial), $url));
$url = (str_replace('{MODEL_NAME}', urlencode($this->model_name), $url));
$url = (str_replace('{MODEL_NUMBER}', urlencode($this->model_number), $url));
return $url;
}
public function __get($property)
{
if (method_exists($this, $property)) {
@@ -0,0 +1,21 @@
<?php
namespace Tests\Unit\Presenters;
use App\Models\Accessory;
use Tests\TestCase;
class AccessoryPresenterTest extends TestCase
{
public function test_dynamic_url()
{
$this->settings->set(['locale' => 'en-US']);
$accessory = Accessory::factory()->create(['model_number' => 'MN-123']);
$this->assertEquals(
'https://example.com/en-US/MN-123',
$accessory->present()->dynamicUrl('https://example.com/{LOCALE}/{MODEL_NUMBER}')
);
}
}
@@ -0,0 +1,29 @@
<?php
namespace Tests\Unit\Presenters;
use App\Models\Asset;
use App\Models\AssetModel;
use Tests\TestCase;
class AssetPresenterTest extends TestCase
{
public function test_dynamic_url()
{
$this->settings->set(['locale' => 'en-US']);
$assetModel = AssetModel::factory()->create([
'model_number' => 'MN-123',
'name' => 'Macbook',
]);
$asset = Asset::factory()
->for($assetModel, 'model')
->create(['serial' => 'SN-123']);
$this->assertEquals(
'https://example.com/en-US/SN-123/MN-123/Macbook',
$asset->present()->dynamicUrl('https://example.com/{LOCALE}/{SERIAL}/{MODEL_NUMBER}/{MODEL_NAME}')
);
}
}
@@ -0,0 +1,24 @@
<?php
namespace Tests\Unit\Presenters;
use App\Models\Component;
use Tests\TestCase;
class ComponentPresenterTest extends TestCase
{
public function test_dynamic_url()
{
$this->settings->set(['locale' => 'en-US']);
$component = Component::factory()->create([
'serial' => 'SN-123',
'model_number' => 'MN-123',
]);
$this->assertEquals(
'https://example.com/en-US/SN-123/MN-123',
$component->present()->dynamicUrl('https://example.com/{LOCALE}/{SERIAL}/{MODEL_NUMBER}')
);
}
}
@@ -0,0 +1,21 @@
<?php
namespace Tests\Unit\Presenters;
use App\Models\Consumable;
use Tests\TestCase;
class ConsumablePresenterTest extends TestCase
{
public function test_dynamic_url()
{
$this->settings->set(['locale' => 'en-US']);
$consumable = Consumable::factory()->create(['model_number' => 'MN-123']);
$this->assertEquals(
'https://example.com/en-US/MN-123',
$consumable->present()->dynamicUrl('https://example.com/{LOCALE}/{MODEL_NUMBER}')
);
}
}
@@ -0,0 +1,21 @@
<?php
namespace Tests\Unit\Presenters;
use App\Models\License;
use Tests\TestCase;
class LicensePresenterTest extends TestCase
{
public function test_dynamic_url()
{
$this->settings->set(['locale' => 'en-US']);
$license = License::factory()->create();
$this->assertEquals(
'https://example.com/en-US',
$license->present()->dynamicUrl('https://example.com/{LOCALE}')
);
}
}