php-tenant/Concerns/HasAnalyticsRelationships.php

67 lines
1.5 KiB
PHP
Raw Normal View History

<?php
declare(strict_types=1);
namespace Core\Tenant\Concerns;
use Illuminate\Database\Eloquent\Relations\HasMany;
/**
* Analytics-related relationships for the User model.
*
* Requires the Analytics module to be installed. All relationships are
* guarded with class_exists() so the tenant package remains functional
* even when the Analytics module is absent.
*
* @mixin \Core\Tenant\Models\User
*/
trait HasAnalyticsRelationships
{
/**
* Get all analytics websites owned by this user.
*/
public function analyticsWebsites(): ?HasMany
{
$class = $this->resolveAnalyticsModel('AnalyticsWebsite');
if (! $class) {
return null;
}
return $this->hasMany($class);
}
/**
* Get all analytics goals owned by this user.
*/
public function analyticsGoals(): ?HasMany
{
$class = $this->resolveAnalyticsModel('AnalyticsGoal');
if (! $class) {
return null;
}
return $this->hasMany($class);
}
/**
* Resolve an Analytics model class, checking common namespaces.
*/
protected function resolveAnalyticsModel(string $model): ?string
{
$candidates = [
"Core\\Mod\\Analytics\\Models\\{$model}",
"App\\Models\\{$model}",
];
foreach ($candidates as $candidate) {
if (class_exists($candidate)) {
return $candidate;
}
}
return null;
}
}