vendor/shopware/core/Content/Product/SalesChannel/Review/RatingMatrix.php line 12

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\Content\Product\SalesChannel\Review;
  3. use Shopware\Core\Framework\DataAbstractionLayer\Search\AggregationResult\Bucket\Bucket;
  4. use Shopware\Core\Framework\Struct\Struct;
  5. if (class_exists('Shopware\Storefront\Page\Product\Review\RatingMatrix')) {
  6.     /**
  7.      * @deprecated tag:v6.5.0 - reason:class-hierarchy-change - will only extend from `Struct` in the future
  8.      */
  9.     class RatingMatrix extends \Shopware\Storefront\Page\Product\Review\RatingMatrix
  10.     {
  11.         private const MAX_POINTS = 5;
  12.         /**
  13.          * @var MatrixElement[]
  14.          */
  15.         protected $matrix = [];
  16.         /**
  17.          * @var int
  18.          */
  19.         protected $totalReviewCount = 0;
  20.         /**
  21.          * @var float
  22.          */
  23.         protected $pointSum = 0;
  24.         /**
  25.          * we expect an array of CountResult elements
  26.          * we are doing all calculation in constructor
  27.          *
  28.          * @param Bucket[] $matrix
  29.          */
  30.         public function __construct(array $matrix)
  31.         {
  32.             for ($points = self::MAX_POINTS; $points > 0; --$points) {
  33.                 foreach ($matrix as $rating) {
  34.                     $rawRatingKey = (float) $rating->getKey();
  35.                     if ($points === (int) round($rawRatingKey)) {
  36.                         $this->pointSum += ($rawRatingKey * $rating->getCount());
  37.                         $this->totalReviewCount += $rating->getCount();
  38.                         $previousCount = isset($this->matrix[$points]) ? $this->matrix[$points]->getCount() : 0;
  39.                         $this->matrix[$points] = new MatrixElement($points, $rating->getCount() + $previousCount);
  40.                     }
  41.                 }
  42.                 if (!\array_key_exists($points, $this->matrix)) {
  43.                     $this->matrix[$points] = new MatrixElement($points, 0);
  44.                 }
  45.             }
  46.             array_walk($this->matrix, function (MatrixElement &$rating): void {
  47.                 if ($this->totalReviewCount > 0) {
  48.                     $rating->setPercent($rating->getCount() / $this->totalReviewCount * 100);
  49.                 } else {
  50.                     $rating->setPercent(0);
  51.                 }
  52.             });
  53.         }
  54.         public function getMatrix(): array
  55.         {
  56.             return $this->matrix;
  57.         }
  58.         public function getTotalReviewCount(): int
  59.         {
  60.             return $this->totalReviewCount;
  61.         }
  62.         public function getPointSum(): float
  63.         {
  64.             return $this->pointSum;
  65.         }
  66.         public function getMaxPoints(): int
  67.         {
  68.             return self::MAX_POINTS;
  69.         }
  70.         public function getAverageRating(): float
  71.         {
  72.             if ($this->getPointSum() > 0) {
  73.                 return $this->getPointSum() / $this->getTotalReviewCount();
  74.             }
  75.             return 0;
  76.         }
  77.     }
  78. } else {
  79.     class RatingMatrix extends Struct
  80.     {
  81.         private const MAX_POINTS = 5;
  82.         /**
  83.          * @var MatrixElement[]
  84.          */
  85.         protected $matrix = [];
  86.         /**
  87.          * @var int
  88.          */
  89.         protected $totalReviewCount = 0;
  90.         /**
  91.          * @var float
  92.          */
  93.         protected $pointSum = 0;
  94.         /**
  95.          * we expect an array of CountResult elements
  96.          * we are doing all calculation in constructor
  97.          *
  98.          * @param Bucket[] $matrix
  99.          */
  100.         public function __construct(array $matrix)
  101.         {
  102.             for ($points = self::MAX_POINTS; $points > 0; --$points) {
  103.                 foreach ($matrix as $rating) {
  104.                     $rawRatingKey = (float) $rating->getKey();
  105.                     if ($points === (int) round($rawRatingKey)) {
  106.                         $this->pointSum += ($rawRatingKey * $rating->getCount());
  107.                         $this->totalReviewCount += $rating->getCount();
  108.                         $previousCount = isset($this->matrix[$points]) ? $this->matrix[$points]->getCount() : 0;
  109.                         $this->matrix[$points] = new MatrixElement($points, $rating->getCount() + $previousCount);
  110.                     }
  111.                 }
  112.                 if (!\array_key_exists($points, $this->matrix)) {
  113.                     $this->matrix[$points] = new MatrixElement($points, 0);
  114.                 }
  115.             }
  116.             array_walk($this->matrix, function (MatrixElement &$rating): void {
  117.                 if ($this->totalReviewCount > 0) {
  118.                     $rating->setPercent($rating->getCount() / $this->totalReviewCount * 100);
  119.                 } else {
  120.                     $rating->setPercent(0);
  121.                 }
  122.             });
  123.         }
  124.         public function getMatrix(): array
  125.         {
  126.             return $this->matrix;
  127.         }
  128.         public function getTotalReviewCount(): int
  129.         {
  130.             return $this->totalReviewCount;
  131.         }
  132.         public function getPointSum(): float
  133.         {
  134.             return $this->pointSum;
  135.         }
  136.         public function getMaxPoints(): int
  137.         {
  138.             return self::MAX_POINTS;
  139.         }
  140.         public function getAverageRating(): float
  141.         {
  142.             if ($this->getPointSum() > 0) {
  143.                 return $this->getPointSum() / $this->getTotalReviewCount();
  144.             }
  145.             return 0;
  146.         }
  147.     }
  148. }