Client::execute()   C
last analyzed

Complexity

Conditions 7
Paths 12

Size

Total Lines 34
Code Lines 21

Duplication

Lines 3
Ratio 8.82 %

Importance

Changes 0
Metric Value
cc 7
eloc 21
nc 12
nop 2
dl 3
loc 34
rs 6.7272
c 0
b 0
f 0
1
<?php
2
/**
3
 * @author    jan huang <[email protected]>
4
 * @copyright 2017
5
 *
6
 * @see      https://www.github.com/janhuang
7
 * @see      http://www.fast-d.cn/
8
 */
9
10
namespace FastD\Console;
11
12
use Symfony\Component\Console\Command\Command;
13
use Symfony\Component\Console\Input\InputArgument;
14
use Symfony\Component\Console\Input\InputInterface;
15
use Symfony\Component\Console\Input\InputOption;
16
use Symfony\Component\Console\Output\OutputInterface;
17
18
/**
19
 * Class Client.
20
 */
21
class Client extends Command
22
{
23
    protected function configure()
24
    {
25
        $this
26
            ->setName('client')
27
            ->addArgument('uri', InputArgument::REQUIRED)
28
            ->addOption('data', '-d', InputOption::VALUE_OPTIONAL, 'send data')
29
            ->addOption('json', null, InputOption::VALUE_NONE, 'output json format')
30
        ;
31
    }
32
33
    /**
34
     * @param InputInterface  $input
35
     * @param OutputInterface $output
36
     *
37
     * @return mixed
38
     */
39
    public function execute(InputInterface $input, OutputInterface $output)
40
    {
41
        $uri = $input->getArgument('uri');
42
43
        $data = $input->getParameterOption(['--data', '-d']);
44
        if (file_exists($data)) {
45
            $data = file_get_contents($data);
46
        }
47
48
        client()->createRequest($uri);
49
50
        if (false === strpos(client()->getProtocol(), 'http') && empty($data)) {
51
            $data = ' ';
52
        }
53
54
        $response = client()->send($data);
55
56
        if ($input->hasParameterOption(['--json'])) {
57
            $json = $response->getContents();
58
            $header = '';
59 View Code Duplication
            foreach ($response->getHeaders() as $key => $value) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
60
                $header .= $key.': '.$response->getHeaderLine($key).PHP_EOL;
61
            }
62
            $response = $header."\r\n".json_encode(json_decode($json, true), JSON_PRETTY_PRINT);
63
        } elseif (false !== strpos(client()->getProtocol(), 'http')) {
64
            $response = (string) $response;
65
        } else {
66
            $response = $response->getContents();
67
        }
68
69
        $output->writeln($response);
70
71
        return 0;
72
    }
73
}
74