Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
17 / 17 |
|
100.00% |
7 / 7 |
CRAP | |
100.00% |
1 / 1 |
| Rating | |
100.00% |
17 / 17 |
|
100.00% |
7 / 7 |
8 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| get | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
2 | |||
| check | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getNumberOfVotings | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| create | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| update | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| deleteAll | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * The main Rating class. |
| 5 | * |
| 6 | * This Source Code Form is subject to the terms of the Mozilla Public License, |
| 7 | * v. 2.0. If a copy of the MPL was not distributed with this file, You can |
| 8 | * obtain one at https://mozilla.org/MPL/2.0/. |
| 9 | * |
| 10 | * @package phpMyFAQ |
| 11 | * @author Thorsten Rinne <thorsten@phpmyfaq.de> |
| 12 | * @copyright 2007-2026 phpMyFAQ Team |
| 13 | * @license https://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0 |
| 14 | * @link https://www.phpmyfaq.de |
| 15 | * @since 2007-03-31 |
| 16 | */ |
| 17 | |
| 18 | declare(strict_types=1); |
| 19 | |
| 20 | namespace phpMyFAQ; |
| 21 | |
| 22 | use phpMyFAQ\Entity\Vote; |
| 23 | use phpMyFAQ\Language\Plurals; |
| 24 | use phpMyFAQ\Search\Rating\RatingRepository; |
| 25 | |
| 26 | /** |
| 27 | * Class Rating |
| 28 | * |
| 29 | * @package phpMyFAQ |
| 30 | */ |
| 31 | readonly class Rating |
| 32 | { |
| 33 | /** |
| 34 | * Plural form support. |
| 35 | */ |
| 36 | private Plurals $plurals; |
| 37 | |
| 38 | /** |
| 39 | * Rating repository. |
| 40 | */ |
| 41 | private RatingRepository $ratingRepository; |
| 42 | |
| 43 | /** |
| 44 | * Constructor. |
| 45 | */ |
| 46 | public function __construct(Configuration $configuration) |
| 47 | { |
| 48 | $this->plurals = new Plurals(); |
| 49 | $this->ratingRepository = new RatingRepository($configuration); |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Calculates the rating of the user voting. |
| 54 | */ |
| 55 | public function get(int $id): string |
| 56 | { |
| 57 | $row = $this->ratingRepository->fetchByRecordId($id); |
| 58 | |
| 59 | if ($row !== null) { |
| 60 | return sprintf( |
| 61 | ' <span data-rating="%s">%s</span> (' |
| 62 | . $this->plurals->get(key: 'plmsgVotes', number: (int) $row->usr) |
| 63 | . ')', |
| 64 | round(num: (int) $row->voting, precision: 2), |
| 65 | round(num: (int) $row->voting, precision: 2), |
| 66 | ); |
| 67 | } |
| 68 | |
| 69 | return ' <span data-rating="0">0</span> (' . $this->plurals->get(key: 'plmsgVotes', number: 0) . ')'; |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Reload locking for user voting. |
| 74 | * |
| 75 | * @param int $id FAQ record id |
| 76 | * @param string $ip IP |
| 77 | */ |
| 78 | public function check(int $id, string $ip): bool |
| 79 | { |
| 80 | return $this->ratingRepository->isVoteAllowed($id, $ip); |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Returns the number of users from the table "faqvotings". |
| 85 | */ |
| 86 | public function getNumberOfVotings(int $recordId): int |
| 87 | { |
| 88 | return $this->ratingRepository->getNumberOfVotings($recordId); |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Adds a new voting record. |
| 93 | */ |
| 94 | public function create(Vote $vote): bool |
| 95 | { |
| 96 | return $this->ratingRepository->create($vote); |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Updates an existing voting record. |
| 101 | */ |
| 102 | public function update(Vote $vote): bool |
| 103 | { |
| 104 | return $this->ratingRepository->update($vote); |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Deletes all votes. |
| 109 | */ |
| 110 | public function deleteAll(): bool |
| 111 | { |
| 112 | return $this->ratingRepository->deleteAll(); |
| 113 | } |
| 114 | } |