Completed
Push — master ( c68689...d67f41 )
by Adam
06:12
created

ReviewExtension::getUnlikesCount()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 1
1
<?php
2
/*
3
 * WellCommerce Open-Source E-Commerce Platform
4
 *
5
 * This file is part of the WellCommerce package.
6
 *
7
 * (c) Adam Piotrowski <[email protected]>
8
 *
9
 * For the full copyright and license information,
10
 * please view the LICENSE file that was distributed with this source code.
11
 */
12
namespace WellCommerce\Bundle\ReviewBundle\Twig\Extension;
13
14
use Doctrine\Common\Collections\Collection;
15
use WellCommerce\Bundle\CoreBundle\Doctrine\Repository\RepositoryInterface;
16
use WellCommerce\Bundle\ReviewBundle\Entity\Review;
17
18
/**
19
 * Class ReviewExtension
20
 *
21
 * @author  Adam Piotrowski <[email protected]>
22
 */
23
final class ReviewExtension extends \Twig_Extension
24
{
25
    public function getFunctions()
26
    {
27
        return [
28
            new \Twig_SimpleFunction('productReviewAverage', [$this, 'getReviewAverage'], ['is_safe' => ['html']]),
29
        ];
30
    }
31
    
32
    /**
33
     * {@inheritdoc}
34
     */
35
    public function getName()
36
    {
37
        return 'review';
38
    }
39
    
40
    public function getReviewAverage(Collection $collection): float
41
    {
42
        $totalRating  = 0;
43
        $reviewsTotal = $collection->count();
44
        
45
        $collection->map(function (Review $review) use (&$totalRating) {
46
            $totalRating += $review->getRating();
47
        });
48
        
49
        return ($reviewsTotal > 0) ? round($totalRating / $reviewsTotal, 2) : 0;
50
    }
51
}
52