DesignCreatorCommand::getArguments()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 6
ccs 2
cts 2
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\InputArgument;
19
use Symfony\Component\Console\Input\InputOption;
20
use Ytake\LaravelCouchbase\Database\CouchbaseConnection;
21
22
/**
23
 * Class DesignCreatorCommand
24
 */
25
final class DesignCreatorCommand extends Command
26
{
27
    /** @var string */
28
    protected $name = 'couchbase:create-design';
29
30
    /** @var string */
31
    protected $description = 'Inserts design document and fails if it is exist already.';
32
33
    /** @var DatabaseManager */
34
    private $databaseManager;
35
36
    /** @var string */
37
    private $defaultDatabase = 'couchbase';
38
39
    /** @var array<string, string> */
40
    private $config = [];
41
42
    /**
43
     * DesignCreatorCommand constructor.
44
     *
45
     * @param DatabaseManager $databaseManager
46
     * @param array           $config
47
     */
48 4
    public function __construct(DatabaseManager $databaseManager, array $config = [])
49
    {
50 4
        $this->databaseManager = $databaseManager;
51 4
        $this->config = $config;
52 4
        parent::__construct();
53 4
    }
54
55
    /**
56
     * @return string[]
57
     */
58 4
    protected function getArguments()
59
    {
60
        return [
61 4
            ['bucket', InputArgument::REQUIRED, 'Represents a bucket connection.'],
62
        ];
63
    }
64
65
    /**
66
     * Get the console command options.
67
     *
68
     * @return array
69
     */
70 4
    protected function getOptions()
71
    {
72
        return [
73 4
            ['database', 'db', InputOption::VALUE_REQUIRED, 'The database connection to use.', $this->defaultDatabase],
74
        ];
75
    }
76
77
    /**
78
     * Execute the console command
79
     */
80 4
    public function handle()
81
    {
82
        /** @var \Illuminate\Database\Connection|CouchbaseConnection $connection */
83 4
        $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...
84 4
        if ($connection instanceof CouchbaseConnection) {
85
            /** @var \Couchbase\Bucket $bucket */
86 4
            $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...
87 4
            foreach ($this->config as $name => $document) {
88 4
                $bucket->manager()->insertDesignDocument($name, $document);
89 4
                $this->comment("created view name [{$name}]");
90
            }
91
        }
92
93 4
        return;
94
    }
95
}
96