CreateClassifierCommand::execute()   B
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 40
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 25
CRAP Score 2

Importance

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