1 | <?php |
||
14 | class Ratings |
||
15 | { |
||
16 | const MINIMUM_RATING = 1; |
||
17 | const MAXIMUM_RATING = 5; |
||
18 | |||
19 | /** @var ApiClientInterface */ |
||
20 | private $client; |
||
21 | |||
22 | /** @var Video [] */ |
||
23 | private $videos = []; |
||
24 | |||
25 | /** @var int */ |
||
26 | private $vmId; |
||
27 | |||
28 | /** @var string */ |
||
29 | private $metadataFieldAverage; |
||
30 | |||
31 | /** @var string */ |
||
32 | private $metadataFieldCount; |
||
33 | |||
34 | public function __construct( |
||
45 | |||
46 | /** |
||
47 | * Increases the count of all ratings by one and calculates a new average rating value. |
||
48 | * |
||
49 | * @param string $videoId |
||
50 | * @param int $rating |
||
51 | * |
||
52 | * @throws \InvalidArgumentException |
||
53 | */ |
||
54 | public function addRating($videoId, $rating) |
||
68 | |||
69 | /** |
||
70 | * Modifies the average rating value. Count of all ratings stays the same (will not be increased). |
||
71 | * The use case of this function is, when someone wants to change its rating (video was already rated by that person). |
||
72 | * |
||
73 | * @param string $videoId |
||
74 | * @param int $rating |
||
75 | * @param int $oldRating |
||
76 | * |
||
77 | * @throws \InvalidArgumentException |
||
78 | */ |
||
79 | public function modifyRating($videoId, $rating, $oldRating) |
||
91 | |||
92 | /** |
||
93 | * Returns the average rating value from the custom meta data fields from a given video. |
||
94 | * |
||
95 | * @param string $videoId |
||
96 | * |
||
97 | * @return float|int |
||
98 | */ |
||
99 | public function getRatingAverage($videoId) |
||
103 | |||
104 | /** |
||
105 | * Returns the count of all ratings from the custom meta data fields from a given video. |
||
106 | * |
||
107 | * @param string $videoId |
||
108 | * |
||
109 | * @return float|int |
||
110 | */ |
||
111 | private function getRatingCount($videoId) |
||
115 | |||
116 | /** |
||
117 | * Returns a meta data field of a video always as a number. |
||
118 | * |
||
119 | * @param $videoId |
||
120 | * @param $customMetaDataField |
||
121 | * |
||
122 | * @return float|int |
||
123 | */ |
||
124 | private function getCustomMetaDataField($videoId, $customMetaDataField) |
||
132 | |||
133 | /** |
||
134 | * Stores the custom meta data fields with the api client. |
||
135 | * |
||
136 | * @param array $customMetaData |
||
137 | * @param string $videoId |
||
138 | */ |
||
139 | private function storeCustomMetaData($customMetaData, $videoId) |
||
151 | |||
152 | /** |
||
153 | * Fetches and returns video from api client and stores it locally. |
||
154 | * This way api client will be requested only once for every video. |
||
155 | * |
||
156 | * @param string $videoId |
||
157 | * |
||
158 | * @return Video |
||
159 | */ |
||
160 | private function getVideo($videoId) |
||
170 | |||
171 | /** |
||
172 | * Returns custom meta data fields which are related to rating. |
||
173 | * |
||
174 | * @param array $customMetaData |
||
175 | * |
||
176 | * @return array |
||
177 | */ |
||
178 | private function filterCustomMetaData($customMetaData) |
||
188 | |||
189 | /** |
||
190 | * Checks the rating value if it is in range from 1 to 5. |
||
191 | * Throws an exception if not. |
||
192 | * |
||
193 | * @param int $rating |
||
194 | * |
||
195 | * @throws \InvalidArgumentException |
||
196 | */ |
||
197 | private function validateRating($rating) |
||
203 | } |
||
204 |