<?php
namespace App\Security\SuperheroOffice;
use App\Entity\SuperheroOffice\UserOffice;
use App\Handler\SuperheroOffice\ShareholderHandler;
use App\Handler\SuperheroOffice\UserOfficeHandler;
use Psr\Container\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
use Symfony\Contracts\Service\ServiceSubscriberInterface;
/**
* Class StatusVoter
*
* @package App\Security\SuperheroOffice
*/
class StatusVoter extends Voter implements ServiceSubscriberInterface
{
/**
* @var ContainerInterface
*/
private $container;
/**
* StatusVoter constructor.
*
* @param ContainerInterface $container
*/
public function __construct(ContainerInterface $container)
{
$this->container = $container;
}
/**
* @param string $attribute
* @param mixed $subject
*
* @return bool
*/
protected function supports($attribute, $subject)
{
return array_key_exists($attribute, UserOffice::STATUSES)
&& $subject instanceof Request;
}
/**
* @param string $attribute
* @param mixed $subject
* @param TokenInterface $token
*
* @return bool
* @throws \Doctrine\ORM\ORMException
*/
protected function voteOnAttribute($attribute, $subject, TokenInterface $token)
{
$userOffice = $this->container->get(UserOfficeHandler::class)->getUserOffice();
$shareholderHandler = $this->container->get(ShareholderHandler::class);
switch ($attribute) {
case 'started':
if ($userOffice->getStatus() === UserOffice::STATUSES['fill_business_information']) {
$shareholderHandler->entityHandler->fullSave($userOffice->setStatus(UserOffice::STATUSES['started']));
return true;
}
return false;
case 'fill_business_information':
return
(
$userOffice->getRegistrationType() !== UserOffice::REGISTRATION_TYPES['banking'] ||
$userOffice->getRegistrationType() !== UserOffice::REGISTRATION_TYPES['switching']
)
&&
(
$userOffice->getStatus() === UserOffice::STATUSES['declined'] ||
$userOffice->getStatus() === UserOffice::STATUSES['started'] ||
$userOffice->getStatus() === UserOffice::STATUSES['fill_shareholders']
)
;
case 'fill_shareholders':
return
(
$userOffice->getRegistrationType() === UserOffice::REGISTRATION_TYPES['company'] ||
$userOffice->getRegistrationType() === UserOffice::REGISTRATION_TYPES['both']
)
&&
(
$userOffice->getStatus() === UserOffice::STATUSES['fill_shareholders']
)
;
case 'waiting_letter_of_engagement':
return
(
$userOffice->getShareholders()->count() > 0 &&
$userOffice->getStatus() === UserOffice::STATUSES['fill_shareholders']
)
||
(
$userOffice->getStatus() === UserOffice::STATUSES['setup_company']
)
;
case 'setup_company':
return $userOffice->getStatus() === UserOffice::STATUSES['started'] ||
$userOffice->getStatus() === UserOffice::STATUSES['declined'];
}
return false;
}
/**
* @return array
*/
public static function getSubscribedServices(): array
{
return [
UserOfficeHandler::class,
ShareholderHandler::class
];
}
}