|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Jalle19\StatusManager\Subscription; |
|
4
|
|
|
|
|
5
|
|
|
use Jalle19\tvheadend\model\comet\LogMessageNotification; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Helper for parsing subscription state changes from raw log messages |
|
9
|
|
|
* |
|
10
|
|
|
* @package Jalle19\StatusManager\Subscription |
|
11
|
|
|
* @copyright Copyright © Sam Stenvall 2016- |
|
12
|
|
|
* @license https://www.gnu.org/licenses/gpl.html The GNU General Public License v2.0 |
|
13
|
|
|
*/ |
|
14
|
|
|
class StateChangeParser |
|
15
|
|
|
{ |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Parses the specified log message notifications into state change objects |
|
19
|
|
|
* |
|
20
|
|
|
* @param LogMessageNotification[] $logMessages |
|
21
|
|
|
* |
|
22
|
|
|
* @return StateChange[] eventual state changes parsed |
|
23
|
|
|
*/ |
|
24
|
2 |
|
public static function parseStateChanges(array $logMessages) |
|
25
|
|
|
{ |
|
26
|
2 |
|
$stateChanges = []; |
|
27
|
|
|
|
|
28
|
2 |
|
foreach ($logMessages as $logMessage) |
|
29
|
|
|
{ |
|
30
|
2 |
|
$stateChange = self::parseMessage($logMessage->logtxt); |
|
31
|
|
|
|
|
32
|
2 |
|
if ($stateChange !== null) |
|
33
|
2 |
|
$stateChanges[] = $stateChange; |
|
34
|
2 |
|
} |
|
35
|
|
|
|
|
36
|
2 |
|
return $stateChanges; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Attempts to parse the specified log message into a state change object |
|
42
|
|
|
* |
|
43
|
|
|
* @param string $message |
|
44
|
|
|
* |
|
45
|
|
|
* @return StateChange|null |
|
46
|
|
|
*/ |
|
47
|
2 |
|
private static function parseMessage($message) |
|
48
|
|
|
{ |
|
49
|
2 |
|
$messageParts = explode(' ', $message); |
|
50
|
|
|
|
|
51
|
|
|
// Check if we're dealing with subscription notifications |
|
52
|
2 |
|
if (strpos($message, 'subscription') !== false) |
|
53
|
2 |
|
{ |
|
54
|
2 |
|
$stateChange = new StateChange(self::getSubscriptionId($messageParts[3])); |
|
55
|
|
|
|
|
56
|
|
|
// Check the state |
|
57
|
2 |
|
if (strpos($message, 'subscribing on channel') !== false) |
|
58
|
2 |
|
$stateChange->setState(StateChange::STATE_SUBSCRIPTION_STARTED); |
|
59
|
1 |
|
elseif (strpos($message, 'unsubscribing from')) |
|
60
|
1 |
|
$stateChange->setState(StateChange::STATE_SUBSCRIPTION_STOPPED); |
|
61
|
|
|
|
|
62
|
2 |
|
return $stateChange; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
return null; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Parses the specified partial log message into a decimal subscription ID |
|
71
|
|
|
* |
|
72
|
|
|
* @param string $messagePart |
|
73
|
|
|
* |
|
74
|
|
|
* @return int |
|
75
|
|
|
*/ |
|
76
|
2 |
|
private static function getSubscriptionId($messagePart) |
|
77
|
|
|
{ |
|
78
|
2 |
|
return hexdec(substr($messagePart, 0, strlen($messagePart) - 1)); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
} |
|
82
|
|
|
|