Completed
Pull Request — master (#24)
by yuuki
11:11
created

IndexFinderCommand::getArguments()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 2
b 0
f 0
nc 1
nop 0
dl 0
loc 6
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
namespace Ytake\LaravelCouchbase\Console;
13
14
use Illuminate\Console\Command;
15
use Illuminate\Database\DatabaseManager;
16
use Symfony\Component\Console\Input\InputOption;
17
use Symfony\Component\Console\Input\InputArgument;
18
use Ytake\LaravelCouchbase\Database\CouchbaseConnection;
19
20
/**
21
 * Class IndexFinderCommand
22
 */
23
class IndexFinderCommand extends Command
24
{
25
    /** @var string */
26
    protected $name = 'couchbase:list-indexes';
27
28
    /** @var string */
29
    protected $description = 'List all N1QL indexes that are registered for the current bucket.';
30
31
    /** @var DatabaseManager */
32
    protected $databaseManager;
33
34
    /** @var string */
35
    protected $defaultDatabase = 'couchbase';
36
37
    /**
38
     * IndexFinderCommand constructor.
39
     *
40
     * @param DatabaseManager $databaseManager
41
     */
42
    public function __construct(DatabaseManager $databaseManager)
43
    {
44
        $this->databaseManager = $databaseManager;
45
        parent::__construct();
46
    }
47
48
    /**
49
     * @return string[]
50
     */
51
    public function getOptions()
52
    {
53
        return [
54
            ['bucket', 'b', InputOption::VALUE_REQUIRED, 'Represents a bucket connection.'],
55
        ];
56
    }
57
58
    /**
59
     * @return string[]
60
     */
61
    protected function getArguments()
62
    {
63
        return [
64
            ['database', InputArgument::OPTIONAL, 'The database connection to use.', $this->defaultDatabase],
65
        ];
66
    }
67
68
    /**
69
     * Execute the console command
70
     */
71
    public function fire()
72
    {
73
        /** @var \Illuminate\Database\Connection|CouchbaseConnection $connection */
74
        $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...
75
        if ($connection instanceof CouchbaseConnection) {
76
            $bucket = $connection->getCouchbase()->openBucket($this->option('bucket'));
77
            $this->recursiveInformation($bucket->manager()->info());
78
79
            return;
80
        }
81
        $this->error('couchbase is not specified ');
82
83
        return;
84
    }
85
86
    /**
87
     * @param array $array
88
     * @param int   $level
89
     */
90
    private function recursiveInformation(array $array, $level = 1)
91
    {
92
        foreach ($array as $key => $value) {
93
            if (is_array($value)) {
94
                $this->recursiveInformation($value, $level + 1);
95
            } else {
96
                $this->line("<comment>{$key} : {$value}</comment>");
97
            }
98
        }
99
    }
100
}
101