Commit a8cf0446 authored by Laurent Lecluse's avatar Laurent Lecluse
Browse files

Création d'un trait (AuthorizeServiceTrait) pour pouvoir accéder au service...

Création d'un trait (AuthorizeServiceTrait) pour pouvoir accéder au service Authorize (pour faire $this->getServiceAuthorize->isAllowed par exemple)
parent f59f92b8
Loading
Loading
Loading
Loading
+56 −0
Original line number Diff line number Diff line
<?php

namespace UnicaenAuth\Service\Traits;

use UnicaenAuth\Service\AuthorizeService;
use RuntimeException;

/**
 * Description of AuthorizeServiceAwareTrait
 *
 * @author Laurent LÉCLUSE <laurent.lecluse at unicaen.fr>
 */
trait AuthorizeServiceAwareTrait
{
    /**
     * @var AuthorizeService
     */
    private $serviceAuthorize;



    /**
     * @param AuthorizeService $serviceAuthorize
     *
     * @return self
     */
    public function setServiceAuthorize(AuthorizeService $serviceAuthorize)
    {
        $this->serviceAuthorize = $serviceAuthorize;

        return $this;
    }



    /**
     * @return AuthorizeService
     * @throws RuntimeException
     */
    public function getServiceAuthorize()
    {
        if (empty($this->serviceAuthorize)) {
            if (!method_exists($this, 'getServiceLocator')) {
                throw new RuntimeException('La classe ' . get_class($this) . ' n\'a pas accès au ServiceLocator.');
            }

            $serviceLocator = $this->getServiceLocator();
            if (method_exists($serviceLocator, 'getServiceLocator')) {
                $serviceLocator = $serviceLocator->getServiceLocator();
            }
            $this->serviceAuthorize = $serviceLocator->get('BjyAuthorize\Service\Authorize');
        }

        return $this->serviceAuthorize;
    }
}
 No newline at end of file