Issues (11)

src/Extensions/CommentExtension.php (5 issues)

1
<?php
2
3
namespace ilateral\SilverStripe\Reviews\Extensions;
4
5
use SilverStripe\ORM\DataExtension;
6
use ilateral\SilverStripe\Reviews\Helpers\ReviewHelper;
7
use SilverStripe\Forms\FieldList;
8
9
class CommentExtension extends DataExtension
10
{
11
    private static $db = [
0 ignored issues
show
The private property $db is not used, and could be removed.
Loading history...
12
        'Rating' => 'Int'
13
    ];
14
15
    private static $casting = [
0 ignored issues
show
The private property $casting is not used, and could be removed.
Loading history...
16
        'MaxRating' => 'Int',
17
        'RatingStars' => 'HTMLText',
18
        'ExcessStars' => 'HTMLText'
19
    ];
20
21
    private static $summary_fields = [
0 ignored issues
show
The private property $summary_fields is not used, and could be removed.
Loading history...
22
        'Rating'
23
    ];
24
25
    public function getMaxRating()
26
    {
27
        return $this->getOwner()->Parent()->getCommentsOption('max_rating');
28
    }
29
30
    /**
31
     * Get the rating as HTML Star characters
32
     * (one star per increment of rating).
33
     * 
34
     * @return string
35
     */
36
    public function getRatingStars()
37
    {
38
        return ReviewHelper::getStarsFromValues(
39
            $this->getOwner()->Parent()->getCommentsOption('min_rating'),
40
            round($this->getOwner()->Rating)
0 ignored issues
show
round($this->getOwner()->Rating) of type double is incompatible with the type integer expected by parameter $max of ilateral\SilverStripe\Re...r::getStarsFromValues(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

40
            /** @scrutinizer ignore-type */ round($this->getOwner()->Rating)
Loading history...
41
        );
42
    }
43
44
    /**
45
     * Get the excess rating as HTML Star characters
46
     * (one star per increment of rating).
47
     * 
48
     * @return string
49
     */
50
    public function getExcessStars()
51
    {
52
        $max = $this->getOwner()->Parent()->getCommentsOption("max_rating");
53
        $rating = $this->getOwner()->Rating;
54
        $excess = $max - round($rating);
55
56
        return ReviewHelper::getStarsFromValues(
57
            $this->getOwner()->Parent()->getCommentsOption("min_rating"),
58
            $excess,
0 ignored issues
show
$excess of type double is incompatible with the type integer expected by parameter $max of ilateral\SilverStripe\Re...r::getStarsFromValues(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

58
            /** @scrutinizer ignore-type */ $excess,
Loading history...
59
            $html = "&#9734;"
60
        );
61
    }
62
63
    public function updateCMSFields(FieldList $fields)
64
    {
65
        /** @var \SilverStripe\Comments\Model\Comment */
66
        $owner = $this->getOwner();
67
68
        $fields->insertBefore(
69
            'Name',
70
            $owner->dbObject('Rating')->scaffoldFormField($owner->fieldLabel('Rating'))
71
        );
72
    }
73
}