FlagsReviewComposer.php 2.03 KB
<?php

namespace FootyRoom\Http\ViewComposers;

use FootyRoom\User\User;
use FootyRoom\Queries\Comment\FlagsReportQuery;
use FootyRoom\Support\Pagination\Paginator;
use Illuminate\Contracts\View\View;

class FlagsReviewComposer
{
    /**
     * @var int
     */
    const ITEMS_PER_PAGE = 1;

    /**
     * @var \FootyRoom\Queries\Comment\FlagsReportQuery
     */
    protected $flagsReportsQuery;

    /**
     * Constructor.
     *
     * @param \FootyRoom\Queries\Comment\FlagsReportQuery $flagsReportsQuery
     */
    public function __construct(FlagsReportQuery $flagsReportsQuery)
    {
        $this->flagsReportsQuery = $flagsReportsQuery;
    }

    /**
     * Compose the view.
     *
     * @param \Illuminate\Contracts\View\View $view
     * @param \FootyRoom\User\User $user
     * @param int $page
     *
     * @return \Illuminate\Contracts\View\View
     */
    public function compose(View $view, User $user, $page = 1)
    {
        $this->loadFlagsReports($view, $user, $page);
        $reportsCount = $this->getPostsCount($this->getPostsCount());

        if (!count($view->flagsReports) && $page > 1) {
            $this->loadFlagsReports($view, $user, $page = 1);
        }

        $view->pagination = (new Paginator(
            $reportsCount,
            self::ITEMS_PER_PAGE,
            $page
        ))
        ->setPath(route('flagsReview'));

        return $view;
    }

    /**
     * Returns flags reports count.
     *
     * @return int
     */
    protected function getPostsCount()
    {
        return $this->flagsReportsQuery->count();
    }

    /**
     * Loads flags reports.
     *
     * @param \Illuminate\Contracts\View\View $view
     * @param \FootyRoom\User\User $user The user
     * @param int $page
     */
    protected function loadFlagsReports(View $view, User $user, $page)
    {
        $flagsReports = $this->flagsReportsQuery->find(
            $user->getRole(),
            self::ITEMS_PER_PAGE,
            self::ITEMS_PER_PAGE * ($page - 1)
        );

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