@@ -92,8 +92,9 @@ discard block |
||
| 92 | 92 | */ |
| 93 | 93 | public function setUpdateInterval($updateInterval) |
| 94 | 94 | { |
| 95 | - if ($updateInterval <= 0) |
|
| 96 | - throw new \RuntimeException('Invalid update interval specified'); |
|
| 95 | + if ($updateInterval <= 0) { |
|
| 96 | + throw new \RuntimeException('Invalid update interval specified'); |
|
| 97 | + } |
|
| 97 | 98 | |
| 98 | 99 | $this->_updateInterval = $updateInterval; |
| 99 | 100 | } |
@@ -133,8 +134,9 @@ discard block |
||
| 133 | 134 | */ |
| 134 | 135 | public function setListenPort($listenPort) |
| 135 | 136 | { |
| 136 | - if ($listenPort < 1 || $listenPort > 65535) |
|
| 137 | - throw new \RuntimeException('Invalid port specified'); |
|
| 137 | + if ($listenPort < 1 || $listenPort > 65535) { |
|
| 138 | + throw new \RuntimeException('Invalid port specified'); |
|
| 139 | + } |
|
| 138 | 140 | |
| 139 | 141 | $this->_listenPort = $listenPort; |
| 140 | 142 | } |
@@ -142,7 +142,7 @@ |
||
| 142 | 142 | if ($configuration === false) |
| 143 | 143 | throw new InvalidConfigurationException('Failed to parse the specified configuration file'); |
| 144 | 144 | |
| 145 | - $instances = []; |
|
| 145 | + $instances = []; |
|
| 146 | 146 | |
| 147 | 147 | // Parse sections |
| 148 | 148 | foreach ($configuration as $section => $values) |
@@ -124,23 +124,26 @@ discard block |
||
| 124 | 124 | private function parseConfiguration(InputInterface $input) |
| 125 | 125 | { |
| 126 | 126 | // Check that the configuration file exists |
| 127 | - if (!file_exists($input->getArgument('configFile'))) |
|
| 128 | - throw new InvalidConfigurationException('The specified configuration file does not exist'); |
|
| 127 | + if (!file_exists($input->getArgument('configFile'))) { |
|
| 128 | + throw new InvalidConfigurationException('The specified configuration file does not exist'); |
|
| 129 | + } |
|
| 129 | 130 | |
| 130 | 131 | // Check that the database exists and is writable |
| 131 | 132 | $databasePath = $input->getArgument('databaseFile'); |
| 132 | 133 | |
| 133 | - if (!file_exists($databasePath)) |
|
| 134 | - throw new InvalidConfigurationException('The specified database path does not exist'); |
|
| 135 | - else if (!is_writable($databasePath)) |
|
| 136 | - throw new InvalidConfigurationException('The specified database path is not writable'); |
|
| 134 | + if (!file_exists($databasePath)) { |
|
| 135 | + throw new InvalidConfigurationException('The specified database path does not exist'); |
|
| 136 | + } else if (!is_writable($databasePath)) { |
|
| 137 | + throw new InvalidConfigurationException('The specified database path is not writable'); |
|
| 138 | + } |
|
| 137 | 139 | |
| 138 | 140 | // Parse the configuration file |
| 139 | 141 | $configuration = parse_ini_file($input->getArgument('configFile'), true); |
| 140 | 142 | |
| 141 | 143 | // Check that the file was parsed |
| 142 | - if ($configuration === false) |
|
| 143 | - throw new InvalidConfigurationException('Failed to parse the specified configuration file'); |
|
| 144 | + if ($configuration === false) { |
|
| 145 | + throw new InvalidConfigurationException('Failed to parse the specified configuration file'); |
|
| 146 | + } |
|
| 144 | 147 | |
| 145 | 148 | $instances = []; |
| 146 | 149 | |
@@ -157,12 +160,14 @@ discard block |
||
| 157 | 160 | $instance = new Instance($name, $address, $port); |
| 158 | 161 | |
| 159 | 162 | // Optionally set ignored users |
| 160 | - if (isset($values['ignoredUsers'])) |
|
| 161 | - $instance->setIgnoredUsers($values['ignoredUsers']); |
|
| 163 | + if (isset($values['ignoredUsers'])) { |
|
| 164 | + $instance->setIgnoredUsers($values['ignoredUsers']); |
|
| 165 | + } |
|
| 162 | 166 | |
| 163 | 167 | // Optionally set credentials |
| 164 | - if (isset($values['username']) && isset($values['password'])) |
|
| 165 | - $instance->setCredentials($values['username'], $values['password']); |
|
| 168 | + if (isset($values['username']) && isset($values['password'])) { |
|
| 169 | + $instance->setCredentials($values['username'], $values['password']); |
|
| 170 | + } |
|
| 166 | 171 | |
| 167 | 172 | $instances[] = $instance; |
| 168 | 173 | break; |
@@ -170,8 +175,9 @@ discard block |
||
| 170 | 175 | } |
| 171 | 176 | |
| 172 | 177 | // Validate the configuration. We need at least one instance. |
| 173 | - if (empty($instances)) |
|
| 174 | - throw new InvalidConfigurationException('No instances defined, you need to specify at least one instance'); |
|
| 178 | + if (empty($instances)) { |
|
| 179 | + throw new InvalidConfigurationException('No instances defined, you need to specify at least one instance'); |
|
| 180 | + } |
|
| 175 | 181 | |
| 176 | 182 | // Create the configuration object |
| 177 | 183 | $config = new Configuration($databasePath, $instances); |
@@ -200,8 +206,9 @@ discard block |
||
| 200 | 206 | */ |
| 201 | 207 | private static function getSectionType($section) |
| 202 | 208 | { |
| 203 | - if (substr($section, 0, 8) === 'instance') |
|
| 204 | - return Configuration::SECTION_TYPE_INSTANCE; |
|
| 209 | + if (substr($section, 0, 8) === 'instance') { |
|
| 210 | + return Configuration::SECTION_TYPE_INSTANCE; |
|
| 211 | + } |
|
| 205 | 212 | |
| 206 | 213 | throw new InvalidConfigurationException('Unknown section "' . $section . '"'); |
| 207 | 214 | } |
@@ -91,13 +91,13 @@ discard block |
||
| 91 | 91 | $this->onUserSeen($instanceName, $connectionStatus->user); |
| 92 | 92 | |
| 93 | 93 | $user = UserQuery::create()->filterByInstanceName($instanceName)->filterByName($connectionStatus->user) |
| 94 | - ->findOne(); |
|
| 94 | + ->findOne(); |
|
| 95 | 95 | } |
| 96 | 96 | |
| 97 | 97 | $connection = new Connection(); |
| 98 | 98 | $connection->setInstanceName($instanceName)->setPeer($connectionStatus->peer) |
| 99 | - ->setUser($user) |
|
| 100 | - ->setStarted($connectionStatus->started)->setType($connectionStatus->type)->save(); |
|
| 99 | + ->setUser($user) |
|
| 100 | + ->setStarted($connectionStatus->started)->setType($connectionStatus->type)->save(); |
|
| 101 | 101 | |
| 102 | 102 | $this->_logger->info('Stored new connection (instance: {instanceName}, peer: {peer})', [ |
| 103 | 103 | 'instanceName' => $instanceName, |
@@ -185,8 +185,8 @@ discard block |
||
| 185 | 185 | |
| 186 | 186 | $subscription = new Subscription(); |
| 187 | 187 | $subscription->setInstance($instance)->setUser($user)->setChannel($channel) |
| 188 | - ->setSubscriptionId($status->id)->setStarted($status->start)->setTitle($status->title) |
|
| 189 | - ->setService($status->service); |
|
| 188 | + ->setSubscriptionId($status->id)->setStarted($status->start)->setTitle($status->title) |
|
| 189 | + ->setService($status->service); |
|
| 190 | 190 | $subscription->save(); |
| 191 | 191 | |
| 192 | 192 | $this->_logger->info('Stored new subscription (instance: {instanceName}, user: {userName}, channel: {channelName})', |
@@ -210,8 +210,8 @@ discard block |
||
| 210 | 210 | |
| 211 | 211 | // Find the latest matching subscription |
| 212 | 212 | $subscription = SubscriptionQuery::create()->filterByInstanceName($instanceName) |
| 213 | - ->filterBySubscriptionId($stateChange->getSubscriptionId()) |
|
| 214 | - ->addDescendingOrderByColumn('started')->findOne(); |
|
| 213 | + ->filterBySubscriptionId($stateChange->getSubscriptionId()) |
|
| 214 | + ->addDescendingOrderByColumn('started')->findOne(); |
|
| 215 | 215 | |
| 216 | 216 | if ($subscription === null) |
| 217 | 217 | { |
@@ -259,7 +259,7 @@ discard block |
||
| 259 | 259 | private function hasConnection($instanceName, ConnectionStatus $connectionStatus) |
| 260 | 260 | { |
| 261 | 261 | return ConnectionQuery::create()->filterByInstanceName($instanceName)->filterByPeer($connectionStatus->peer) |
| 262 | - ->filterByStarted($connectionStatus->started)->findOne() !== null; |
|
| 262 | + ->filterByStarted($connectionStatus->started)->findOne() !== null; |
|
| 263 | 263 | } |
| 264 | 264 | |
| 265 | 265 | |
@@ -284,7 +284,7 @@ discard block |
||
| 284 | 284 | private function hasChannel($instanceName, $channelName) |
| 285 | 285 | { |
| 286 | 286 | return ChannelQuery::create()->filterByInstanceName($instanceName)->filterByName($channelName) |
| 287 | - ->findOne() !== null; |
|
| 287 | + ->findOne() !== null; |
|
| 288 | 288 | } |
| 289 | 289 | |
| 290 | 290 | |
@@ -307,9 +307,9 @@ discard block |
||
| 307 | 307 | $userId = $user !== null ? $user->getId() : null; |
| 308 | 308 | |
| 309 | 309 | return SubscriptionQuery::create()->filterByInstance($instance)->filterByUserId($userId) |
| 310 | - ->filterByChannel($channel) |
|
| 311 | - ->filterBySubscriptionId($subscription->id)->filterByStarted($subscription->start) |
|
| 312 | - ->findOne() !== null; |
|
| 310 | + ->filterByChannel($channel) |
|
| 311 | + ->filterBySubscriptionId($subscription->id)->filterByStarted($subscription->start) |
|
| 312 | + ->findOne() !== null; |
|
| 313 | 313 | } |
| 314 | 314 | |
| 315 | 315 | } |
@@ -50,8 +50,9 @@ discard block |
||
| 50 | 50 | */ |
| 51 | 51 | public function onInstanceSeen(Tvheadend $instance) |
| 52 | 52 | { |
| 53 | - if ($this->hasInstance($instance)) |
|
| 54 | - return; |
|
| 53 | + if ($this->hasInstance($instance)) { |
|
| 54 | + return; |
|
| 55 | + } |
|
| 55 | 56 | |
| 56 | 57 | $instanceModel = new Database\Instance(); |
| 57 | 58 | $instanceModel->setPrimaryKey($instance->getHostname()); |
@@ -80,8 +81,9 @@ discard block |
||
| 80 | 81 | */ |
| 81 | 82 | public function onConnectionSeen($instanceName, ConnectionStatus $connectionStatus) |
| 82 | 83 | { |
| 83 | - if ($this->hasConnection($instanceName, $connectionStatus)) |
|
| 84 | - return; |
|
| 84 | + if ($this->hasConnection($instanceName, $connectionStatus)) { |
|
| 85 | + return; |
|
| 86 | + } |
|
| 85 | 87 | |
| 86 | 88 | $user = null; |
| 87 | 89 | |
@@ -114,8 +116,9 @@ discard block |
||
| 114 | 116 | */ |
| 115 | 117 | public function onUserSeen($instanceName, $userName) |
| 116 | 118 | { |
| 117 | - if ($this->hasUser($instanceName, $userName)) |
|
| 118 | - return; |
|
| 119 | + if ($this->hasUser($instanceName, $userName)) { |
|
| 120 | + return; |
|
| 121 | + } |
|
| 119 | 122 | |
| 120 | 123 | $user = new User(); |
| 121 | 124 | $user->setInstanceName($instanceName)->setName($userName); |
@@ -136,8 +139,9 @@ discard block |
||
| 136 | 139 | */ |
| 137 | 140 | public function onChannelSeen($instanceName, $channelName) |
| 138 | 141 | { |
| 139 | - if ($this->hasChannel($instanceName, $channelName)) |
|
| 140 | - return; |
|
| 142 | + if ($this->hasChannel($instanceName, $channelName)) { |
|
| 143 | + return; |
|
| 144 | + } |
|
| 141 | 145 | |
| 142 | 146 | $channel = new Channel(); |
| 143 | 147 | $channel->setInstanceName($instanceName)->setName($channelName); |
@@ -159,8 +163,9 @@ discard block |
||
| 159 | 163 | public function onSubscriptionSeen($instanceName, SubscriptionStatus $status) |
| 160 | 164 | { |
| 161 | 165 | // Ignore EPG grabber subscriptions |
| 162 | - if ($status->getType() === SubscriptionStatus::TYPE_EPGGRAB) |
|
| 163 | - return; |
|
| 166 | + if ($status->getType() === SubscriptionStatus::TYPE_EPGGRAB) { |
|
| 167 | + return; |
|
| 168 | + } |
|
| 164 | 169 | |
| 165 | 170 | // Determine the username to store for the subscription |
| 166 | 171 | $username = $status->username; |
@@ -180,8 +185,9 @@ discard block |
||
| 180 | 185 | $this->onChannelSeen($instanceName, $status->channel); |
| 181 | 186 | $channel = ChannelQuery::create()->filterByInstance($instance)->filterByName($status->channel)->findOne(); |
| 182 | 187 | |
| 183 | - if ($this->hasSubscription($instance, $user, $channel, $status)) |
|
| 184 | - return; |
|
| 188 | + if ($this->hasSubscription($instance, $user, $channel, $status)) { |
|
| 189 | + return; |
|
| 190 | + } |
|
| 185 | 191 | |
| 186 | 192 | $subscription = new Subscription(); |
| 187 | 193 | $subscription->setInstance($instance)->setUser($user)->setChannel($channel) |
@@ -205,8 +211,9 @@ discard block |
||
| 205 | 211 | public function onSubscriptionStateChange($instanceName, StateChange $stateChange) |
| 206 | 212 | { |
| 207 | 213 | // We only need to persist subscription stops |
| 208 | - if ($stateChange->getState() === StateChange::STATE_SUBSCRIPTION_STARTED) |
|
| 209 | - return; |
|
| 214 | + if ($stateChange->getState() === StateChange::STATE_SUBSCRIPTION_STARTED) { |
|
| 215 | + return; |
|
| 216 | + } |
|
| 210 | 217 | |
| 211 | 218 | // Find the latest matching subscription |
| 212 | 219 | $subscription = SubscriptionQuery::create()->filterByInstanceName($instanceName) |
@@ -251,7 +251,7 @@ |
||
| 251 | 251 | |
| 252 | 252 | |
| 253 | 253 | /** |
| 254 | - * @param $instanceName |
|
| 254 | + * @param string $instanceName |
|
| 255 | 255 | * @param ConnectionStatus $connectionStatus |
| 256 | 256 | * |
| 257 | 257 | * @return bool whether the connection exists in the database |
@@ -70,8 +70,9 @@ discard block |
||
| 70 | 70 | $this->_instances = new \SplObjectStorage(); |
| 71 | 71 | |
| 72 | 72 | // Attach a state to each instance |
| 73 | - foreach ($this->_configuration->getInstances() as $instance) |
|
| 74 | - $this->_instances->attach($instance, new InstanceState()); |
|
| 73 | + foreach ($this->_configuration->getInstances() as $instance) { |
|
| 74 | + $this->_instances->attach($instance, new InstanceState()); |
|
| 75 | + } |
|
| 75 | 76 | |
| 76 | 77 | // Start the persistence manager |
| 77 | 78 | $this->_persistenceManager = new PersistenceManager($logger); |
@@ -147,16 +148,19 @@ discard block |
||
| 147 | 148 | ]); |
| 148 | 149 | |
| 149 | 150 | // Persist connections |
| 150 | - foreach ($instanceStatus->getConnections() as $connection) |
|
| 151 | - $this->_persistenceManager->onConnectionSeen($instanceName, $connection); |
|
| 151 | + foreach ($instanceStatus->getConnections() as $connection) { |
|
| 152 | + $this->_persistenceManager->onConnectionSeen($instanceName, $connection); |
|
| 153 | + } |
|
| 152 | 154 | |
| 153 | 155 | // Persist running subscriptions |
| 154 | - foreach ($instanceStatus->getSubscriptions() as $subscription) |
|
| 155 | - $this->_persistenceManager->onSubscriptionSeen($instanceName, $subscription); |
|
| 156 | + foreach ($instanceStatus->getSubscriptions() as $subscription) { |
|
| 157 | + $this->_persistenceManager->onSubscriptionSeen($instanceName, $subscription); |
|
| 158 | + } |
|
| 156 | 159 | |
| 157 | 160 | // Handle subscription state changes |
| 158 | - foreach ($instanceStatus->getSubscriptionStateChanges() as $subscriptionStateChange) |
|
| 159 | - $this->_persistenceManager->onSubscriptionStateChange($instanceName, $subscriptionStateChange); |
|
| 161 | + foreach ($instanceStatus->getSubscriptionStateChanges() as $subscriptionStateChange) { |
|
| 162 | + $this->_persistenceManager->onSubscriptionStateChange($instanceName, $subscriptionStateChange); |
|
| 163 | + } |
|
| 160 | 164 | } |
| 161 | 165 | |
| 162 | 166 | // Broadcast the status messages to all connected clients |
@@ -204,8 +208,7 @@ discard block |
||
| 204 | 208 | |
| 205 | 209 | $instanceState->setReachability(InstanceState::REACHABLE); |
| 206 | 210 | } |
| 207 | - } |
|
| 208 | - catch (\Exception $e) |
|
| 211 | + } catch (\Exception $e) |
|
| 209 | 212 | { |
| 210 | 213 | // Mark the instance as unreachable |
| 211 | 214 | $message = 'Instance {instanceName} not reachable, will wait for {cycles} cycles before retrying. |
@@ -219,8 +222,7 @@ discard block |
||
| 219 | 222 | |
| 220 | 223 | $instanceState->setReachability(InstanceState::UNREACHABLE); |
| 221 | 224 | } |
| 222 | - } |
|
| 223 | - else |
|
| 225 | + } else |
|
| 224 | 226 | { |
| 225 | 227 | // Wait for some cycles and then mark unreachable instances as maybe reachable |
| 226 | 228 | if ($instanceState->getRetryCount() === self::UNREACHABLE_CYCLES_UNTIL_RETRY - 1) |
@@ -231,9 +233,9 @@ discard block |
||
| 231 | 233 | $this->_logger->info('Retrying instance {instanceName} during next cycle', [ |
| 232 | 234 | 'instanceName' => $instanceName, |
| 233 | 235 | ]); |
| 236 | + } else { |
|
| 237 | + $instanceState->incrementRetryCount(); |
|
| 234 | 238 | } |
| 235 | - else |
|
| 236 | - $instanceState->incrementRetryCount(); |
|
| 237 | 239 | } |
| 238 | 240 | } |
| 239 | 241 | |
@@ -29,8 +29,9 @@ discard block |
||
| 29 | 29 | { |
| 30 | 30 | $stateChange = self::parseMessage($logMessage->logtxt); |
| 31 | 31 | |
| 32 | - if ($stateChange !== null) |
|
| 33 | - $stateChanges[] = $stateChange; |
|
| 32 | + if ($stateChange !== null) { |
|
| 33 | + $stateChanges[] = $stateChange; |
|
| 34 | + } |
|
| 34 | 35 | } |
| 35 | 36 | |
| 36 | 37 | return $stateChanges; |
@@ -54,10 +55,11 @@ discard block |
||
| 54 | 55 | $stateChange = new StateChange(self::getSubscriptionId($messageParts[3])); |
| 55 | 56 | |
| 56 | 57 | // Check the state |
| 57 | - if (strpos($message, 'subscribing on channel') !== false) |
|
| 58 | - $stateChange->setState(StateChange::STATE_SUBSCRIPTION_STARTED); |
|
| 59 | - else if (strpos($message, 'unsubscribing from')) |
|
| 60 | - $stateChange->setState(StateChange::STATE_SUBSCRIPTION_STOPPED); |
|
| 58 | + if (strpos($message, 'subscribing on channel') !== false) { |
|
| 59 | + $stateChange->setState(StateChange::STATE_SUBSCRIPTION_STARTED); |
|
| 60 | + } else if (strpos($message, 'unsubscribing from')) { |
|
| 61 | + $stateChange->setState(StateChange::STATE_SUBSCRIPTION_STOPPED); |
|
| 62 | + } |
|
| 61 | 63 | |
| 62 | 64 | return $stateChange; |
| 63 | 65 | } |