Issues (16)

src/Loaders/Consul.php (4 issues)

Labels
Severity
1
<?php
2
/**
3
 * Loading values from consul-kv
4
 * User: moyo
5
 * Date: 16/10/2017
6
 * Time: 2:52 PM
7
 */
8
9
namespace Carno\Config\Loaders;
10
11
use Carno\Channel\Chan;
12
use Carno\Channel\Channel;
13
use Carno\Channel\Worker;
14
use Carno\Config\Config;
15
use Carno\Consul\KVStore;
0 ignored issues
show
The type Carno\Consul\KVStore was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
16
use Carno\Consul\Types\Agent;
0 ignored issues
show
The type Carno\Consul\Types\Agent was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
17
use Carno\Promise\Promise;
18
use Carno\Promise\Promised;
19
20
/**
21
 * @codeCoverageIgnore
22
 */
23
class Consul
24
{
25
    /**
26
     * @var string
27
     */
28
    private const KEYS = '*';
29
30
    /**
31
     * @var Chan
32
     */
33
    private $chan = null;
34
35
    /**
36
     * @var Agent
37
     */
38
    private $agent = null;
39
40
    /**
41
     * @var Config
42
     */
43
    private $config = null;
44
45
    /**
46
     * @var string
47
     */
48
    private $prefix = null;
49
50
    /**
51
     * Consul constructor.
52
     * @param Agent $agent
53
     * @param Config $source
54
     * @param string $prefix
55
     */
56
    public function __construct(Agent $agent, Config $source, string $prefix = 'conf')
57
    {
58
        $this->agent = $agent;
59
        $this->config = $source;
60
        $this->prefix = $prefix;
61
    }
62
63
    /**
64
     * @return Promised
65
     */
66
    public function connect() : Promised
67
    {
68
        $dir = $this->folder();
69
70
        (new KVStore($this->agent))->watching($dir, self::KEYS, $this->chan = new Channel());
71
72
        ($await = Promise::deferred())->then(static function () use ($dir) {
73
            logger('config')->info('Config watcher is connected', ['dir' => $dir]);
0 ignored issues
show
The function logger was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

73
            /** @scrutinizer ignore-call */ 
74
            logger('config')->info('Config watcher is connected', ['dir' => $dir]);
Loading history...
74
        });
75
76
        new Worker($this->chan, function (array $changes) use ($await) {
77
            $await->pended() && $await->resolve();
78
            foreach ($changes as $key => $value) {
79
                $this->config->set($key, $value);
80
            }
81
        });
82
83
        return $await;
84
    }
85
86
    /**
87
     * @return Promised
88
     */
89
    public function disconnect() : Promised
90
    {
91
        $dir = $this->folder();
92
93
        $this->chan->close();
94
95
        ($wait = $this->chan->closed())->then(static function () use ($dir) {
96
            logger('config')->info('Config watcher is closed', ['dir' => $dir]);
0 ignored issues
show
The function logger was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

96
            /** @scrutinizer ignore-call */ 
97
            logger('config')->info('Config watcher is closed', ['dir' => $dir]);
Loading history...
97
        });
98
99
        return $wait;
100
    }
101
102
    /**
103
     * @return string
104
     */
105
    private function folder() : string
106
    {
107
        if (empty($this->config->scoped())) {
108
            return $this->prefix;
109
        } else {
110
            return sprintf('%s/%s', $this->prefix, $this->config->scoped());
111
        }
112
    }
113
}
114