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/Feed.php
<?php

namespace App\Komma\Kiyoh;

use App\Komma\Kiyoh\Resources\Company;
use App\Komma\Kiyoh\Resources\Review;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use Illuminate\Support\Str;

class Feed
{
    /**
     * @var \App\Komma\Kiyoh\Client
     */
    protected $apiClient;

    /**
     * The maximum number of reviews to fetch.
     *
     * @var int
     */
    protected $limit;

    /**
     * Include migrated reviews. (defaults to false).
     *
     * @var bool
     */
    protected $withMigrated;

    /**
     * @var \App\Komma\Kiyoh\Resources\Company;
     */
    public $company;

    /**
     * @var \Illuminate\Support\Collection
     */
    public $reviews;

    /**
     * Create a new Feed instance.
     *
     * @param \App\Komma\Kiyoh\Client $client
     * @return void
     */
    public function __construct(Client $client)
    {
        $this->apiClient = $client;

        $this->limit = 10;
        $this->withMigrated = false;

        $this->company = new Company();
        $this->reviews = new Collection();
    }

    /**
     * Get the feed.
     *
     * @return $this
     */
    public function get()
    {
        $response = collect(
            $this->apiClient->performHttpCall([
                'limit' => $this->getLimit(),
            ])
        );

        $this->company->fill(
            $this->getCompanyAttributes($response)
        );

        $response->only('reviews')->flatten(1)
            ->when(! $this->withMigrated, function ($collection) {
                return $collection->reject(function ($review) {
                    return Str::startsWith($review['reviewId'], 'KIYNL-');
                });
            })->each(function ($item) {
                $this->reviews->push(
                    new Review($this->getReviewAttributes($item))
                );
            });

        return $this;
    }

    /**
     * Get the maximum numbers of reviews.
     *
     * @return int
     */
    public function getLimit()
    {
        return $this->limit;
    }

    /**
     * Include migrated reviews in the feed.
     *
     * @param bool $value
     * @return $this
     */
    public function withMigrated($value = true)
    {
        $this->withMigrated = (bool) $value;

        return $this;
    }

    /**
     * Set the maximum number of reviews to fetch.
     *
     * @param int $value
     * @return $this
     */
    public function limit($value)
    {
        if (is_numeric($value) && $value >= 0) {
            $this->limit = (int) $value;
        }

        return $this;
    }

    /**
     * Get the company attributes from the collection.
     *
     * @param \Tightenco\Collect\Support\Collection $collection
     * @return array
     */
    private function getCompanyAttributes($collection)
    {
        return $collection->intersectByKeys([
            'locationId' => '',
            'locationName' => '',
            'averageRating' => '',
            'numberReviews' => '',
            'percentageRecommendation' => '',
        ])->all();
    }

    /**
     * Get the review attributes from the "reviewContent".
     *
     * @param array $value
     * @return array
     */
    private function getReviewAttributes(array $array)
    {
        $attributes = collect($array)->only('reviewContent')->flatten(1)->mapWithKeys(function ($item) {
            if ($key = $this->lookupReviewContentAttribute($item['questionGroup'])) {
                return [
                    $key => Arr::get($item, 'rating', 0),
                ];
            }

            return [];
        });

        return collect($array)->forget('reviewContent')->merge($attributes)->all();
    }

    /**
     * Lookup the attribute key for the given "questionGroup".
     *
     * @param string $value
     * @return string
     */
    private function lookupReviewContentAttribute($value)
    {
        return collect([
            'DEFAULT_RECOMMEND' => 'recommendation',
            'DEFAULT_ONELINER' => 'headline',
            'DEFAULT_OVERALL' => 'rating',
            'DEFAULT_OPINION' => 'text',
        ])
        ->get($value);
    }
}