Completed
Branch master (07905b)
by yuuki
01:56
created

QueueCreatorCommand::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 5
rs 9.4285
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 QueueCreatorCommand
23
 *
24
 * @codeCoverageIgnore
25
 *
26
 * @author Yuuki Takezawa<[email protected]>
27
 */
28
class QueueCreatorCommand extends Command
29
{
30
    /** @var string */
31
    protected $name = 'couchbase:create-queue-index';
32
33
    /** @var string */
34
    protected $description = 'Create primary index, secondary indexes for the queue jobs couchbase bucket.';
35
36
    /** @var DatabaseManager */
37
    protected $databaseManager;
38
39
    /** @var string */
40
    protected $defaultDatabase = 'couchbase';
41
42
    const PRIMARY_KEY = '#job_queue_primary';
43
44
    /** @var string[] */
45
    protected $secondaryIndexes = [
46
        'idx_job_queue'       => [ // index name
47
            'queue', // fields
48
        ],
49
        'idx_job_identifier'  => [
50
            'id',
51
        ],
52
        'idx_job_queue_cover' => [
53
            'queue',
54
            'reserved',
55
            'reserved_at',
56
            'available_at',
57
            'id',
58
        ],
59
    ];
60
61
    /**
62
     * IndexFinderCommand constructor.
63
     *
64
     * @param DatabaseManager $databaseManager
65
     */
66
    public function __construct(DatabaseManager $databaseManager)
67
    {
68
        $this->databaseManager = $databaseManager;
69
        parent::__construct();
70
    }
71
72
    /**
73
     * @return string[]
74
     */
75
    protected function getArguments()
76
    {
77
        return [
78
            ['bucket', InputArgument::OPTIONAL, 'Represents a bucket connection.', 'jobs'],
79
        ];
80
    }
81
82
    /**
83
     * Get the console command options.
84
     *
85
     * @return array
86
     */
87 View Code Duplication
    protected function getOptions()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
88
    {
89
        return [
90
            ['database', 'db', InputOption::VALUE_REQUIRED, 'The database connection to use.', $this->defaultDatabase],
91
            [
92
                'ignore',
93
                'ig',
94
                InputOption::VALUE_NONE,
95
                'if a primary index already exists, an exception will be thrown unless this is set to true.',
96
            ],
97
            [
98
                'defer',
99
                null,
100
                InputOption::VALUE_NONE,
101
                'true to defer building of the index until buildN1qlDeferredIndexes()}is called (or a direct call to the corresponding query service API)',
102
            ],
103
        ];
104
    }
105
106
    /**
107
     * Execute the console command
108
     */
109
    public function fire()
110
    {
111
        /** @var \Illuminate\Database\Connection|CouchbaseConnection $connection */
112
        $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...
113
        if ($connection instanceof CouchbaseConnection) {
114
            $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...
115
            $primary = self::PRIMARY_KEY;
116
            try {
117
                $bucket->manager()->createN1qlPrimaryIndex(
118
                    $primary,
119
                    $this->option('ignore'),
120
                    $this->option('defer')
121
                );
122
                $this->info("created PRIMARY INDEX [{$primary}] for [{$this->argument('bucket')}] bucket.");
123
            } catch (\Exception $e) {
124
                $this->error($e->getMessage());
125
            }
126
            foreach ($this->secondaryIndexes as $name => $fields) {
127
                try {
128
                    $bucket->manager()->createN1qlIndex(
129
                        $name,
130
                        $fields,
131
                        '',
132
                        $this->option('ignore'),
133
                        $this->option('defer')
134
                    );
135
                    $field = implode(",", $fields);
136
                    $this->info("created SECONDARY INDEX [{$name}] fields [{$field}], for [{$this->argument('bucket')}] bucket.");
137
                } catch (\Exception $e) {
138
                    $this->error($e->getMessage());
139
                }
140
            }
141
        }
142
143
        return;
144
    }
145
}
146