Completed
Push — master ( a9471a...211374 )
by yuuki
16:33 queued 09:44
created

IndexFinderCommand::getOptions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 6
ccs 1
cts 1
cp 1
crap 1
rs 9.4285
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 IndexFinderCommand
23
 *
24
 * @author Yuuki Takezawa<[email protected]>
25
 */
26
class IndexFinderCommand extends Command
27
{
28
    /** @var string */
29
    protected $name = 'couchbase:list-indexes';
30
31
    /** @var string */
32
    protected $description = 'List all N1QL indexes that are registered for the current bucket.';
33
34
    /** @var DatabaseManager */
35
    protected $databaseManager;
36
37
    /** @var string */
38
    protected $defaultDatabase = 'couchbase';
39
40
    /** @var string[] */
41
    private $headers = [
42
        "name",
43
        "isPrimary",
44
        "type",
45 1
        "state",
46
        "keyspace",
47 1
        "namespace",
48 1
        "fields",
49 1
        "condition",
50
    ];
51
52
    /**
53
     * IndexFinderCommand constructor.
54 1
     *
55
     * @param DatabaseManager $databaseManager
56
     */
57 1
    public function __construct(DatabaseManager $databaseManager)
58
    {
59
        $this->databaseManager = $databaseManager;
60
        parent::__construct();
61
    }
62
63
    /**
64 1
     * @return string[]
65
     */
66
    public function getOptions()
67 1
    {
68
        return [
69
            ['bucket', 'b', InputOption::VALUE_REQUIRED, 'Represents a bucket connection.'],
70
        ];
71
    }
72
73
    /**
74 1
     * @return string[]
75
     */
76
    protected function getArguments()
77 1
    {
78 1
        return [
79 1
            ['database', InputArgument::OPTIONAL, 'The database connection to use.', $this->defaultDatabase],
80 1
        ];
81
    }
82 1
83
    /**
84
     * Execute the console command
85
     */
86
    public function fire()
87
    {
88
        $row = [];
89
        $tableRows = [];
90
        /** @var \Illuminate\Database\Connection|CouchbaseConnection $connection */
91
        $connection = $this->databaseManager->connection($this->argument('database'));
0 ignored issues
show
Bug introduced by
It seems like $this->argument('database') targeting Illuminate\Console\Command::argument() 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...
92
        if ($connection instanceof CouchbaseConnection) {
93 1
            $bucket = $connection->getCouchbase()->openBucket($this->option('bucket'));
94
            $indexes = $bucket->manager()->listN1qlIndexes();
95 1
            // $this->recursiveInformation($bucket->manager()->info());
0 ignored issues
show
Unused Code Comprehensibility introduced by
75% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
96 1
            foreach ($indexes as $index) {
97 1
                foreach ($index as $key => $value) {
98
                    if (array_search($key, $this->headers) !== false) {
99 1
                        $row[] = (!is_array($value)) ? $value : implode(",", $value);
100
                    }
101
                }
102 1
                $tableRows[] = $row;
103
            }
104
            $this->table($this->headers, $tableRows);
105
106
            return;
107
        }
108
        $this->error('couchbase is not specified ');
109
110
        return;
111
    }
112
}
113