Skip to content
This repository has been archived by the owner on Oct 21, 2022. It is now read-only.

Commit

Permalink
ability to override method behavior without overriding the model clas…
Browse files Browse the repository at this point in the history
…s itself
  • Loading branch information
michael-rubel committed Feb 12, 2022
1 parent 63b2474 commit b416f88
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 17 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
"require": {
"php": "^8.0|^8.1",
"laravel/framework": "^8.71|^9.0",
"spatie/laravel-package-tools": "^1.10"
"spatie/laravel-package-tools": "^1.10",
"michael-rubel/laravel-enhanced-container": "^6.0|^7.0"
},
"require-dev": {
"nunomaduro/collision": "^5.10|^6.0",
Expand Down
2 changes: 2 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ parameters:
- '#Method MichaelRubel\\SeoManager\\Composers\\SeoComposer\:\:getInstance\(\) is unused\.#'
- '#Cannot call method toArray\(\) on mixed\.#'
- '#Method MichaelRubel\\SeoManager\\Composers\\SeoComposer\:\:getMaxWildcardLevels\(\) is unused\.#'
- '#Parameter \#1 \$class of function call expects object\|string, mixed given\.#'
- '#Cannot call method (.*) on mixed\.#'

level: max

Expand Down
25 changes: 9 additions & 16 deletions src/Composers/SeoComposer.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Illuminate\Support\Str;
use Illuminate\View\View;
use MichaelRubel\EnhancedContainer\Call;
use MichaelRubel\SeoManager\Contracts\SeoTagContract;
use MichaelRubel\SeoManager\Exceptions\ShouldImplementSeoTagInterfaceException;
use MichaelRubel\SeoManager\Models\SeoTag;
Expand Down Expand Up @@ -48,27 +49,23 @@ public function compose(View $view): void
*/
protected function getSeoTags(): mixed
{
$configuredModel = config('seo-manager.model');

$model = app(
is_string($configuredModel) && class_exists($configuredModel)
? $configuredModel
: SeoTag::class
$model = call(
config('seo-manager.model', SeoTag::class)
);

if (! $model instanceof SeoTagContract) {
if (! $model->getInternal(Call::INSTANCE) instanceof SeoTagContract) {
throw new ShouldImplementSeoTagInterfaceException();
}

$nonPrefixedUrl = request()->path();
$url = Str::start($nonPrefixedUrl, '/');

$instance = $model::where($model->getUrlColumnName(), $url)
$instance = $model->where($model->getUrlColumnName(), $url)
->orWhere($model->getUrlColumnName(), $nonPrefixedUrl)
->first();

if (is_null($instance)) {
$instance = $model::whereIn($model->getUrlColumnName(), $this->wildcard($url))
$instance = $model->whereIn($model->getUrlColumnName(), $this->wildcard($url))
->limit($this->getMaxWildcardLevels())
->get()
->sortByDesc(
Expand Down Expand Up @@ -107,14 +104,10 @@ protected function wildcard(string $url): array
/**
* Gets the levels to limit the query.
*
* @return int
* @return mixed
*/
private function getMaxWildcardLevels(): int
private function getMaxWildcardLevels(): mixed
{
$levels = config('seo-manager.max_wildcard_levels');

return is_int($levels)
? $levels
: 3;
return config('seo-manager.max_wildcard_levels', 3);
}
}
3 changes: 3 additions & 0 deletions src/SeoManagerServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace MichaelRubel\SeoManager;

use MichaelRubel\EnhancedContainer\LecServiceProvider;
use MichaelRubel\SeoManager\Composers\SeoComposer;
use Spatie\LaravelPackageTools\Package;
use Spatie\LaravelPackageTools\PackageServiceProvider;
Expand Down Expand Up @@ -33,6 +34,8 @@ public function configurePackage(Package $package): void
*/
public function packageRegistered(): void
{
$this->app->register(LecServiceProvider::class);

$this->app->scoped(SeoComposer::class);
}
}
53 changes: 53 additions & 0 deletions tests/BindabilityTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

namespace MichaelRubel\SeoManager\Tests;

use Illuminate\View\ViewException;
use MichaelRubel\SeoManager\Models\SeoTag;
use MichaelRubel\SeoManager\Tests\Stubs\TestServiceProvider;

class BindabilityTest extends TestCase
{
/**
* @return void
*/
public function setUp(): void
{
parent::setUp();

SeoTag::create([
'url' => '/',
'tags' => json_decode('{"title":"TestTitle"}'),
]);

app()->register(TestServiceProvider::class);
}

/** @test */
public function testCanBindUrlColumnNameMethod()
{
$this->expectException(ViewException::class);

bind(SeoTag::class)->method('getUrlColumnName', function ($model) {
return $model->getUrlColumnName() . '_applied_wrong_column_name';
});

$view = $this->view('test-seo-manager::test-view');

$view->assertSee('TestTitle');
}

/** @test */
public function testCanBindTagsColumnNameMethod()
{
$this->expectException(ViewException::class);

bind(SeoTag::class)->method('getTagsColumnName', function ($model) {
return $model->getTagsColumnName() . '_applied_wrong_column_name';
});

$view = $this->view('test-seo-manager::test-view');

$view->assertSee('TestTitle');
}
}

0 comments on commit b416f88

Please sign in to comment.