1 | <?php |
||
14 | class Ratings |
||
15 | { |
||
16 | /** @var ApiClientInterface */ |
||
17 | private $client; |
||
18 | |||
19 | /** @var Video [] */ |
||
20 | private $videos = []; |
||
21 | |||
22 | /** @var int */ |
||
23 | private $vmId; |
||
24 | |||
25 | /** @var string */ |
||
26 | private $metadataFieldAverage; |
||
27 | |||
28 | /** @var string */ |
||
29 | private $metadataFieldCount; |
||
30 | |||
31 | public function __construct( |
||
32 | ApiClientInterface $client, |
||
33 | $vmId, |
||
34 | $metadataFieldAverage, |
||
35 | $metadataFieldCount |
||
36 | ) { |
||
37 | $this->client = $client; |
||
38 | $this->metadataFieldAverage = $metadataFieldAverage; |
||
39 | $this->metadataFieldCount = $metadataFieldCount; |
||
40 | $this->vmId = $vmId; |
||
41 | } |
||
42 | |||
43 | /** |
||
44 | * Increases the count of all ratings by one and calculates a new average rating value. |
||
45 | * |
||
46 | * @param string $videoId |
||
47 | * @param int $rating |
||
48 | */ |
||
49 | public function addRating($videoId, $rating) |
||
50 | { |
||
51 | $customMetaData = $this->getVideo($videoId)->getCustomMetadata(); |
||
52 | |||
53 | $average = $this->getRatingAverage($videoId); |
||
54 | $count = $this->getRatingCount($videoId); |
||
55 | $newCount = $count + 1; |
||
56 | |||
57 | $customMetaData[$this->metadataFieldCount] = $newCount; |
||
58 | $customMetaData[$this->metadataFieldAverage] = (($average * $count) + $rating) / $newCount; |
||
59 | |||
60 | $this->storeCustomMetaData($customMetaData, $videoId); |
||
61 | } |
||
62 | |||
63 | /** |
||
64 | * Modifies the average rating value. Count of all ratings stays the same (will not be increased). |
||
65 | * The use case of this function is, when someone wants to change its rating (video was already rated by that person). |
||
66 | * |
||
67 | * @param string $videoId |
||
68 | * @param int $rating |
||
69 | * @param int $oldRating |
||
70 | */ |
||
71 | public function modifyRating($videoId, $rating, $oldRating) |
||
72 | { |
||
73 | $customMetaData = $this->getVideo($videoId)->getCustomMetadata(); |
||
74 | |||
75 | $average = $this->getRatingAverage($videoId); |
||
76 | $count = $this->getRatingCount($videoId); |
||
77 | |||
78 | $customMetaData[$this->metadataFieldAverage] = (($average * $count) - $oldRating + $rating) / $count; |
||
79 | |||
80 | $this->storeCustomMetaData($customMetaData, $videoId); |
||
81 | } |
||
82 | |||
83 | /** |
||
84 | * Returns the average rating value from the custom meta data fields from a given video. |
||
85 | * |
||
86 | * @param string $videoId |
||
87 | * |
||
88 | * @return int |
||
89 | */ |
||
90 | private function getRatingAverage($videoId) |
||
91 | { |
||
92 | return $this->getCustomMetaDataField($videoId, $this->metadataFieldAverage); |
||
93 | } |
||
94 | |||
95 | /** |
||
96 | * Returns the count of all ratings from the custom meta data fields from a given video. |
||
97 | * |
||
98 | * @param string $videoId |
||
99 | * |
||
100 | * @return int |
||
101 | */ |
||
102 | private function getRatingCount($videoId) |
||
103 | { |
||
104 | return $this->getCustomMetaDataField($videoId, $this->metadataFieldCount); |
||
105 | } |
||
106 | |||
107 | /** |
||
108 | * Returns a meta data field of a video always as a number. |
||
109 | * |
||
110 | * @param $videoId |
||
111 | * @param $customMetaDataField |
||
112 | * |
||
113 | * @return float|int |
||
114 | */ |
||
115 | private function getCustomMetaDataField($videoId, $customMetaDataField) |
||
123 | |||
124 | /** |
||
125 | * Stores the custom meta data fields with the api client. |
||
126 | * |
||
127 | * @param array $customMetaData |
||
128 | * @param string $videoId |
||
129 | */ |
||
130 | private function storeCustomMetaData($customMetaData, $videoId) |
||
137 | |||
138 | /** |
||
139 | * Fetches and returns video from api client and stores it locally. |
||
140 | * This way api client will be requested only once for every video. |
||
141 | * |
||
142 | * @param string $videoId |
||
143 | * |
||
144 | * @return Video |
||
145 | */ |
||
146 | private function getVideo($videoId) |
||
156 | } |
||
157 |