MakeSupervisorConfig   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 3
dl 0
loc 96
ccs 31
cts 31
cp 1
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 2
A handle() 0 9 1
A makeSupervisorConfig() 0 28 4
A getLogsPath() 0 4 1
A configName() 0 4 1
A getApplicationName() 0 4 1
1
<?php
2
3
namespace MadWeb\Initializer\Jobs\Supervisor;
4
5
use Illuminate\Bus\Queueable;
6
use Illuminate\Foundation\Bus\Dispatchable;
7
use Illuminate\Support\Str;
8
9
abstract class MakeSupervisorConfig
10
{
11
    use Dispatchable, Queueable;
12
13
    /**
14
     * Supervisor config folder path.
15
     */
16
    protected $path;
17
18
    /**
19
     * Supervisor configuration name.
20
     */
21
    protected $fileName;
22
23
    /**
24
     * Supervisor config parameters.
25
     */
26
    protected $params;
27
28
    /**
29
     * Name of the supervisor process.
30
     */
31
    protected $processName = '';
32
33
    /**
34
     * Create a new job instance.
35
     */
36 24
    public function __construct(array $params = [], string $fileName = '', string $path = '/etc/supervisor/conf.d/')
37
    {
38 24
        $this->path = $path;
39 24
        $this->fileName = $fileName ?: $this->configName().'.conf';
40 24
        $this->params = $params;
41 24
    }
42
43
    /**
44
     * Execute the job.
45
     *
46
     * @return string
47
     */
48 24
    public function handle()
49
    {
50 24
        file_put_contents(
51 24
            $this->path.$this->fileName,
52 24
            $this->makeSupervisorConfig($this->processName, $this->params)
53
        );
54
55 24
        return 'Supervisor config file created. Path: '.$this->path.$this->fileName;
56
    }
57
58 24
    protected function makeSupervisorConfig(string $programName, array $data)
59
    {
60
        $default_config = [
61 24
            'process_name' => '%(program_name)s_%(process_num)02d',
62 24
            'directory' => base_path(),
63
            'autostart' => true,
64
            'autorestart' => true,
65 24
            'user' => get_current_user(),
66 24
            'numprocs' => 1,
67
            'redirect_stderr' => true,
68 24
            'stdout_logfile' => "{$this->getLogsPath()}/{$this->configName()}.log",
69
        ];
70 24
        $data = array_merge($default_config, $data);
71
72 24
        $app_name = Str::slug($this->getApplicationName());
73 24
        $config = "[program:$app_name-$programName]".PHP_EOL;
74
75 24
        foreach ($data as $key => $value) {
76 24
            if (is_bool($value)) {
77 24
                $value = $value ? 'true' : 'false';
78
            } else {
79 24
                $value = (string) $value;
80
            }
81 24
            $config .= "$key=$value".PHP_EOL;
82
        }
83
84 24
        return $config;
85
    }
86
87 24
    protected function getLogsPath(): string
88
    {
89 24
        return storage_path('logs');
90
    }
91
92
    /**
93
     * @return string
94
     */
95 24
    protected function configName(): string
96
    {
97 24
        return Str::slug($this->getApplicationName().'-'.$this->processName);
98
    }
99
100 24
    protected function getApplicationName()
101
    {
102 24
        return config('app.name');
103
    }
104
}
105