Completed
Push — master ( 8bcd18...3260d0 )
by Sam
02:25
created

StatisticsManager::getPopularChannels()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 22
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 2
Metric Value
c 4
b 0
f 2
dl 0
loc 22
rs 9.2
cc 3
eloc 10
nc 4
nop 4
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\PopularChannelsRequest;
11
use Jalle19\StatusManager\Message\Request\StatisticsRequest;
12
use Jalle19\StatusManager\Message\Response\PopularChannelsResponse;
13
14
/**
15
 * Class StatisticsManager
16
 * @package   Jalle19\StatusManager\Manager
17
 * @copyright Copyright &copy; Sam Stenvall 2016-
18
 * @license   https://www.gnu.org/licenses/gpl.html The GNU General Public License v2.0
19
 */
20
class StatisticsManager extends AbstractManager implements HandlerInterface
21
{
22
23
	/**
24
	 * @inheritdoc
25
	 */
26
	public function handleMessage(AbstractMessage $message)
27
	{
28
		switch ($message->getType())
29
		{
30
			case AbstractMessage::TYPE_POPULAR_CHANNELS_REQUEST:
31
				/* @var PopularChannelsRequest $message */
32
				return new PopularChannelsResponse($message, $this->getPopularChannels(
33
					$message->getInstanceName(),
34
					$message->getUserName(),
35
					$message->getLimit(),
36
					$message->getTimeInterval()));
37
		}
38
39
		return false;
40
	}
41
42
43
	/**
44
	 * @param string      $instanceName
45
	 * @param string|null $userName
46
	 * @param int|null    $limit
47
	 * @param string      $timeInterval
48
	 *
49
	 * @return array the popular channels
50
	 */
51
	private function getPopularChannels($instanceName, $userName, $limit, $timeInterval)
52
	{
53
		// Find the instance and the user
54
		$instance = InstanceQuery::create()->findOneByName($instanceName);
55
		$user     = UserQuery::create()->findOneByName($userName);
56
57
		// Find the subscriptions
58
		$query = SubscriptionQuery::createPopularChannelsQuery($instance, $user);
59
60
		// Apply additional filters not done by the query
61
		if ($limit !== null)
62
			$query->limit($limit);
63
64
		if ($timeInterval !== StatisticsRequest::TIME_INTERVAL_ALL_TIME)
65
		{
66
			$query->filterByStopped([
67
				'min' => $this->getTimeIntervalTimestamp($timeInterval),
68
			]);
69
		}
70
71
		return $query->find()->getData();
72
	}
73
74
75
	/**
76
	 * @param string $timeInterval
77
	 *
78
	 * @return int
79
	 */
80
	private function getTimeIntervalTimestamp($timeInterval)
81
	{
82
		$dateTime = new \DateTime();
83
84
		switch ($timeInterval)
85
		{
86
			case StatisticsRequest::TIME_INTERVAL_LAST_MONTH:
87
				$dateTime = $dateTime->modify('-1 month');
88
		}
89
90
		return $dateTime->getTimestamp();
91
	}
92
93
}
94