1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Bobbyshaw\WatsonVisualRecognition\Commands; |
4
|
|
|
|
5
|
|
|
use Bobbyshaw\WatsonVisualRecognition\Message\ClassifiersResponse; |
6
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
7
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
8
|
|
|
use Symfony\Component\Console\Input\InputOption; |
9
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
10
|
|
|
use Symfony\Component\Console\Helper\Table; |
11
|
|
|
use Bobbyshaw\WatsonVisualRecognition\Classifier; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Command to get a list of classifiers |
15
|
|
|
* |
16
|
|
|
* @package Bobbyshaw\WatsonVisualRecognition\Commands |
17
|
|
|
* @author Tom Robertshaw <[email protected]> |
18
|
|
|
*/ |
19
|
|
|
class GetClassifiersCommand extends BaseCommand |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* Configure command |
23
|
|
|
*/ |
24
|
|
View Code Duplication |
protected function configure() |
|
|
|
|
25
|
|
|
{ |
26
|
|
|
$this |
27
|
|
|
->setName('classifiers:get') |
28
|
|
|
->setDescription('Get Classifiers') |
29
|
|
|
->addArgument( |
30
|
|
|
'username', |
31
|
|
|
InputArgument::REQUIRED, |
32
|
|
|
'IBM Watson Service credentials username.' |
33
|
|
|
) |
34
|
|
|
->addArgument( |
35
|
|
|
'password', |
36
|
|
|
InputArgument::REQUIRED, |
37
|
|
|
'IBM Watson Service credentials password.' |
38
|
|
|
) |
39
|
|
|
->addOption( |
40
|
|
|
'api-verbose', |
41
|
|
|
'-a', |
42
|
|
|
InputOption::VALUE_NONE, |
43
|
|
|
'Enable verbose API request' |
44
|
|
|
) |
45
|
|
|
->addOption( |
46
|
|
|
'version-date', |
47
|
|
|
'-d', |
48
|
|
|
InputOption::VALUE_REQUIRED, |
49
|
|
|
'API version date, defaults to current date i.e. latest release' |
50
|
|
|
); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Execute command |
55
|
|
|
* |
56
|
|
|
* @param InputInterface $input |
57
|
|
|
* @param OutputInterface $output |
58
|
|
|
* @return void |
59
|
|
|
* @throws \Exception |
60
|
|
|
*/ |
61
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
62
|
|
|
{ |
63
|
|
|
$config = [ |
64
|
|
|
'username' => $input->getArgument('username'), |
65
|
|
|
'password' => $input->getArgument('password'), |
66
|
|
|
]; |
67
|
|
|
|
68
|
|
|
if ($version = $input->getOption('version-date')) { |
69
|
|
|
$config['version'] = $version; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
$this->client->initialize($config); |
73
|
|
|
|
74
|
|
|
$params = []; |
75
|
|
|
if ($verbose = $input->getOption('api-verbose')) { |
76
|
|
|
$params['verbose'] = $verbose; |
77
|
|
|
} |
78
|
|
|
$request = $this->client->getClassifiers($params); |
79
|
|
|
|
80
|
|
|
/** @var ClassifiersResponse $response */ |
81
|
|
|
$response = $request->send(); |
82
|
|
|
|
83
|
|
|
$classifiers = $response->getClassifiers(); |
84
|
|
|
|
85
|
|
|
$tableRows = []; |
86
|
|
|
/** @var Classifier $classifier */ |
87
|
|
|
foreach ($classifiers as $classifier) { |
88
|
|
|
$row = [$classifier->getId(), $classifier->getName()]; |
89
|
|
|
if ($verbose) { |
90
|
|
|
$row = array_merge($row, [$classifier->getOwner(), $classifier->getCreated()->format('Y-m-d H:i:s')]); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
$tableRows[] = $row; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
$headers = ['ID', 'Name']; |
97
|
|
|
if ($verbose) { |
98
|
|
|
$headers = array_merge($headers, ['Owner', 'Created At']); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
$table = new Table($output); |
102
|
|
|
$table->setHeaders($headers)->setRows($tableRows)->render(); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
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.