Lines 100.00% 17 / 17
Methods 100.00% 7 / 7
Classes 100.00% 1 / 1
Covered by tests of size
Name Lines Methods CRAP
 __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
31readonly 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}