Passed
Pull Request — 1 (#4)
by
unknown
10:02
created

CommentExtension::updateController()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 2
rs 10
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
use ilateral\SilverStripe\Reviews\Control\ReviewsController;
9
10
class CommentExtension extends DataExtension
11
{
12
    private static $db = [
0 ignored issues
show
introduced by
The private property $db is not used, and could be removed.
Loading history...
13
        'Rating' => 'Int'
14
    ];
15
16
    private static $casting = [
0 ignored issues
show
introduced by
The private property $casting is not used, and could be removed.
Loading history...
17
        'MaxRating' => 'Int',
18
        'RatingStars' => 'HTMLText',
19
        'ExcessStars' => 'HTMLText'
20
    ];
21
22
    private static $summary_fields = [
0 ignored issues
show
introduced by
The private property $summary_fields is not used, and could be removed.
Loading history...
23
        'Rating'
24
    ];
25
26
    public function getMaxRating()
27
    {
28
        return $this->getOwner()->Parent()->getCommentsOption('max_rating');
29
    }
30
31
    /**
32
     * Get the rating as HTML Star characters
33
     * (one star per increment of rating).
34
     * 
35
     * @return string
36
     */
37
    public function getRatingStars()
38
    {
39
        return ReviewHelper::getStarsFromValues(
40
            $this->getOwner()->Parent()->getCommentsOption('min_rating'),
41
            round($this->getOwner()->Rating)
0 ignored issues
show
Bug introduced by
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

41
            /** @scrutinizer ignore-type */ round($this->getOwner()->Rating)
Loading history...
42
        );
43
    }
44
45
    /**
46
     * Get the excess rating as HTML Star characters
47
     * (one star per increment of rating).
48
     * 
49
     * @return string
50
     */
51
    public function getExcessStars()
52
    {
53
        $max = $this->getOwner()->Parent()->getCommentsOption("max_rating");
54
        $rating = $this->getOwner()->Rating;
55
        $excess = $max - round($rating);
56
57
        return ReviewHelper::getStarsFromValues(
58
            $this->getOwner()->Parent()->getCommentsOption("min_rating"),
59
            $excess,
0 ignored issues
show
Bug introduced by
$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

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