HoFPostComposer.php 3.42 KB
<?php

namespace FootyRoom\Http\ViewComposers;

use FootyRoom\Queries\HoFQuery;
use FootyRoom\Queries\PostQuery;
use Illuminate\Contracts\View\View;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

class HoFPostComposer
{
    /**
     * @var \FootyRoom\Queries\HoFQuery
     */
    protected $hofQuery;

    /**
     * @var \FootyRoom\Queries\PostQuery
     */
    protected $postQuery;

    /**
     * Constructor.
     *
     * @param \FootyRoom\Queries\HoFQuery $hofQuery
     * @param \FootyRoom\Queries\PostQuery $postQuery
     */
    public function __construct(
        HoFQuery $hofQuery,
        PostQuery $postQuery
    ) {
        $this->hofQuery = $hofQuery;
        $this->postQuery = $postQuery;
    }

    /**
     * Compose the view.
     *
     * @param \Illuminate\Contracts\View\View $view
     * @param string $playerSlug
     *
     * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
     *
     * @return \Illuminate\Contracts\View\View
     */
    public function compose(View $view, $playerSlug)
    {
        // Load post with meta data.
        $this->loadPost($view, $playerSlug);

        $playerName = $view->post->title;

        // Load history data.
        $this->loadHistory($view, $playerName);

        // Load related histories data.
        $this->loadRelatedPlayers($view, $view->player);

        // Load records data.
        $this->loadRecords($view, $playerName);

        // Load honors data.
        $this->loadHonors($view, $playerName);

        return $view;
    }

    /**
     * Loads post.
     *
     * @param \Illuminate\Contracts\View\View $view
     * @param string $playerSlug
     */
    protected function loadPost(View $view, $playerSlug)
    {
        $post = $this->postQuery->findBySlug($playerSlug);

        if (!$post) {
            throw new NotFoundHttpException();
        }

        $post->meta = $this->postQuery->findMetaByPostId($post->id, ['pft_widescreen', 'video']);

        $view->with('post', $post);
    }

    /**
     * Loads player history information.
     *
     * @param \Illuminate\Contracts\View\View $view
     * @param string $playerName
     */
    protected function loadHistory(View $view, $playerName)
    {
        $player = $this->hofQuery->findBySlug($playerName);

        $view->with('player', $player);
    }

    /**
     * Loads player records.
     *
     * @param \Illuminate\Contracts\View\View $view
     * @param string $playerName
     */
    protected function loadRecords(View $view, $playerName)
    {
        $view->with('records', $this->hofQuery->findRecordsByName($playerName));
    }

    /**
     * Loads honors.
     *
     * @param \Illuminate\Contracts\View\View $view
     * @param string $playerName
     */
    protected function loadHonors(View $view, $playerName)
    {
        $view->with('honours', $this->hofQuery->findHonorsByName($playerName));
    }

    /**
     * Loads related players.
     *
     * @param \Illuminate\Contracts\View\View $view
     * @param object $player
     */
    protected function loadRelatedPlayers(View $view, $player)
    {
        $relatedPlayers = $this->hofQuery->findByPeriod($player->period);

        // Remove current player from $relatedPlayers
        foreach ($relatedPlayers as $key => $relatedPlayer) {
            if ($player->id === $relatedPlayer->id) {
                unset($relatedPlayers[$key]);
            }
        }

        $view->with('relatedPlayers', $relatedPlayers);
    }
}