Completed
Push — develop ( 3bcd98...bdc471 )
by Tom
02:31
created

CreateClassifierCommand::configure()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 37
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 37
rs 8.8571
cc 1
eloc 29
nc 1
nop 0
1
<?php
2
3
namespace Bobbyshaw\WatsonVisualRecognition\Commands;
4
5
use Bobbyshaw\WatsonVisualRecognition\Classifier;
6
use Bobbyshaw\WatsonVisualRecognition\Message\ClassifierResponse;
7
use Symfony\Component\Console\Input\InputArgument;
8
use Symfony\Component\Console\Input\InputInterface;
9
use Symfony\Component\Console\Input\InputOption;
10
use Symfony\Component\Console\Output\OutputInterface;
11
use Symfony\Component\Console\Helper\Table;
12
13
/**
14
 * Command to train a new classifier
15
 *
16
 * @package Bobbyshaw\WatsonVisualRecognition\Commands
17
 * @author Tom Robertshaw <[email protected]>
18
 */
19
class CreateClassifierCommand extends BaseCommand
20
{
21
    /**
22
     * Configure command
23
     */
24
    protected function configure()
25
    {
26
        $this
27
            ->setName('classifier:create')
28
            ->setDescription('Train a new classifier')
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
            ->addArgument(
40
                'positive_examples',
41
                InputArgument::REQUIRED,
42
                'Zip of positive images.'
43
            )
44
            ->addArgument(
45
                'negative_examples',
46
                InputArgument::REQUIRED,
47
                'Zip of negative images.'
48
            )
49
            ->addArgument(
50
                'name',
51
                InputArgument::REQUIRED,
52
                'New classifier name.'
53
            )
54
            ->addOption(
55
                'version-date',
56
                '-d',
57
                InputOption::VALUE_REQUIRED,
58
                'API version date, defaults to latest release'
59
            );
60
    }
61
62
    /**
63
     * Execute command
64
     *
65
     * @param InputInterface $input
66
     * @param OutputInterface $output
67
     * @return void
68
     * @throws \Exception
69
     */
70
    protected function execute(InputInterface $input, OutputInterface $output)
71
    {
72
        $config = [
73
            'username' => $input->getArgument('username'),
74
            'password' => $input->getArgument('password'),
75
        ];
76
77
        if ($version = $input->getOption('version-date')) {
78
            $config['version'] = $version;
79
        }
80
81
        $params = [
82
            'positive_examples' => $input->getArgument('positive_examples'),
83
            'negative_examples' => $input->getArgument('negative_examples'),
84
            'name' => $input->getArgument('name')
85
86
        ];
87
88
        $this->client->initialize($config);
89
        $request = $this->client->createClassifier($params);
90
91
        /** @var ClassifierResponse $response */
92
        $response = $request->send();
93
94
        /** @var Classifier $classifier */
95
        $classifier = $response->getClassifier();
96
97
        $headers = ['ID', 'Name', 'Owner', 'Created At'];
98
        $tableRows = [
99
            [
100
                $classifier->getId(),
101
                $classifier->getName(),
102
                $classifier->getOwner(),
103
                $classifier->getCreated()->format('Y-m-d H:i:s')
104
            ]
105
        ];
106
107
        $table = new Table($output);
108
        $table->setHeaders($headers)->setRows($tableRows)->render();
109
    }
110
}
111