ClassifyCommand::execute()   B
last analyzed

Complexity

Conditions 5
Paths 12

Size

Total Lines 39
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 27
CRAP Score 5

Importance

Changes 3
Bugs 0 Features 1
Metric Value
c 3
b 0
f 1
dl 0
loc 39
ccs 27
cts 27
cp 1
rs 8.439
cc 5
eloc 21
nc 12
nop 2
crap 5
1
<?php
2
3
namespace Bobbyshaw\WatsonVisualRecognition\Commands;
4
5
use Bobbyshaw\WatsonVisualRecognition\Classifier;
6
use Bobbyshaw\WatsonVisualRecognition\Image;
7
use Bobbyshaw\WatsonVisualRecognition\Message\ClassifyResponse;
8
use Symfony\Component\Console\Input\InputArgument;
9
use Symfony\Component\Console\Input\InputInterface;
10
use Symfony\Component\Console\Input\InputOption;
11
use Symfony\Component\Console\Output\OutputInterface;
12
use Symfony\Component\Console\Helper\Table;
13
14
/**
15
 * Command to get classify image(s)
16
 *
17
 * @package Bobbyshaw\WatsonVisualRecognition\Commands
18
 * @author Tom Robertshaw <[email protected]>
19
 */
20
class ClassifyCommand extends BaseCommand
21
{
22
    /**
23
     * Configure command
24
     */
25 1
    protected function configure()
26
    {
27 1
        parent::configure();
28
29 1
        $this
30 1
            ->setName('classifiers:classify')
31 1
            ->setDescription('Use classifiers to classify image')
32 1
            ->addArgument(
33 1
                'images',
34 1
                InputArgument::REQUIRED,
35
                'Individual or zip of images.'
36 1
            )
37 1
            ->addOption(
38 1
                'classifiers',
39 1
                '-c',
40 1
                InputOption::VALUE_REQUIRED,
41 1
                'Classifiers that should be tested against'
42 1
            );
43 1
    }
44
45
    /**
46
     * Execute command
47
     *
48
     * @param InputInterface $input
49
     * @param OutputInterface $output
50
     * @return void
51
     * @throws \Exception
52
     */
53 1
    protected function execute(InputInterface $input, OutputInterface $output)
54
    {
55
        $config = [
56 1
            'username' => $input->getArgument('username'),
57 1
            'password' => $input->getArgument('password'),
58 1
        ];
59
60 1
        if ($version = $input->getOption('version-date')) {
61 1
            $config['version'] = $version;
62 1
        }
63
64 1
        $params = ['images_file' => $input->getArgument('images')];
65
66 1
        if ($input->getOption('classifiers')) {
67 1
            $params['classifier_ids'] = explode(',', $input->getOption('classifiers'));
68 1
        }
69
70 1
        $this->client->initialize($config);
71 1
        $request = $this->client->classify($params);
72
73
        /** @var ClassifyResponse $response */
74 1
        $response = $request->send();
75 1
        $images = $response->getImages();
76
77 1
        $tableRows = [];
78
        /** @var Image $image */
79 1
        foreach ($images as $image) {
80
            /** @var Classifier $classifier */
81 1
            foreach ($image->getClassifiers() as $classifier) {
82 1
                $tableRows[] = [
83 1
                    $image->getName(), $classifier->getId(), $classifier->getName(), $classifier->getScore()
84 1
                ];
85 1
            }
86 1
        }
87
88 1
        $table = new Table($output);
89 1
        $table->setHeaders(['Image', 'Classifier ID', 'Classifier Name', 'Classifier Score'])
90 1
            ->setRows($tableRows)->render();
91 1
    }
92
}
93