Rating   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 6
eloc 19
dl 0
loc 45
c 0
b 0
f 0
ccs 0
cts 15
cp 0
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 18 6
1
<?php
2
/**
3
 * YAWIK
4
 *
5
 * @filesource
6
 * @copyright (c) 2013 - 2016 Cross Solution (http://cross-solution.de)
7
 * @license   MIT
8
 */
9
10
/** Core view helpers */
11
namespace Core\View\Helper;
12
13
use Laminas\View\Helper\AbstractHelper;
14
use Core\Entity\RatingInterface;
15
16
/**
17
 * Renders a visual representation of a rating value.
18
 *
19
 * <code>
20
 *
21
 *      // Renders a compact rating bar representation:
22
 *      echo $this->rating(3);
23
 *
24
 *      // Renders a wide rating bar representation:
25
 *      echo $this->rating(4, 'wider');
26
 *
27
 *      // Pass an rating interface
28
 *      $rating = $entity->getRating();
29
 *      echo $this->rating($rating);
30
 * </code>
31
 *
32
 * @see \Core\Entity\Rating
33
 * @author Mathias Gelhausen <[email protected]>
34
 */
35
class Rating extends AbstractHelper
36
{
37
38
    /**
39
     * Maps rating values to text.
40
     *
41
     * @var array
42
     */
43
    protected static $ratingValueMap = array(
44
        RatingInterface::RATING_NONE      => 'Not Rated',
45
        RatingInterface::RATING_POOR      => 'Poor',
46
        RatingInterface::RATING_BAD       => 'Bad',
47
        RatingInterface::RATING_AVERAGE   => 'Average',
48
        RatingInterface::RATING_GOOD      => 'Good',
49
        RatingInterface::RATING_EXCELLENT => 'Excellent',
50
    );
51
    
52
    
53
    /**
54
     * generates a rating bar from a rating value
55
     *
56
     * @param int|RatingInterface $rating
57
     * @param string $mode Rendering mode:
58
     *                     - "compact": renders a densed rating presentation.
59
     *                     - ANY STRING: renders a wide rating presentation.
60
     * @return string
61
     */
62
    public function __invoke($rating, $mode = 'compact')
63
    {
64
        if ($rating instanceof RatingInterface) {
65
            $rating = $rating->getAverage();
66
        }
67
        
68
        $output = '<div class="br-widget br-readonly '
69
                . ('compact' == $mode ? ' br-compact' : '')
70
                . '" title="' . $this->getView()->translate(self::$ratingValueMap[$rating]) . '">';
0 ignored issues
show
Bug introduced by
The method translate() does not exist on Laminas\View\Renderer\RendererInterface. It seems like you code against a sub-type of Laminas\View\Renderer\RendererInterface such as Laminas\View\Renderer\PhpRenderer. ( Ignorable by Annotation )

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

70
                . '" title="' . $this->getView()->/** @scrutinizer ignore-call */ translate(self::$ratingValueMap[$rating]) . '">';
Loading history...
71
        for ($i=1; $i<6; $i++) {
72
            $class = $i <= $rating ? 'br-selected' : '';
73
            $class .= $i == $rating ? ' br-current' : '';
74
            
75
            $output .= '<a class="' . $class . '"></a>';
76
        }
77
        $output .= '</div>';
78
        
79
        return $output;
80
    }
81
}
82