Completed
Push — master ( 4ceab2...5384e5 )
by Sam
03:45
created

SubscriptionQuery::filterByTimeFrame()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 11
ccs 8
cts 8
cp 1
rs 9.4285
cc 2
eloc 5
nc 2
nop 1
crap 2
1
<?php
2
3
namespace Jalle19\StatusManager\Database;
4
5
use Jalle19\StatusManager\Database\Base\SubscriptionQuery as BaseSubscriptionQuery;
6
use Jalle19\StatusManager\TimeFrame;
7
use Jalle19\tvheadend\model\SubscriptionStatus;
8
use Propel\Runtime\ActiveQuery\Criteria;
9
10
/**
11
 * @package   Jalle19\StatusManager\Database
12
 * @copyright Copyright &copy; Sam Stenvall 2015-
13
 * @license   https://www.gnu.org/licenses/gpl.html The GNU General Public License v2.0
14
 */
15
class SubscriptionQuery extends BaseSubscriptionQuery
0 ignored issues
show
Bug introduced by
There is one abstract method limit in this class; you could implement it, or declare this class as abstract.
Loading history...
16
{
17
18
	use LimitTrait;
19
20
21
	/**
22
	 * @param Instance           $instance
23
	 * @param User|null          $user
24
	 * @param Channel            $channel
25
	 * @param SubscriptionStatus $subscription
26
	 *
27
	 * @return bool
28
	 * @throws \Propel\Runtime\Exception\PropelException
29
	 */
30
	public function hasSubscription(
31
		Instance $instance,
32
		$user,
33
		Channel $channel,
34
		SubscriptionStatus $subscription
35
	) {
36
		// Not all subscriptions are tied to a user
37
		$userId = $user !== null ? $user->getId() : null;
38
39
		return $this->filterByInstance($instance)->filterByUserId($userId)
40
		            ->filterByChannel($channel)
41
		            ->filterBySubscriptionId($subscription->id)->filterByStarted($subscription->start)
42
		            ->findOne() !== null;
43
	}
44
45
46
	/**
47
	 * @param TimeFrame $timeFrame
48
	 *
49
	 * @return SubscriptionQuery
50
	 */
51 4
	public function filterByTimeFrame($timeFrame)
52
	{
53 4
		if ($timeFrame->getType() !== TimeFrame::TIME_FRAME_ALL_TIME)
54 4
		{
55 3
			$this->filterByStopped([
56 3
				'min' => $timeFrame->getTimestamp(),
57 3
			]);
58 3
		}
59
60 4
		return $this;
61
	}
62
63
64
	/**
65
	 * @param string $instanceName
66
	 * @param int    $subscriptionId
67
	 *
68
	 * @return Subscription
69
	 */
70
	public function getNewestMatching($instanceName, $subscriptionId)
71
	{
72
		return $this->filterByInstanceName($instanceName)
73
		            ->filterBySubscriptionId($subscriptionId)
74
		            ->addDescendingOrderByColumn('started')->findOne();
75
	}
76
77
78
	/**
79
	 * @param Instance $instance
80
	 * @param User     $user
81
	 *
82
	 * @return SubscriptionQuery
83
	 * @throws \Propel\Runtime\Exception\PropelException
84
	 */
85
	public function getPopularChannelsQuery(Instance $instance, User $user)
86
	{
87
		$this->withColumn('channel.name', 'channelName');
88
		$this->withColumn('user.name', 'userName');
89
		$this->withColumn('SUM((julianday(subscription.stopped) - julianday(subscription.started)) * 86400)',
90
			'totalTimeSeconds');
91
		$this->select(['channelName', 'userName', 'totalTimeSeconds']);
92
93
		// Join the user table with useUserQuery() so the same method can be used later
94
		$this->joinChannel('channel');
95
		$this->useUserQuery()->endUse();
96
		$this->groupBy('channelName');
97
		$this->orderBy('totalTimeSeconds', Criteria::DESC);
98
99
		// Apply filtering
100
		$this->filterByStopped(null, Criteria::NOT_EQUAL);
101
		$this->filterByInstance($instance);
102
103
		if ($user !== null)
104
			$this->filterByUser($user);
105
106
		return $this;
107
	}
108
109
}
110