Completed
Push — master ( 07905b...790544 )
by yuuki
10s
created

IndexCreatorCommand::getOptions()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 25
Code Lines 16

Duplication

Lines 25
Ratio 100 %

Code Coverage

Tests 14
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 16
c 0
b 0
f 0
nc 1
nop 0
dl 25
loc 25
ccs 14
cts 14
cp 1
crap 1
rs 8.8571
1
<?php
2
3
/**
4
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
5
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
6
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
7
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
8
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
9
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
10
 * THE SOFTWARE.
11
 */
12
13
namespace Ytake\LaravelCouchbase\Console;
14
15
use Illuminate\Console\Command;
16
use Illuminate\Database\DatabaseManager;
17
use Symfony\Component\Console\Input\InputOption;
18
use Symfony\Component\Console\Input\InputArgument;
19
use Ytake\LaravelCouchbase\Database\CouchbaseConnection;
20
21
/**
22
 * Class IndexCreatorCommand
23
 *
24
 * @author Yuuki Takezawa<[email protected]>
25
 */
26
class IndexCreatorCommand extends Command
27
{
28
    /** @var string */
29
    protected $name = 'couchbase:create-index';
30
31
    /** @var string */
32
    protected $description = 'Create a secondary index for the current bucket.';
33
34
    /** @var DatabaseManager */
35
    protected $databaseManager;
36
37
    /** @var string */
38
    protected $defaultDatabase = 'couchbase';
39
40
    /**
41
     * IndexFinderCommand constructor.
42
     *
43
     * @param DatabaseManager $databaseManager
44
     */
45 1
    public function __construct(DatabaseManager $databaseManager)
46
    {
47 1
        $this->databaseManager = $databaseManager;
48 1
        parent::__construct();
49 1
    }
50
51
    /**
52
     * @return string[]
53
     */
54 1
    protected function getArguments()
55
    {
56
        return [
57 1
            ['bucket', InputArgument::REQUIRED, 'Represents a bucket connection.'],
58 1
            ['name', InputArgument::REQUIRED, 'the name of the index.'],
59 1
            ['fields', InputArgument::IS_ARRAY | InputArgument::REQUIRED, 'the JSON fields to index.'],
60
        ];
61
    }
62
63
    /**
64
     * Get the console command options.
65
     *
66
     * @return array
67
     */
68 1 View Code Duplication
    protected function getOptions()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
69
    {
70
        return [
71 1
            ['database', 'db', InputOption::VALUE_REQUIRED, 'The database connection to use.', $this->defaultDatabase],
72
            [
73 1
                'where',
74
                null,
75 1
                InputOption::VALUE_REQUIRED,
76 1
                'the WHERE clause of the index.',
77 1
                '',
78
            ],
79
            [
80 1
                'ignore',
81 1
                'ig',
82 1
                InputOption::VALUE_NONE,
83 1
                'if a primary index already exists, an exception will be thrown unless this is set to true.',
84
            ],
85
            [
86 1
                'defer',
87 1
                'd',
88 1
                InputOption::VALUE_NONE,
89 1
                'true to defer building of the index until buildN1qlDeferredIndexes()}is called (or a direct call to the corresponding query service API)',
90
            ],
91
        ];
92
    }
93
94
    /**
95
     * Execute the console command
96
     */
97 1
    public function fire()
98
    {
99
        /** @var \Illuminate\Database\Connection|CouchbaseConnection $connection */
100 1
        $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...
101 1
        if ($connection instanceof CouchbaseConnection) {
102 1
            $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...
103 1
            $fields = $this->argument('fields');
104 1
            $whereClause = $this->option('where');
105 1
            $name = $this->argument('name');
106 1
            $bucket->manager()->createN1qlIndex(
107
                $name,
108
                $fields,
109
                $whereClause,
110 1
                $this->option('ignore'),
111 1
                $this->option('defer')
112
            );
113 1
            $field = implode(",", $fields);
114 1
            $this->info("created SECONDARY INDEX [{$name}] fields [{$field}], for [{$this->argument('bucket')}] bucket.");
115 1
            if ($whereClause !== '') {
116
                $this->comment("WHERE clause [{$whereClause}]");
117
            }
118
        }
119
120 1
        return;
121
    }
122
}
123