|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Startwind\Inventorio\Command; |
|
4
|
|
|
|
|
5
|
|
|
use Startwind\Inventorio\Config\Config; |
|
6
|
|
|
use Symfony\Component\Console\Command\Command; |
|
7
|
|
|
use Symfony\Component\Console\Input\InputOption; |
|
8
|
|
|
|
|
9
|
|
|
abstract class InventorioCommand extends Command |
|
10
|
|
|
{ |
|
11
|
|
|
protected Config $config; |
|
12
|
|
|
|
|
13
|
|
|
protected function configure(): void |
|
14
|
|
|
{ |
|
15
|
|
|
$this->addOption('configFile', 'c', InputOption::VALUE_OPTIONAL, 'The configuration file', __DIR__ . '/../../config/default.yml'); |
|
16
|
|
|
} |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Return the path to the configuration file. |
|
20
|
|
|
*/ |
|
21
|
|
|
protected function getConfigFile(): string |
|
22
|
|
|
{ |
|
23
|
|
|
return $this->config->getConfigFile(); |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
protected function initConfiguration(string $configFile): void |
|
27
|
|
|
{ |
|
28
|
|
|
$this->config = new Config($configFile); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Return true if the application is already initialized. |
|
33
|
|
|
*/ |
|
34
|
|
|
protected function isInitialized(): bool |
|
35
|
|
|
{ |
|
36
|
|
|
$configFile = $this->getConfigFile(); |
|
37
|
|
|
return file_exists($configFile); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Return the server identifier. |
|
42
|
|
|
*/ |
|
43
|
|
|
protected function getServerId(): string |
|
44
|
|
|
{ |
|
45
|
|
|
if (!$this->isInitialized()) { |
|
46
|
|
|
throw new \RuntimeException('System was not initialized yet.'); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
$config = json_decode(file_get_contents($this->getConfigFile()), true); |
|
50
|
|
|
|
|
51
|
|
|
return $config['serverId']; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Return true if remote is enabled |
|
56
|
|
|
*/ |
|
57
|
|
|
protected function isRemoteEnabled(): bool |
|
58
|
|
|
{ |
|
59
|
|
|
if (!$this->isInitialized()) { |
|
60
|
|
|
throw new \RuntimeException('System was not initialized yet.'); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
$config = json_decode(file_get_contents($this->getConfigFile()), true); |
|
64
|
|
|
|
|
65
|
|
|
if (!array_key_exists('remote', $config)) { |
|
66
|
|
|
return false; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
return $config['remote']; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Return the user identifier. |
|
74
|
|
|
*/ |
|
75
|
|
|
protected function getUserId(): string |
|
76
|
|
|
{ |
|
77
|
|
|
if (!$this->isInitialized()) { |
|
78
|
|
|
throw new \RuntimeException('System was not initialized yet.'); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
$config = json_decode(file_get_contents($this->getConfigFile()), true); |
|
82
|
|
|
|
|
83
|
|
|
return $config['userId']; |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|