Completed
Push — master ( 9804ea...45188d )
by Sam
02:27
created

StatisticsManager::getMostActiveWatchers()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 18
rs 9.4285
cc 3
eloc 10
nc 4
nop 3
1
<?php
2
3
namespace Jalle19\StatusManager\Manager;
4
5
use Jalle19\StatusManager\Database\InstanceQuery;
6
use Jalle19\StatusManager\Database\SubscriptionQuery;
7
use Jalle19\StatusManager\Database\UserQuery;
8
use Jalle19\StatusManager\Message\AbstractMessage;
9
use Jalle19\StatusManager\Message\Handler\HandlerInterface;
10
use Jalle19\StatusManager\Message\Request\MostActiveWatchersRequest;
11
use Jalle19\StatusManager\Message\Request\PopularChannelsRequest;
12
use Jalle19\StatusManager\Message\Request\StatisticsRequest;
13
use Jalle19\StatusManager\Message\Response\MostActiveWatchersResponse;
14
use Jalle19\StatusManager\Message\Response\PopularChannelsResponse;
15
use Jalle19\tvheadend\exception\RequestFailedException;
16
use Propel\Runtime\Exception\PropelException;
17
18
/**
19
 * Class StatisticsManager
20
 * @package   Jalle19\StatusManager\Manager
21
 * @copyright Copyright &copy; Sam Stenvall 2016-
22
 * @license   https://www.gnu.org/licenses/gpl.html The GNU General Public License v2.0
23
 */
24
class StatisticsManager extends AbstractManager implements HandlerInterface
25
{
26
27
	/**
28
	 * @inheritdoc
29
	 */
30
	public function handleMessage(AbstractMessage $message)
31
	{
32
		// Wrap database exceptions in the more generic RequestFailedException
33
		try
34
		{
35
			switch ($message->getType())
36
			{
37
				case AbstractMessage::TYPE_POPULAR_CHANNELS_REQUEST:
38
					/* @var PopularChannelsRequest $message */
39
					return new PopularChannelsResponse($message, $this->getPopularChannels(
40
						$message->getInstanceName(),
41
						$message->getUserName(),
42
						$message->getLimit(),
43
						$message->getTimeInterval()));
44
				case AbstractMessage::TYPE_MOST_ACTIVE_WATCHERS_REQUEST:
45
					/* @var MostActiveWatchersRequest $message */
46
					return new MostActiveWatchersResponse($message, $this->getMostActiveWatchers(
47
						$message->getInstanceName(),
48
						$message->getLimit(),
49
						$message->getTimeInterval()));
50
			}
51
		}
52
		catch (PropelException $e)
53
		{
54
			throw new RequestFailedException('A database error occured: ' . $e->getMessage());
55
		}
56
57
		return false;
58
	}
59
60
61
	/**
62
	 * @param string      $instanceName
63
	 * @param string|null $userName
64
	 * @param int|null    $limit
65
	 * @param string      $timeInterval
66
	 *
67
	 * @return array the popular channels
68
	 */
69
	private function getPopularChannels($instanceName, $userName, $limit, $timeInterval)
70
	{
71
		// Find the instance and the user
72
		$instance = InstanceQuery::create()->findOneByName($instanceName);
73
		$user     = UserQuery::create()->findOneByName($userName);
74
75
		// Find the subscriptions
76
		$query = SubscriptionQuery::create()->getPopularChannelsQuery($instance, $user);
77
78
		// Apply additional filters not done by the query
79
		if ($limit !== null)
80
			$query->limit($limit);
81
82
		if ($timeInterval !== StatisticsRequest::TIME_INTERVAL_ALL_TIME)
83
		{
84
			$query->filterByStopped([
85
				'min' => $this->getTimeIntervalTimestamp($timeInterval),
86
			]);
87
		}
88
89
		return $query->find()->getData();
90
	}
91
92
93
	/**
94
	 * @param string $instanceName
95
	 * @param int    $limit
96
	 * @param string $timeInterval
97
	 *
98
	 * @return array
99
	 */
100
	private function getMostActiveWatchers($instanceName, $limit, $timeInterval)
101
	{
102
		$instance = InstanceQuery::create()->findOneByName($instanceName);
103
		$query    = UserQuery::create()->getMostActiveWatchersQuery($instance);
104
105
		// Apply additional filters not done by the query
106
		if ($limit !== null)
107
			$query->limit($limit);
108
109
		if ($timeInterval !== StatisticsRequest::TIME_INTERVAL_ALL_TIME)
110
		{
111
			$query->useSubscriptionQuery()->filterByStopped([
112
				'min' => $this->getTimeIntervalTimestamp($timeInterval),
113
			])->endUse();
114
		}
115
116
		return $query->find()->getData();
117
	}
118
119
120
	/**
121
	 * @param string $timeInterval
122
	 *
123
	 * @return int
124
	 */
125
	private function getTimeIntervalTimestamp($timeInterval)
126
	{
127
		$dateTime = new \DateTime();
128
129
		switch ($timeInterval)
130
		{
131
			case StatisticsRequest::TIME_INTERVAL_LAST_MONTH:
132
				$dateTime = $dateTime->modify('-1 month');
133
		}
134
135
		return $dateTime->getTimestamp();
136
	}
137
138
}
139