Passed
Push — master ( b313ea...47c9db )
by Roman
03:00
created

Env::getHost()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 9
nc 2
nop 0
dl 0
loc 13
rs 9.9666
c 0
b 0
f 0
1
<?php
2
/**
3
 * @link https://github.com/zhuravljov/yii2-queue-monitor
4
 * @copyright Copyright (c) 2017 Roman Zhuravlev
5
 * @license http://opensource.org/licenses/BSD-3-Clause
6
 */
7
8
namespace zhuravljov\yii\queue\monitor;
9
10
use yii\base\BaseObject;
11
use yii\caching\Cache;
12
use yii\db\Connection;
13
use yii\db\Query;
14
use yii\di\Instance;
15
16
/**
17
 * Class Env
18
 *
19
 * @author Roman Zhuravlev <[email protected]>
20
 */
21
class Env extends BaseObject
22
{
23
    /**
24
     * @var Cache|array|string
25
     */
26
    public $cache = 'cache';
27
    /**
28
     * @var Connection|array|string
29
     */
30
    public $db = 'db';
31
    /**
32
     * @var string
33
     */
34
    public $pushTableName = '{{%queue_push}}';
35
    /**
36
     * @var string
37
     */
38
    public $execTableName = '{{%queue_exec}}';
39
    /**
40
     * @var string
41
     */
42
    public $workerTableName = '{{%queue_worker}}';
43
    /**
44
     * @var int
45
     */
46
    public $workerPingInterval = 15;
47
48
    /**
49
     * @inheritdoc
50
     */
51
    public function init()
52
    {
53
        parent::init();
54
        $this->cache = Instance::ensure($this->cache, Cache::class);
55
        $this->db = Instance::ensure($this->db, Connection::class);
56
    }
57
58
    /**
59
     * @return bool
60
     */
61
    public function canListenWorkerLoop()
62
    {
63
        return !!$this->workerPingInterval;
64
    }
65
66
    /**
67
     * @return string
68
     */
69
    public function getHost()
70
    {
71
        if ($this->db->driverName === 'mysql') {
72
            $pair = (new Query())
73
                ->from('information_schema.PROCESSLIST')
74
                ->where('[[ID]] = CONNECTION_ID()')
75
                ->limit(1)
76
                ->select('HOST')
77
                ->scalar();
78
            return substr($pair, 0, strrpos($pair, ':'));
0 ignored issues
show
Bug introduced by
It seems like $pair can also be of type false; however, parameter $string of substr() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

78
            return substr(/** @scrutinizer ignore-type */ $pair, 0, strrpos($pair, ':'));
Loading history...
Bug introduced by
It seems like $pair can also be of type false; however, parameter $haystack of strrpos() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

78
            return substr($pair, 0, strrpos(/** @scrutinizer ignore-type */ $pair, ':'));
Loading history...
79
        }
80
81
        return '127.0.0.1'; // By default
82
    }
83
}
84