Completed
Push — master ( 0f46ff...89eb26 )
by yuuki
11s
created

IndexFinderCommand::fire()   B

Complexity

Conditions 6
Paths 2

Size

Total Lines 23
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 6

Importance

Changes 0
Metric Value
cc 6
eloc 15
nc 2
nop 0
dl 0
loc 23
ccs 15
cts 15
cp 1
crap 6
rs 8.5906
c 0
b 0
f 0
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: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
        "state",
46
        "keyspace",
47
        "namespace",
48
        "fields",
49
        "condition",
50
    ];
51
52
    /**
53
     * IndexFinderCommand constructor.
54
     *
55
     * @param DatabaseManager $databaseManager
56
     */
57 1
    public function __construct(DatabaseManager $databaseManager)
58
    {
59 1
        $this->databaseManager = $databaseManager;
60 1
        parent::__construct();
61 1
    }
62
63
    /**
64
     * @return string[]
65
     */
66 1
    protected function getArguments()
67
    {
68
        return [
69 1
            ['bucket', InputArgument::REQUIRED, 'Represents a bucket connection.'],
70
        ];
71
    }
72
73
    /**
74
     * Get the console command options.
75
     *
76
     * @return array
77
     */
78 1
    protected function getOptions()
79
    {
80
        return [
81 1
            ['database', 'db', InputOption::VALUE_REQUIRED, 'The database connection to use.', $this->defaultDatabase],
82
        ];
83
    }
84
85
    /**
86
     * Execute the console command
87
     */
88 1
    public function fire()
89
    {
90 1
        $row = [];
91 1
        $tableRows = [];
92
        /** @var \Illuminate\Database\Connection|CouchbaseConnection $connection */
93 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...
94 1
        if ($connection instanceof CouchbaseConnection) {
95 1
            $bucket = $connection->getCouchbase()->openBucket($this->argument('bucket'));
96 1
            $indexes = $bucket->manager()->listN1qlIndexes();
97 1
            foreach ($indexes as $index) {
98 1
                foreach ($index as $key => $value) {
99 1
                    if (array_search($key, $this->headers) !== false) {
100 1
                        $row[] = (!is_array($value)) ? $value : implode(",", $value);
101
                    }
102
                }
103 1
                $tableRows[] = $row;
104 1
                $row = [];
105
            }
106 1
            $this->table($this->headers, $tableRows);
107
        }
108
109 1
        return;
110
    }
111
}
112