AbstractPheanstalkCommand::getPheanstalk()   A
last analyzed

Complexity

Conditions 4
Paths 6

Size

Total Lines 17
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 8
c 1
b 0
f 0
nc 6
nop 1
dl 0
loc 17
ccs 9
cts 9
cp 1
crap 4
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