Completed
Push — master ( 600e8c...3dfec0 )
by Sam
02:51
created

TvheadendStatusManagerCommand::parseConfiguration()   B

Complexity

Conditions 5
Paths 7

Size

Total Lines 49
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 11
Bugs 0 Features 4
Metric Value
c 11
b 0
f 4
dl 0
loc 49
rs 8.5906
cc 5
eloc 25
nc 7
nop 1
1
<?php
2
3
namespace Jalle19\StatusManager\Console\Commands;
4
5
use Bramus\Monolog\Formatter\ColoredLineFormatter;
6
use Jalle19\StatusManager\Application;
7
use Jalle19\StatusManager\Configuration\Configuration;
8
use Jalle19\StatusManager\Configuration\Parser as ConfigurationParser;
9
use Monolog\Handler\StreamHandler;
10
use Monolog\Logger;
11
use Monolog\Processor\PsrLogMessageProcessor;
12
use Propel\Runtime\Connection\ConnectionManagerSingle;
13
use Propel\Runtime\Propel;
14
use Propel\Runtime\ServiceContainer\StandardServiceContainer;
15
use Psr\Log\LoggerInterface;
16
use Symfony\Bridge\Monolog\Handler\ConsoleHandler;
17
use Symfony\Component\Console\Command\Command;
18
use Symfony\Component\Console\Input\InputArgument;
19
use Symfony\Component\Console\Input\InputInterface;
20
use Symfony\Component\Console\Input\InputOption;
21
use Symfony\Component\Console\Output\OutputInterface;
22
23
/**
24
 * Class TvheadendStatusManagerCommand
25
 * @package   Jalle19\StatusManager\Console\Command
26
 * @copyright Copyright &copy; Sam Stenvall 2015-
27
 * @license   https://www.gnu.org/licenses/gpl.html The GNU General Public License v2.0
28
 */
29
class TvheadendStatusManagerCommand extends Command
30
{
31
32
	const COMMAND_NAME = 'tvheadend-status-manager';
33
34
35
	/**
36
	 * @inheritdoc
37
	 */
38
	protected function configure()
39
	{
40
		$this->setName(self::COMMAND_NAME);
41
		$this->setDescription('Aggregating status manager for tvheadend instances');
42
43
		// Add arguments
44
		$this->addArgument('configFile', InputArgument::REQUIRED, 'The path to the configuration file');
45
		$this->addArgument('databaseFile', InputArgument::REQUIRED, 'The path to the database');
46
		$this->addArgument('logFile', InputArgument::OPTIONAL, 'The path to the log file');
47
48
		// Add options
49
		$this->addOption('updateInterval', 'i', InputOption::VALUE_REQUIRED, 'The status update interval (in seconds)',
50
			Configuration::DEFAULT_UPDATE_INTERVAL);
51
52
		$this->addOption('listenAddress', 'l', InputOption::VALUE_REQUIRED,
53
			'The address the Websocket server should be listening on',
54
			Configuration::DEFAULT_LISTEN_ADDRESS);
55
56
		$this->addOption('listenPort', 'p', InputOption::VALUE_REQUIRED,
57
			'The port the Websocket server should be listening on', Configuration::DEFAULT_LISTEN_PORT);
58
	}
59
60
61
	/**
62
	 * @inheritdoc
63
	 */
64
	protected function execute(InputInterface $input, OutputInterface $output)
65
	{
66
		// Parse the configuration
67
		$configuration = ConfigurationParser::parseConfiguration($input);
68
69
		// Configure the logger
70
		$logger = $this->configureLogger($output, $configuration);
71
72
		// Configure Propel
73
		$this->configurePropel($configuration, $logger);
74
75
		// Start the application
76
		$application = new Application($configuration, $logger);
77
		$application->run();
78
	}
79
80
81
	/**
82
	 * Configures and returns the logger instance
83
	 *
84
	 * @param OutputInterface $output
85
	 * @param Configuration   $configuration
86
	 *
87
	 * @return Logger
88
	 */
89
	private function configureLogger(OutputInterface $output, Configuration $configuration)
90
	{
91
		$consoleHandler = new ConsoleHandler($output);
92
		$consoleHandler->setFormatter(new ColoredLineFormatter(null, "[%datetime%] %level_name%: %message%\n"));
93
94
		$logger = new Logger(self::COMMAND_NAME);
95
		$logger->pushHandler($consoleHandler);
96
		$logger->pushProcessor(new PsrLogMessageProcessor());
97
98
		if ($configuration->getLogPath() !== null)
99
		{
100
			$fileHandler = new StreamHandler($configuration->getLogPath());
101
			$logger->pushHandler($fileHandler);
102
		}
103
104
		return $logger;
105
	}
106
107
108
	/**
109
	 * Configures the database
110
	 *
111
	 * @param Configuration   $configuration
112
	 * @param LoggerInterface $logger
113
	 */
114
	private function configurePropel(Configuration $configuration, LoggerInterface $logger)
115
	{
116
		/* @var StandardServiceContainer $serviceContainer */
117
		$serviceContainer = Propel::getServiceContainer();
118
		$serviceContainer->checkVersion('2.0.0-dev');
119
		$serviceContainer->setAdapterClass('tvheadend_status_manager', 'sqlite');
120
		$manager = new ConnectionManagerSingle();
121
		$manager->setConfiguration([
122
			'classname'  => 'Propel\\Runtime\\Connection\\ConnectionWrapper',
123
			'dsn'        => 'sqlite:' . $configuration->getDatabasePath(),
124
			'user'       => null,
125
			'password'   => '',
126
			'attributes' => [
127
				'ATTR_EMULATE_PREPARES' => false,
128
			],
129
			'settings'   => [
130
				'charset' => 'utf8',
131
				'queries' => [],
132
			],
133
		]);
134
		$manager->setName('tvheadend_status_manager');
135
		$serviceContainer->setConnectionManager('tvheadend_status_manager', $manager);
136
		$serviceContainer->setDefaultDatasource('tvheadend_status_manager');
137
138
		$serviceContainer->setLogger(self::COMMAND_NAME, $logger);
139
	}
140
141
}
142