Blame view
app/Queries/VoteQuery.php
3.96 KB
e77200db5 Initial commit |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
<?php namespace FootyRoom\Queries; use FootyRoom\Queries\Vote\Vote; use FootyRoom\Support\AutoMapper; use FootyRoom\Support\MongoClient; class VoteQuery { /** * @var \FootyRoom\Support\MongoClient */ protected $mongo; /** * Constructor. * * @param \FootyRoom\Support\MongoClient $mongo */ public function __construct(MongoClient $mongo) { $this->mongo = $mongo->footyroom; } /** * Finds votes by specified tracker that have no user id. * * @param string $pollRef * @param string $tracker * @param array $fields * * @return array */ public function findByTrackerOnly($pollRef, $tracker, $fields = []): array { $cursor = $this->mongo->votes->find([ 'pollRef' => $pollRef, 'tracker' => $tracker, 'userId' => ['$exists' => false], ], ['projection' => $fields]); return iterator_to_array($cursor); } /** * Finds votes by specified user id. * * @param string $pollRef * @param int $userId * @param array $fields * * @return array */ public function findByUserId($pollRef, $userId, $fields = []): array { $cursor = $this->mongo->votes->find([ 'pollRef' => $pollRef, 'userId' => $userId ?: ['$exists' => false], ], ['projection' => $fields]); return iterator_to_array($cursor, false); } /** * Returns an aggregate of votes for specified poll. * * @param string $pollId * @param int $limit * * @return object */ public function aggregate($pollId, $limit = null) { $pipeline[] = ['$match' => ['pollRef' => $pollId]]; $pipeline[] = ['$group' => [ '_id' => ['value' => '$value', 'valueRef' => '$valueRef'], 'votes' => ['$sum' => 1], ]]; $pipeline[] = ['$project' => [ 'vote' => ['votes' => '$votes', 'value' => '$_id.value', 'valueRef' => '$_id.valueRef'], ]]; $pipeline[] = ['$group' => [ '_id' => $pollId, 'votes' => ['$addToSet' => '$vote'], 'totalVotes' => ['$sum' => '$vote.votes'], 'maxVotes' => ['$max' => '$vote.votes'], ]]; if (isset($limit)) { $pipeline[] = ['$unwind' => '$votes']; $pipeline[] = ['$sort' => ['votes.votes' => -1]]; $pipeline[] = ['$limit' => $limit]; $pipeline[] = ['$group' => [ '_id' => $pollId, 'votes' => ['$addToSet' => '$votes'], 'totalVotes' => ['$first' => '$totalVotes'], 'maxVotes' => ['$first' => '$maxVotes'], ]]; } $pipeline[] = ['$project' => [ 'pollRef' => '$_id', 'votes' => 1, 'totalVotes' => 1, 'maxVotes' => 1, '_id' => 0, ], ]; $result = $this->mongo->votes->aggregate($pipeline)->toArray(); if (!isset($result[0])) { return null; } return $result[0]; } /** * Finds votes by specified pollRef. * * @return \FootyRoom\Queries\Vote\Vote[] */ public function findByPollRef(string $pollRef): array { $cursor = $this->mongo->votes->aggregate([ ['$match' => [ 'pollRef' => $pollRef, 'userId' => [ '$exists' => true, '$ne' => null, ] ]], ['$lookup' => [ 'from' => 'users', 'localField' => 'userId', 'foreignField' => 'userId', 'as' => 'user', ]], ['$project' => [ '_id' => 0, 'value' => 1, 'username' => ['$arrayElemAt' => ['$user.username', 0]] ]], ]); foreach ($cursor as $vote) { $votes[] = AutoMapper::map($vote, Vote::class); } return $votes ?? []; } } |