rinvex/cortex-foundation

View on GitHub
src/Console/Commands/PolicyMakeCommand.php

Summary

Maintainability
A
0 mins
Test Coverage
<?php

declare(strict_types=1);

namespace Cortex\Foundation\Console\Commands;

use Illuminate\Support\Str;
use Illuminate\Console\ConfirmableTrait;
use Symfony\Component\Console\Attribute\AsCommand;
use Cortex\Foundation\Traits\ConsoleMakeModuleCommand;
use Illuminate\Foundation\Console\PolicyMakeCommand as BasePolicyMakeCommand;

#[AsCommand(name: 'make:policy')]
class PolicyMakeCommand extends BasePolicyMakeCommand
{
    use ConfirmableTrait;
    use ConsoleMakeModuleCommand;

    /**
     * Replace the User model namespace.
     *
     * @param string $stub
     *
     * @return string
     */
    protected function replaceUserNamespace($stub): string
    {
        if (! $userModel = config('auth.providers.'.config('auth.guards.'.config('auth.defaults.guard').'.provider').'.model')) {
            return $stub;
        }

        return str_replace(
            $this->rootNamespace().'User',
            $userModel,
            $stub
        );
    }

    /**
     * Qualify the given model class base name.
     *
     * @param string $model
     *
     * @return string
     */
    protected function qualifyModel(string $model)
    {
        $model = ltrim($model, '\\/');

        $model = str_replace('/', '\\', $model);

        $rootNamespace = $this->rootNamespace();

        if (Str::startsWith($model, $rootNamespace)) {
            return $model;
        }

        return $rootNamespace.'Models\\'.$model;
    }
}