AbstractPheanstalkCommand::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 5
ccs 3
cts 3
cp 1
crap 1
rs 10
1
<?php
2
3
namespace Pyrowman\PheanstalkBundle\Command;
4
5
use Pyrowman\PheanstalkBundle\PheanstalkLocator;
6
use Pheanstalk\PheanstalkInterface;
7
use Symfony\Component\Console\Command\Command;
8
9
abstract class AbstractPheanstalkCommand extends Command
10
{
11
    /**
12
     * @var PheanstalkLocator
13
     */
14
    protected $locator;
15
16
    /**
17
     * @param PheanstalkLocator $locator
18
     */
19 4
    public function __construct(PheanstalkLocator $locator)
20
    {
21 4
        parent::__construct();
22
23 4
        $this->locator = $locator;
24
    }
25
26
    /**
27
     * @param string $name
28
     *
29
     * @return PheanstalkInterface
30
     */
31 4
    protected function getPheanstalk(&$name = null)
32
    {
33 4
        $pheanstalk = $this->locator->getPheanstalk($name);
34
35 4
        if (null === $name) {
36 4
            $name = 'default';
37
        }
38
39 4
        if (null === $pheanstalk) {
40 1
            throw new \RuntimeException('Pheanstalk not found: '.$name);
41
        }
42
43 3
        if (!$pheanstalk->getConnection()->isServiceListening()) {
44 1
            throw new \RuntimeException('Pheanstalk not connected: '.$name);
45
        }
46
47 2
        return $pheanstalk;
48
    }
49
}
50