Skip to content

Commit

Permalink
Refactor WithSeo trait to improve code readability and maintainability
Browse files Browse the repository at this point in the history
  • Loading branch information
francoism90 committed Aug 17, 2024
1 parent cbb8666 commit d7165d9
Showing 1 changed file with 7 additions and 15 deletions.
22 changes: 7 additions & 15 deletions src/Views/Concerns/WithSeo.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,21 @@
namespace Foxws\WireUse\Views\Concerns;

use Artesaos\SEOTools\Facades\SEOMeta;
use Closure;

trait WithSeo
{
public function bootWithSeo(): void
{
SEOMeta::setTitle((string) $this->seoValue('getTitle'));
SEOMeta::setDescription((string) $this->seoValue('getDescription'));
SEOMeta::setRobots((string) $this->seoValue('getRobots'));
}

protected function seoValue(mixed $value, mixed $default = null): mixed
{
if ($value instanceof Closure) {
return value($value);
if (method_exists(static::class, 'getTitle')) {
SEOMeta::setTitle($this->getTitle());
}

if (method_exists(static::class, $value)) {
$value = $this->$value();

return is_string($value) ? strip_tags($value) : $value;
if (method_exists(static::class, 'getDescription')) {
SEOMeta::setDescription($this->getDescription());
}

return $default;
if (method_exists(static::class, 'getRobots')) {
SEOMeta::setRobots($this->getRobots());
}
}
}

0 comments on commit d7165d9

Please sign in to comment.