IndexRemoverCommand::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 5
ccs 4
cts 4
cp 1
crap 1
rs 9.4285
c 0
b 0
f 0
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 IndexRemoverCommand
24
 *
25
 * @author Yuuki Takezawa<[email protected]>
26
 */
27
class IndexRemoverCommand extends Command
28
{
29
    /** @var string */
30
    protected $name = 'couchbase:drop-index';
31
32
    /** @var string */
33
    protected $description = 'Drop the given secondary index associated with 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
        ];
61
    }
62
63
    /**
64
     * Get the console command options.
65
     *
66
     * @return array
67
     */
68 2
    protected function getOptions()
69
    {
70
        return [
71 2
            ['database', 'db', InputOption::VALUE_REQUIRED, 'The database connection to use.', $this->defaultDatabase],
72
            [
73 2
                'ignore',
74 2
                'ig',
75 2
                InputOption::VALUE_NONE,
76 2
                'if a primary index already exists, an exception will be thrown unless this is set to true.',
77
            ],
78
        ];
79
    }
80
81
    /**
82
     * Execute the console command
83
     */
84 2
    public function handle()
85
    {
86
        /** @var \Illuminate\Database\Connection|CouchbaseConnection $connection */
87 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...
88 2
        if ($connection instanceof CouchbaseConnection) {
89 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...
90 2
            $name = $this->argument('name');
91 2
            $bucket->manager()->dropN1qlIndex(
92 2
                $name,
93 2
                $this->option('ignore')
94
            );
95 2
            $this->info("dropped SECONDARY INDEX [{$name}] for [{$this->argument('bucket')}] bucket.");
96
        }
97
98 2
        return;
99
    }
100
}
101