IndexCreatorCommand   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 97
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 3

Test Coverage

Coverage 97.44%

Importance

Changes 0
Metric Value
dl 0
loc 97
ccs 38
cts 39
cp 0.9744
rs 10
c 0
b 0
f 0
wmc 6
lcom 2
cbo 3

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getArguments() 0 8 1
B getOptions() 0 25 1
B handle() 0 25 3
1
<?php
2
declare(strict_types=1);
3
4
/**
5
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
6
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
7
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
8
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
9
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
10
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
11
 * THE SOFTWARE.
12
 */
13
14
namespace Ytake\LaravelCouchbase\Console;
15
16
use Illuminate\Console\Command;
17
use Illuminate\Database\DatabaseManager;
18
use Symfony\Component\Console\Input\InputOption;
19
use Symfony\Component\Console\Input\InputArgument;
20
use Ytake\LaravelCouchbase\Database\CouchbaseConnection;
21
22
/**
23
 * Class IndexCreatorCommand
24
 *
25
 * @author Yuuki Takezawa<[email protected]>
26
 */
27
class IndexCreatorCommand extends Command
28
{
29
    /** @var string */
30
    protected $name = 'couchbase:create-index';
31
32
    /** @var string */
33
    protected $description = 'Create a secondary index for the current bucket.';
34
35
    /** @var DatabaseManager */
36
    protected $databaseManager;
37
38
    /** @var string */
39
    protected $defaultDatabase = 'couchbase';
40
41
    /**
42
     * IndexFinderCommand constructor.
43
     *
44
     * @param DatabaseManager $databaseManager
45
     */
46 2
    public function __construct(DatabaseManager $databaseManager)
47
    {
48 2
        $this->databaseManager = $databaseManager;
49 2
        parent::__construct();
50 2
    }
51
52
    /**
53
     * @return string[]
54
     */
55 2
    protected function getArguments()
56
    {
57
        return [
58 2
            ['bucket', InputArgument::REQUIRED, 'Represents a bucket connection.'],
59 2
            ['name', InputArgument::REQUIRED, 'the name of the index.'],
60 2
            ['fields', InputArgument::IS_ARRAY | InputArgument::REQUIRED, 'the JSON fields to index.'],
61
        ];
62
    }
63
64
    /**
65
     * Get the console command options.
66
     *
67
     * @return array
68
     */
69 2
    protected function getOptions()
70
    {
71
        return [
72 2
            ['database', 'db', InputOption::VALUE_REQUIRED, 'The database connection to use.', $this->defaultDatabase],
73
            [
74 2
                'where',
75
                null,
76 2
                InputOption::VALUE_REQUIRED,
77 2
                'the WHERE clause of the index.',
78 2
                '',
79
            ],
80
            [
81 2
                'ignore',
82 2
                'ig',
83 2
                InputOption::VALUE_NONE,
84 2
                'if a primary index already exists, an exception will be thrown unless this is set to true.',
85
            ],
86
            [
87 2
                'defer',
88
                null,
89 2
                InputOption::VALUE_NONE,
90 2
                'true to defer building of the index until buildN1qlDeferredIndexes()}is called (or a direct call to the corresponding query service API)',
91
            ],
92
        ];
93
    }
94
95
    /**
96
     * Execute the console command
97
     */
98 2
    public function handle()
99
    {
100
        /** @var \Illuminate\Database\Connection|CouchbaseConnection $connection */
101 2
        $connection = $this->databaseManager->connection($this->option('database'));
0 ignored issues
show
Bug introduced by
It seems like $this->option('database') targeting Illuminate\Console\Command::option() can also be of type array; however, Illuminate\Database\DatabaseManager::connection() does only seem to accept string|null, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
102 2
        if ($connection instanceof CouchbaseConnection) {
103 2
            $bucket = $connection->openBucket($this->argument('bucket'));
0 ignored issues
show
Bug introduced by
It seems like $this->argument('bucket') targeting Illuminate\Console\Command::argument() can also be of type array; however, Ytake\LaravelCouchbase\D...onnection::openBucket() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
104 2
            $fields = $this->argument('fields');
105 2
            $whereClause = $this->option('where');
106 2
            $name = $this->argument('name');
107 2
            $bucket->manager()->createN1qlIndex(
108 2
                $name,
109 2
                $fields,
110 2
                $whereClause,
111 2
                $this->option('ignore'),
112 2
                $this->option('defer')
113
            );
114 2
            $field = implode(",", $fields);
115 2
            $this->info("created SECONDARY INDEX [{$name}] fields [{$field}], for [{$this->argument('bucket')}] bucket.");
116 2
            if ($whereClause !== '') {
117
                $this->comment("WHERE clause [{$whereClause}]");
118
            }
119
        }
120
121 2
        return;
122
    }
123
}
124