Passed
Push — master ( 056bfa...778836 )
by Mr
03:45 queued 01:50
created

Manager::getWorkers()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
namespace EasyRSA\Laravel;
4
5
use EasyRSA\Interfaces\WorkerInterface;
6
use EasyRSA\Worker;
7
use Illuminate\Contracts\Container\Container;
8
use Illuminate\Support\Arr;
9
use InvalidArgumentException;
10
11
class Manager
12
{
13
    /**
14
     * The application instance.
15
     *
16
     * @var \Illuminate\Contracts\Container\Container
17
     */
18
    protected $app;
19
20
    /**
21
     * The EasyRSA connection factory instance.
22
     *
23
     * @var \EasyRSA\Laravel\Factory
24
     */
25
    protected $factory;
26
27
    /**
28
     * The active worker instances.
29
     *
30
     * @var array
31
     */
32
    protected $workers = [];
33
34
    /**
35
     * @param \Illuminate\Contracts\Container\Container $app
36
     * @param \EasyRSA\Laravel\Factory                  $factory
37
     */
38
    public function __construct(Container $app, Factory $factory)
39
    {
40
        $this->app     = $app;
41
        $this->factory = $factory;
42
    }
43
44
    /**
45
     * Get the default worker.
46
     *
47
     * @return string
48
     */
49
    public function getDefaultWorker(): string
50
    {
51
        return $this->app['config']['easy-rsa.defaultWorker'];
52
    }
53
54
    /**
55
     * Set the default worker.
56
     *
57
     * @param string $connection
58
     */
59
    public function setDefaultWorker(string $connection): void
60
    {
61
        $this->app['config']['easy-rsa.defaultConnection'] = $connection;
62
    }
63
64
    /**
65
     * Get the configuration for a named worker.
66
     *
67
     * @param string $name
68
     *
69
     * @return array
70
     * @throws \InvalidArgumentException
71
     */
72
    protected function getConfig(string $name): array
73
    {
74
        $connections = $this->app['config']['easy-rsa.connections'];
75
76
        if (null === $config = Arr::get($connections, $name)) {
77
            throw new InvalidArgumentException("EasyRSA worker [$name] not configured.");
78
        }
79
80
        return $config;
81
    }
82
83
    /**
84
     * Make a new worker.
85
     *
86
     * @param string $name
87
     *
88
     * @return \EasyRSA\Worker
89
     */
90
    protected function makeWorker(string $name): Worker
91
    {
92
        $config = $this->getConfig($name);
93
94
        return $this->factory->make($config);
95
    }
96
97
    /**
98
     * Return all of the created workers.
99
     *
100
     * @return array
101
     */
102
    public function getWorkers(): array
103
    {
104
        return $this->workers;
105
    }
106
107
    /**
108
     * Instantiate worker object
109
     *
110
     * @param string|null $name
111
     *
112
     * @return \EasyRSA\Interfaces\WorkerInterface
113
     */
114
    public function worker(string $name = null): WorkerInterface
115
    {
116
        $name = $name ?: $this->getDefaultWorker();
117
118
        if (!isset($this->workers[$name])) {
119
            $worker = $this->makeWorker($name);
120
121
            $this->workers[$name] = $worker;
122
        }
123
124
        return $this->workers[$name];
125
    }
126
127
    /**
128
     * Dynamically pass methods to the default worker.
129
     *
130
     * @param string $method
131
     * @param array  $parameters
132
     *
133
     * @return mixed
134
     */
135
    public function __call(string $method, array $parameters)
136
    {
137
        return call_user_func_array([$this->worker(), $method], $parameters);
138
    }
139
}
140