1 | <?php |
||
8 | abstract class StatusMapper extends AbstractMapper |
||
9 | { |
||
10 | /** |
||
11 | * Define the status for a movie-user tuple. If an existing satus exists and |
||
12 | * is very recent, it will be updated, otherwise a new status will be created. |
||
13 | * IMPORTANT: This is the only allowed way to modify status. |
||
14 | * @param \mQueue\Model\Movie $movie |
||
15 | * @param \mQueue\Model\User $user |
||
16 | * @param int $rating @see \mQueue\Model\Status |
||
17 | * @return \mQueue\Model\Status |
||
18 | */ |
||
19 | public static function set(Movie $movie, User $user, $rating) |
||
|
|||
20 | { |
||
21 | $db = self::getDbTable()->getAdapter(); |
||
22 | $db->beginTransaction(); |
||
23 | |||
24 | // Find out if a very recent status exist to be replaced, so user can change their mind "quickly" |
||
25 | $select = self::getDbTable()->select() |
||
26 | ->where('idUser = ?', $user->id) |
||
27 | ->where('idMovie = ?', $movie->id) |
||
28 | ->where('dateUpdate > DATE_SUB(NOW(), INTERVAL 5 MINUTE)'); |
||
29 | |||
30 | $status = self::getDbTable()->fetchRow($select); |
||
31 | |||
32 | // Otherwise create a brand new one and set all existing one as "old" |
||
33 | if (!$status) { |
||
34 | $status = self::getDbTable()->createRow(); |
||
35 | $status->idUser = $user->id; |
||
36 | $status->idMovie = $movie->id; |
||
37 | $status->isLatest = true; |
||
38 | |||
39 | // Here we must set dateUpdate to itself to avoid auto-update of the timestamp field by MySql |
||
40 | $db->query('UPDATE `status` SET isLatest = 0, dateUpdate = dateUpdate WHERE idUser = ? AND idMovie = ?', [$user->id, $movie->id]); |
||
41 | } |
||
42 | |||
43 | $status->rating = $rating; |
||
44 | $status->save(); |
||
45 | |||
46 | $db->commit(); |
||
47 | |||
48 | return $status; |
||
49 | } |
||
50 | |||
51 | /** |
||
52 | * Find a status by its user and movie. If not found it will be created (but not saved). |
||
53 | * @param int $idMovie |
||
54 | * @param \mQueue\Model\User|null $user |
||
55 | * @return \mQueue\Model\Status |
||
56 | */ |
||
57 | 1 | public static function find($idMovie, User $user = null) |
|
63 | |||
64 | /** |
||
65 | * Returns an array of Status containing all statuses for specified ids |
||
66 | * (if they don't exist in database, they will be created with default values but not saved) |
||
67 | * |
||
68 | * @param array $idMovies |
||
69 | * @param \mQueue\Model\User|null $user |
||
70 | * @return array of \mQueue\Model\Status |
||
71 | */ |
||
72 | 1 | public static function findAll(array $idMovies, User $user = null) |
|
107 | |||
108 | /** |
||
109 | * Build statistic for the given user. |
||
110 | * @param \mQueue\Model\User $user |
||
111 | * @return array statistics |
||
112 | */ |
||
113 | 1 | public static function getStatistics(User $user) |
|
142 | |||
143 | /** |
||
144 | * Build statistic for the given user. |
||
145 | * @param \mQueue\Model\User $user |
||
146 | * @return array statistics |
||
147 | */ |
||
148 | public static function getGraph(User $user = null, $percent = false) |
||
211 | |||
212 | /** |
||
213 | * Returns the query to get activity for either the whole system, or a specific user, or a specific movie |
||
214 | * @param \mQueue\Model\User|\mQueue\Model\Movie|null $item |
||
215 | * @return Zend_Db_Table_Select |
||
216 | */ |
||
217 | 3 | public static function getActivityQuery($item = null) |
|
231 | } |
||
232 |
Even though PHP does not care about the name of your methods, it is generally a good practice to choose method names which can be easily understood by other human readers.