Passed
Push — master ( cc655e...a277e5 )
by Roman
02:33
created

Env::ensure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
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;
11
use yii\base\BaseObject;
12
use yii\caching\Cache;
13
use yii\db\Connection;
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
     * @return static
50
     */
51
    public static function ensure()
52
    {
53
        return Yii::$container->get(static::class);
54
    }
55
56
    /**
57
     * @inheritdoc
58
     */
59
    public function init()
60
    {
61
        parent::init();
62
        $this->cache = Instance::ensure($this->cache, Cache::class);
63
        $this->db = Instance::ensure($this->db, Connection::class);
64
    }
65
66
    /**
67
     * @return bool
68
     */
69
    public function canListenWorkerLoop()
70
    {
71
        return !!$this->workerPingInterval;
72
    }
73
74
    /**
75
     * @return string
76
     */
77
    public function getHost()
78
    {
79
        if ($this->db->driverName === 'mysql') {
80
            $pair = $this->db
81
                ->createCommand('SELECT `HOST` FROM `information_schema`.`PROCESSLIST` WHERE `ID` = CONNECTION_ID()')
82
                ->queryScalar();
83
            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 $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

83
            return substr($pair, 0, strrpos(/** @scrutinizer ignore-type */ $pair, ':'));
Loading history...
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

83
            return substr(/** @scrutinizer ignore-type */ $pair, 0, strrpos($pair, ':'));
Loading history...
84
        }
85
86
        if ($this->db->driverName === 'pgsql') {
87
            return $this->db
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->db->create...addr()')->queryScalar() could also return false which is incompatible with the documented return type string. Did you maybe forget to handle an error condition?

If the returned type also contains false, it is an indicator that maybe an error condition leading to the specific return statement remains unhandled.

Loading history...
88
                ->createCommand('SELECT inet_client_addr()')
89
                ->queryScalar();
90
        }
91
92
        return '127.0.0.1'; // By default
93
    }
94
}
95