| 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\User; |
||||||||||||||
| 8 | use Jalle19\StatusManager\Database\UserQuery; |
||||||||||||||
| 9 | use Jalle19\StatusManager\Message\AbstractMessage; |
||||||||||||||
| 10 | use Jalle19\StatusManager\Message\Handler\HandlerInterface; |
||||||||||||||
| 11 | use Jalle19\StatusManager\Message\Request\InstancesRequest; |
||||||||||||||
| 12 | use Jalle19\StatusManager\Message\Request\MostActiveWatchersRequest; |
||||||||||||||
| 13 | use Jalle19\StatusManager\Message\Request\PopularChannelsRequest; |
||||||||||||||
| 14 | use Jalle19\StatusManager\Message\Request\UsersRequest; |
||||||||||||||
| 15 | use Jalle19\StatusManager\Message\Response\InstancesResponse; |
||||||||||||||
| 16 | use Jalle19\StatusManager\Message\Response\MostActiveWatchersResponse; |
||||||||||||||
| 17 | use Jalle19\StatusManager\Message\Response\PopularChannelsResponse; |
||||||||||||||
| 18 | use Jalle19\StatusManager\Message\Response\UsersResponse; |
||||||||||||||
| 19 | use Jalle19\StatusManager\TimeFrame; |
||||||||||||||
| 20 | use Jalle19\tvheadend\exception\RequestFailedException; |
||||||||||||||
| 21 | use Propel\Runtime\Exception\PropelException; |
||||||||||||||
| 22 | use Ratchet\ConnectionInterface; |
||||||||||||||
| 23 | |||||||||||||||
| 24 | /** |
||||||||||||||
| 25 | * Class StatisticsManager |
||||||||||||||
| 26 | * @package Jalle19\StatusManager\Manager |
||||||||||||||
| 27 | * @copyright Copyright © Sam Stenvall 2016- |
||||||||||||||
| 28 | * @license https://www.gnu.org/licenses/gpl.html The GNU General Public License v2.0 |
||||||||||||||
| 29 | */ |
||||||||||||||
| 30 | class StatisticsManager extends AbstractManager implements HandlerInterface |
||||||||||||||
| 31 | { |
||||||||||||||
| 32 | |||||||||||||||
| 33 | /** |
||||||||||||||
| 34 | * @inheritdoc |
||||||||||||||
| 35 | */ |
||||||||||||||
| 36 | public function handleMessage(AbstractMessage $message, ConnectionInterface $sender) |
||||||||||||||
| 37 | { |
||||||||||||||
| 38 | // Wrap database exceptions in the more generic RequestFailedException |
||||||||||||||
| 39 | try |
||||||||||||||
| 40 | { |
||||||||||||||
| 41 | switch ($message->getType()) |
||||||||||||||
| 42 | { |
||||||||||||||
| 43 | case AbstractMessage::TYPE_POPULAR_CHANNELS_REQUEST: |
||||||||||||||
| 44 | /* @var PopularChannelsRequest $message */ |
||||||||||||||
| 45 | return new PopularChannelsResponse($message, $this->getPopularChannels( |
||||||||||||||
| 46 | $message->getInstanceName(), |
||||||||||||||
| 47 | $message->getUserName(), |
||||||||||||||
| 48 | $message->getLimit(), |
||||||||||||||
| 49 | $message->getTimeFrame())); |
||||||||||||||
| 50 | case AbstractMessage::TYPE_MOST_ACTIVE_WATCHERS_REQUEST: |
||||||||||||||
| 51 | /* @var MostActiveWatchersRequest $message */ |
||||||||||||||
| 52 | return new MostActiveWatchersResponse($message, $this->getMostActiveWatchers( |
||||||||||||||
| 53 | $message->getInstanceName(), |
||||||||||||||
| 54 | $message->getLimit(), |
||||||||||||||
| 55 | $message->getTimeFrame())); |
||||||||||||||
| 56 | case AbstractMessage::TYPE_INSTANCES_REQUEST: |
||||||||||||||
| 57 | /* @var InstancesRequest $message */ |
||||||||||||||
| 58 | return new InstancesResponse($this->configuration->getInstances()); |
||||||||||||||
| 59 | case AbstractMessage::TYPE_USERS_REQUEST: |
||||||||||||||
| 60 | /* @var UsersRequest $message */ |
||||||||||||||
| 61 | return new UsersResponse($this->getUsers($message->getInstanceName())); |
||||||||||||||
| 62 | } |
||||||||||||||
| 63 | } |
||||||||||||||
| 64 | catch (PropelException $e) |
||||||||||||||
| 65 | { |
||||||||||||||
| 66 | throw new RequestFailedException('A database error occured: ' . $e->getMessage()); |
||||||||||||||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||||||||||||
| 67 | } |
||||||||||||||
| 68 | |||||||||||||||
| 69 | return false; |
||||||||||||||
| 70 | } |
||||||||||||||
| 71 | |||||||||||||||
| 72 | |||||||||||||||
| 73 | /** |
||||||||||||||
| 74 | * @param string $instanceName |
||||||||||||||
| 75 | * @param string|null $userName |
||||||||||||||
| 76 | * @param int|null $limit |
||||||||||||||
| 77 | * @param TimeFrame $timeFrame |
||||||||||||||
| 78 | * |
||||||||||||||
| 79 | * @return array the popular channels |
||||||||||||||
| 80 | */ |
||||||||||||||
| 81 | private function getPopularChannels($instanceName, $userName, $limit, $timeFrame) |
||||||||||||||
| 82 | { |
||||||||||||||
| 83 | // Find the instance and the user |
||||||||||||||
| 84 | $instance = InstanceQuery::create()->findOneByName($instanceName); |
||||||||||||||
| 85 | $user = UserQuery::create()->findOneByName($userName); |
||||||||||||||
| 86 | $query = SubscriptionQuery::create()->getPopularChannelsQuery($instance, $user); |
||||||||||||||
|
0 ignored issues
–
show
It seems like
$instance can also be of type null; however, parameter $instance of Jalle19\StatusManager\Da...tPopularChannelsQuery() does only seem to accept Jalle19\StatusManager\Database\Instance, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
It seems like
$user can also be of type null; however, parameter $user of Jalle19\StatusManager\Da...tPopularChannelsQuery() does only seem to accept Jalle19\StatusManager\Database\User, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||||||||||
| 87 | $ignoredUsers = $this->configuration->getInstanceByName($instanceName)->getIgnoredUsers(); |
||||||||||||||
| 88 | |||||||||||||||
| 89 | $query = $query->filterByLimit($limit); |
||||||||||||||
| 90 | $query = $query->filterByTimeFrame($timeFrame); |
||||||||||||||
| 91 | $query = $query->useUserQuery()->filterIgnoredUsers($ignoredUsers)->endUse(); |
||||||||||||||
| 92 | |||||||||||||||
| 93 | return $query->find()->getData(); |
||||||||||||||
| 94 | } |
||||||||||||||
| 95 | |||||||||||||||
| 96 | |||||||||||||||
| 97 | /** |
||||||||||||||
| 98 | * @param string $instanceName |
||||||||||||||
| 99 | * @param int $limit |
||||||||||||||
| 100 | * @param TimeFrame $timeFrame |
||||||||||||||
| 101 | * |
||||||||||||||
| 102 | * @return array |
||||||||||||||
| 103 | */ |
||||||||||||||
| 104 | private function getMostActiveWatchers($instanceName, $limit, $timeFrame) |
||||||||||||||
| 105 | { |
||||||||||||||
| 106 | $instance = InstanceQuery::create()->findOneByName($instanceName); |
||||||||||||||
| 107 | $query = UserQuery::create()->getMostActiveWatchersQuery($instance); |
||||||||||||||
| 108 | $ignoredUsers = $this->configuration->getInstanceByName($instanceName)->getIgnoredUsers(); |
||||||||||||||
| 109 | |||||||||||||||
| 110 | $query = $query->filterByLimit($limit); |
||||||||||||||
| 111 | $query = $query->useSubscriptionQuery()->filterByTimeFrame($timeFrame)->endUse(); |
||||||||||||||
| 112 | $query = $query->filterIgnoredUsers($ignoredUsers); |
||||||||||||||
|
0 ignored issues
–
show
The method
filterIgnoredUsers() does not exist on Jalle19\StatusManager\Database\InstanceQuery. Since you implemented __call, consider adding a @method annotation.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
The method
filterIgnoredUsers() does not exist on Jalle19\StatusManager\Database\ChannelQuery. Since you implemented __call, consider adding a @method annotation.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
The method
filterIgnoredUsers() does not exist on Jalle19\StatusManager\Database\InputQuery. Since you implemented __call, consider adding a @method annotation.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||||||||||
| 113 | |||||||||||||||
| 114 | return $query->find()->getData(); |
||||||||||||||
| 115 | } |
||||||||||||||
| 116 | |||||||||||||||
| 117 | |||||||||||||||
| 118 | /** |
||||||||||||||
| 119 | * Returns all users found so far for the specified instance name |
||||||||||||||
| 120 | * |
||||||||||||||
| 121 | * @param string $instanceName |
||||||||||||||
| 122 | * |
||||||||||||||
| 123 | * @return User[] |
||||||||||||||
| 124 | */ |
||||||||||||||
| 125 | private function getUsers($instanceName) |
||||||||||||||
| 126 | { |
||||||||||||||
| 127 | $ignoredUsers = $this->configuration->getInstanceByName($instanceName)->getIgnoredUsers(); |
||||||||||||||
| 128 | |||||||||||||||
| 129 | return UserQuery::create() |
||||||||||||||
| 130 | ->filterIgnoredUsers($ignoredUsers) |
||||||||||||||
| 131 | ->findByInstanceName($instanceName)->getArrayCopy(); |
||||||||||||||
| 132 | } |
||||||||||||||
| 133 | |||||||||||||||
| 134 | } |
||||||||||||||
| 135 |