<?php


namespace App\Model\Facades\Readers;


use App\Model\Entities\Security\Role;
use App\Model\Services\PermissionService;

class PermissionReader extends BaseReader
{
    /** @var PermissionService */
    protected $permissionService;
    /** @var SiteReader */
    protected $siteReader;

    public function __construct(PermissionService $permissionService, SiteReader $siteReader)
    {
        $this->permissionService = $permissionService;
        $this->siteReader = $siteReader;
    }

    public function findRoleById($id) : Role
    {
        $role = $this->permissionService->findRoleById($id);
        $this->validateEntity($role);

        return $role;
    }

    public function validateRole($role) : Role
    {
        if ($role instanceof Role) {
            return $role;
        }

        return $this->findRoleById($role);
    }

    public function findGuestRole() : Role
    {
        $role = $this->permissionService->getGuestRole();
        $this->validateEntity($role);

        return $role;
    }

    public function findWebmasterRole() : Role
    {
        $role = $this->permissionService->getWebmasterRole();
        $this->validateEntity($role);

        return $role;
    }

    public function findSiteDefaultRole($site) : Role
    {
        $siteEntity = $this->siteReader->validateSite($site);
        $role = $this->permissionService->findSiteDefaultRole($siteEntity);
        $this->validateEntity($role);

        return $role;
    }
}