Completed
Branch master (a7df58)
by Toan
03:51
created

AbstractCommand::getOptionItem()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 1
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
1
<?php
2
/**
3
 * Copyright © 2017 Toan Nguyen. All rights reserved.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
9
namespace Gojira\Command\Jira;
10
11
use Gojira\Api\Authentication\JiraBasicAuthentication;
12
use Gojira\Api\Client\GuzzleClient;
13
use Gojira\Api\Configuration\AuthInterface;
14
use Gojira\Api\Configuration\Configuration;
15
use Gojira\Api\Configuration\ConfigurationInterface;
16
use Gojira\Api\Configuration\Options;
17
use Gojira\Api\Configuration\OptionsInterface;
18
use Gojira\Provider\Console\Command;
19
use Gojira\Provider\Console\Table;
20
use Symfony\Component\Console\Output\OutputInterface;
21
22
/**
23
 * Base class for all JIRA commands.
24
 *
25
 * @package Gojira\Command\Jira
26
 * @author  Toan Nguyen <[email protected]>
27
 */
28
abstract class AbstractCommand extends Command
29
{
30
    /**
31
     * @var \Gojira\Api\Client\GuzzleClient
32
     */
33
    protected $apiClient = null;
34
35
    /**
36
     * @var \Gojira\Api\Configuration\Configuration
37
     */
38
    protected $configuration = null;
39
40
    /**
41
     * @var \Gojira\Api\Authentication\JiraBasicAuthentication
42
     */
43
    protected $authentication = null;
44
45
    /**
46
     * @var \Gojira\Provider\Console\Table
47
     */
48
    private $table = null;
49
50
    /**
51
     * @var array
52
     */
53
    protected $optionItems = null;
54
55
    /**
56
     * AbstractCommand constructor.
57
     *
58
     * @param null $name
59
     */
60
    public function __construct($name = null)
61
    {
62
        $this->configuration = new Configuration();
63
        $this->authentication = new JiraBasicAuthentication($this->configuration);
64
        parent::__construct($name);
65
    }
66
67
    /**
68
     * Get response data w/ specified endpoint instance
69
     *
70
     * @param array $filters Filters passing to endpoint
71
     *
72
     * @return mixed
73
     */
74
    abstract protected function getResponse($filters = []);
75
76
    /**
77
     * Render response data for outputting
78
     *
79
     * @param array       $response REST API response after deserialized
80
     * @param string|null $type     Result type to render
81
     *
82
     * @return mixed
83
     */
84
    abstract protected function renderResult($response = [], $type = null);
85
86
    /**
87
     * Render table faster
88
     *
89
     * @param OutputInterface $output
90
     * @param array           $data
91
     *
92
     * @return void
93
     */
94
    protected function renderTable(OutputInterface $output, array $data)
95
    {
96
        $this->table = new Table($output, $data);
97
        $this->table->render();
98
    }
99
100
    /**
101
     * Get Base URI
102
     *
103
     * @return string
104
     */
105
    protected function getBaseUri()
106
    {
107
        return $this->configuration->getData(ConfigurationInterface::AUTH . '/' . AuthInterface::BASE_URI);
108
    }
109
110
    /**
111
     * Returns ApiClient object instance
112
     *
113
     * @param bool $debug Debug current request
114
     *
115
     * @return GuzzleClient
116
     */
117
    protected function getApiClient($debug = false)
118
    {
119
        if ($this->apiClient === null) {
120
            $this->apiClient = new GuzzleClient(
121
                $this->getBaseUri(),
122
                $this->authentication,
123
                (bool)$debug,
124
                (bool)$this->optionItems[OptionsInterface::IS_USE_CACHE]
125
            );
126
        }
127
128
        return $this->apiClient;
129
    }
130
131
    /**
132
     * Get available issue statues
133
     *
134
     * @return string
135
     */
136
    protected function getAvailableStatuses()
137
    {
138
        $configItems = $this->configuration->getData(ConfigurationInterface::OPTIONS);
139
140
        return implode('", "', $configItems[OptionsInterface::AVAILABLE_ISSUES]);
141
    }
142
143
    /**
144
     * Get option item
145
     *
146
     * @param string $key
147
     *
148
     * @return array|mixed|null
149
     */
150
    protected function getOptionItem($key = null)
151
    {
152
        $optionItems = $this->configuration->getData(ConfigurationInterface::OPTIONS);
153
        if ($key === null) {
154
            return $optionItems;
155
        }
156
157
        return $optionItems[$key];
158
    }
159
160
    /**
161
     * Get application container throught Pimple
162
     *
163
     * @return mixed|null
164
     */
165
    protected function getApp()
166
    {
167
        return $this->getApplication()->getService('console');
168
    }
169
}
170