BasePlugin   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 8
eloc 13
c 1
b 0
f 1
dl 0
loc 31
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A run() 0 7 3
A __construct() 0 3 1
A createFromGlobals() 0 9 4
1
<?php
2
3
/**
4
 * @file
5
 * BasePlugin.php
6
 *
7
 * @author: Frédéric G. MARAND <[email protected]>
8
 * @copyright (c) 2014-2020 Ouest Systèmes Informatiques (OSInet)
9
 * @license Apache License 2.0 or later
10
 */
11
12
declare(strict_types=1);
13
14
namespace OSInet\Beanstalkd\Munin;
15
16
use Pheanstalk\Connection;
17
use Pheanstalk\Contract\PheanstalkInterface;
18
use Pheanstalk\Pheanstalk;
19
use Pheanstalk\SocketFactory;
20
21
abstract class BasePlugin implements PluginInterface
22
{
23
24
    public PheanstalkInterface $server;
25
26
    public function __construct(PheanstalkInterface $server)
27
    {
28
        $this->server = $server;
29
    }
30
31
    /**
32
     * @return static
33
     */
34
    public static function createFromGlobals(): PluginInterface
35
    {
36
        $host = getenv('BEANSTALKD_HOST') ?: 'localhost';
37
        $port = (int)(getenv('BEANSTALKD_PORT') ?: 11300);
38
        $timeout = (int)(getenv('BEANSTALKD_TIMEOUT') ?: 10);
39
        $server = new Pheanstalk(
40
            new Connection(new SocketFactory($host, $port, $timeout))
41
        );
42
        return new static($server);
43
    }
44
45
    public function run(array $argv): string
46
    {
47
        $ret = (count($argv) == 2 && $argv[1] === 'config')
48
          ? $this->config()
49
          : $this->data();
50
51
        return $ret;
52
    }
53
}
54