Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
85.71% covered (warning)
85.71%
36 / 42
75.00% covered (warning)
75.00%
3 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
MigrateMakeCommand
85.71% covered (warning)
85.71%
36 / 42
75.00% covered (warning)
75.00%
3 / 4
9.24
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 handle
64.71% covered (warning)
64.71%
11 / 17
0.00% covered (danger)
0.00%
0 / 1
7.58
 writeSqloutMigration
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
1 / 1
1
 getMigrationContents
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3namespace Baril\Sqlout\Migrations;
4
5use Baril\Sqlout\Migrations\MigrationCreator;
6use Illuminate\Database\Console\Migrations\MigrateMakeCommand as BaseCommand;
7use Illuminate\Support\Composer;
8use Illuminate\Support\Str;
9use InvalidArgumentException;
10
11class MigrateMakeCommand extends BaseCommand
12{
13    protected $signature = 'sqlout:make-migration
14        {connection? : Name of the connection}
15        {table? : Name of the table}
16        {--model= : The model that the index is for}
17        {--name= : The name of the migration.}
18        {--path= : The location where the migration file should be created.}
19        {--realpath : Indicate any provided migration file paths are pre-resolved absolute paths.}
20        {--migrate : Migrate the database after the migration file has been created.}';
21    protected $description = 'Create the migration file for Sqlout, and optionally run the migration';
22
23    public function __construct(MigrationCreator $creator, Composer $composer)
24    {
25        parent::__construct($creator, $composer);
26    }
27
28    public function handle()
29    {
30        $name = $this->input->getOption('name');
31
32        if ($modelClass = $this->input->getOption('model')) {
33            if (!class_exists($modelClass)) {
34                throw new InvalidArgumentException("$modelClass class does not exist!");
35            }
36            if (!method_exists($modelClass, 'searchableAs')) {
37                throw new InvalidArgumentException("$modelClass class is not searchable!");
38            }
39            $model = new $modelClass();
40            $connection = $model->getConnectionName();
41            $table = $model->searchableAs();
42            $name = 'create sqlout index for ' . class_basename($modelClass);
43        } else {
44            $connection = $this->input->getArgument('connection') ?? config('database.default');
45            $table = $this->input->getArgument('table') ?? config('scout.sqlout.table_name');
46            $name = "create sqlout index $connection $table";
47        }
48
49        $this->writeSqloutMigration($name, $connection, $table);
50        $this->composer->dumpAutoloads();
51
52        if ($this->input->hasOption('migrate') && $this->option('migrate')) {
53            $this->call('migrate');
54        }
55    }
56
57    protected function writeSqloutMigration($name, $connection, $tableName)
58    {
59        // Get the name for the migration file:
60        $name = Str::snake(trim($name));
61        $className = Str::studly($name);
62
63        // Generate the content of the migration file:
64        $contents = $this->getMigrationContents($className, $connection, $tableName);
65
66        // Generate the file:
67        $file = $this->creator->create(
68            $name,
69            $this->getMigrationPath(),
70            $tableName,
71            true
72        );
73        file_put_contents($file, $contents);
74
75        // Output information:
76        $file = pathinfo($file, PATHINFO_FILENAME);
77        $this->line("<info>Created Migration:</info> {$file}");
78    }
79
80    protected function getMigrationContents($className, $connection, $tableName)
81    {
82        $contents = file_get_contents(__DIR__ . '/stubs/migration.stub');
83        $contents = str_replace([
84            'class CreateSqloutIndex',
85            '::connection()',
86            "config('scout.sqlout.table_name')",
87        ], [
88            'class ' . $className,
89            "::connection('$connection')",
90            "'$tableName'"
91        ], $contents);
92        $contents = preg_replace('/\;[\s]*\/\/.*\n/U', ";\n", $contents);
93        return $contents;
94    }
95}