PrimaryIndexCreatorCommand::handle()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 10
nc 2
nop 0
dl 0
loc 16
rs 9.4285
c 0
b 0
f 0
ccs 10
cts 10
cp 1
crap 2
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 PrimaryIndexCreatorCommand
24
 *
25
 * @author Yuuki Takezawa<[email protected]>
26
 */
27
class PrimaryIndexCreatorCommand extends Command
28
{
29
    /** @var string */
30
    protected $name = 'couchbase:create-primary-index';
31
32
    /** @var string */
33
    protected $description = 'Create a primary N1QL 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
        ];
60
    }
61
62
    /**
63
     * Get the console command options.
64
     *
65
     * @return array
66
     */
67 2
    protected function getOptions()
68
    {
69
        return [
70 2
            ['database', 'db', InputOption::VALUE_REQUIRED, 'The database connection to use.', $this->defaultDatabase],
71 2
            ['name', null, InputOption::VALUE_REQUIRED, 'the custom name for the primary index.', '#primary'],
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 2
                'defer',
80
                null,
81 2
                InputOption::VALUE_NONE,
82 2
                'true to defer building of the index until buildN1qlDeferredIndexes()}is called (or a direct call to the corresponding query service API)',
83
            ],
84
        ];
85
    }
86
87
    /**
88
     * Execute the console command
89
     */
90 2
    public function handle()
91
    {
92
        /** @var \Illuminate\Database\Connection|CouchbaseConnection $connection */
93 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...
94 2
        if ($connection instanceof CouchbaseConnection) {
95 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...
96 2
            $bucket->manager()->createN1qlPrimaryIndex(
97 2
                $this->option('name'),
98 2
                $this->option('ignore'),
99 2
                $this->option('defer')
100
            );
101 2
            $this->info("created PRIMARY INDEX [{$this->option('name')}] for [{$this->argument('bucket')}] bucket.");
102
        }
103
104 2
        return;
105
    }
106
}
107