Completed
Push — master ( 9ae6ea...b2a4a3 )
by Nenad
02:45
created

ImportCommand::handle()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 27
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 27
rs 8.8571
cc 2
eloc 17
nc 2
nop 1
1
<?php
2
3
namespace TeamTNT\Scout\Console;
4
5
use Illuminate\Console\Command;
6
use Illuminate\Contracts\Events\Dispatcher;
7
use TeamTNT\TNTSearch\TNTSearch;
8
9
class ImportCommand extends Command
10
{
11
    /**
12
     * The name and signature of the console command.
13
     *
14
     * @var string
15
     */
16
    protected $signature = 'tntsearch:import {model}';
17
18
    /**
19
     * The console command description.
20
     *
21
     * @var string
22
     */
23
    protected $description = 'Import the given model into the search index';
24
25
    /**
26
     * Execute the console command.
27
     *
28
     * @param  \Illuminate\Contracts\Events\Dispatcher  $events
29
     * @return void
30
     */
31
    public function handle(Dispatcher $events)
32
    {
33
        $class = $this->argument('model');
34
35
        $model  = new $class;
36
        $tnt    = new TNTSearch;
37
        $driver = config('database.default');
38
        $config = config('scout.tntsearch') + config("database.connections.$driver");
39
40
        $tnt->loadConfig($config);
41
        $tnt->setDatabaseHandle(app('db')->connection()->getPdo());
0 ignored issues
show
Bug introduced by
The method setDatabaseHandle() does not seem to exist on object<TeamTNT\TNTSearch\TNTSearch>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
42
43
        $indexer = $tnt->createIndex($model->searchableAs() . ".index");
44
        $indexer->setPrimaryKey($model->getKeyName());
0 ignored issues
show
Bug introduced by
The method setPrimaryKey() does not seem to exist on object<TeamTNT\TNTSearch\Indexer\TNTIndexer>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
45
        $fields = implode(', ', array_keys($model->toSearchableArray()));
46
47
        $query = "{$model->getKeyName()}, $fields";
48
49
        if ($fields == "") {
50
            $query = "*";
51
        }
52
53
        $indexer->query("SELECT $query FROM {$model->getTable()};");
54
55
        $indexer->run();
56
        $this->info('All [' . $class . '] records have been imported.');
57
    }
58
}
59