Command   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 2
dl 0
loc 47
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 1
A initialize() 0 17 4
1
<?php
2
3
namespace Solr\Console\Command\Collection;
4
5
use GuzzleHttp\Client;
6
use Symfony\Component\Console\Command\Command as BaseCommand;
7
use Symfony\Component\Console\Input\InputInterface;
8
use Symfony\Component\Console\Input\InputOption as IOpt;
9
use Symfony\Component\Console\Output\OutputInterface;
10
11
/**
12
 * Abstract command for collections command.
13
 */
14
abstract class Command extends BaseCommand
15
{
16
    /**
17
     * @var \GuzzleHttp\Client
18
     */
19
    protected $client;
20
21
    /**
22
     * Constructor.
23
     *
24
     * @param \GuzzleHttp\Client $client
25
     */
26
    public function __construct(Client $client = null)
27
    {
28
        parent::__construct();
29
30
        $this->client = $client;
31
32
        $this->addOption('host', null, IOpt::VALUE_REQUIRED, 'Solr Host')
33
             ->addOption('port', null, IOpt::VALUE_OPTIONAL, 'Solr Port', 8983)
34
             ->addOption('path', null, IOpt::VALUE_OPTIONAL, 'Solr Path', '/solr');
35
    }
36
37
    /**
38
     * Initialize command with custom client.
39
     *
40
     * @param Symfony\Component\Console\Input\InputInterface  $input
41
     * @param Symfony\Component\Console\Output\OutpuInterface $output
42
     */
43
    protected function initialize(InputInterface $input, OutputInterface $output)
44
    {
45
        $host = $input->getOption('host');
46
47
        if (is_null($host) && !$this->client instanceof Client) {
48
            throw new \RuntimeException('Please define a host or client instance');
49
        }
50
51
        if (!is_null($host)) {
52
            $port = $input->getOption('port');
53
            $path = $input->getOption('path');
54
55
            $baseUrl = sprintf('http://%s:%d%s/', $host, $port, $path);
56
57
            $this->client = new Client(['base_url' => $baseUrl]);
58
        }
59
    }
60
}
61