HEX
Server: Microsoft-IIS/8.5
System: Windows NT YDAWBH120 6.3 build 9600 (Windows Server 2012 R2 Standard Edition) AMD64
User: tentjecom_web (0)
PHP: 7.4.14
Disabled: NONE
Upload Files
File: D:/HostingSpaces/SBogers10/farmfun.komma.pro/app/Komma/Kiyoh/KiyohComposer.php
<?php

namespace App\Komma\Kiyoh;

use App\Komma\Locations\Models\Location;
use Illuminate\View\View;

final class KiyohComposer
{
    /** @var KiyohService */
    private $kiyohService;

    public function __construct()
    {
        $this->kiyohService = app()->make(KiyohService::class);
    }

    public function general(View $view)
    {
        $view->with('kiyoh', (object) $this->kiyohService->getTotalScore());
    }

    public function all(View $view)
    {
        $view->with('reviews', $this->kiyohService->getLastReviews());
    }

    public function topReviewsAsJson(View $view, int $rating = 9, int $limit = 5): View
    {
        $reviews = $this->kiyohService->getLastReviews($rating, $limit);
        $locations = Location::with('translation')->get();

        $mappedReviews = $reviews->map(function ($review) use ($locations) {
            return [
                'location' => $locations->find($review->location_id)->translation->name,
                'rating' => $review->rating,
                'date' => $review->created_at->format('Y-m-d'),
                'authorName' => $review->payload['author']['name'],
                'headline' => $review->payload['headline'],
                'text' => $review->payload['text'],
            ];
        });

        return $view->with('reviews', json_encode($mappedReviews));
    }
}