Invis1ble/assistant

View on GitHub
src/AppBundle/Security/Authorization/Voter/TaskVoter.php

Summary

Maintainability
A
2 hrs
Test Coverage
<?php
 
namespace AppBundle\Security\Authorization\Voter;
 
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
use Symfony\Component\Security\Core\User\UserInterface;
 
use AppBundle\Entity\Task;
 
/**
* TaskVoter
*
* @author Max Invis1ble
* @copyright (c) 2016, Max Invis1ble
* @license http://www.opensource.org/licenses/mit-license.php MIT
*/
class TaskVoter extends Voter
{
const SHOW = 'show';
const EDIT = 'edit';
const DELETE = 'delete';
 
/**
* Determines if the attribute and subject are supported by this voter.
*
* @param string $attribute An attribute
* @param mixed $subject The subject to secure, e.g. an object the user wants to access or any other PHP type
*
* @return bool True if the attribute and subject are supported, false otherwise
*/
Identical blocks of code found in 2 locations. Consider refactoring.
protected function supports($attribute, $subject)
{
$attribute = strtolower($attribute);
 
if (!in_array($attribute, [
self::SHOW,
self::EDIT,
self::DELETE,
])) {
return false;
}
 
if (!$subject instanceof Task) {
return false;
}
 
return true;
}
 
/**
* Perform a single access check operation on a given attribute, subject and token.
*
* @param string $attribute
* @param Task $task
* @param TokenInterface $token
*
* @return bool
*/
Method `voteOnAttribute` has 27 lines of code (exceeds 25 allowed). Consider refactoring.
Function `voteOnAttribute` has a Cognitive Complexity of 8 (exceeds 5 allowed). Consider refactoring.
protected function voteOnAttribute($attribute, $task, TokenInterface $token)
{
$user = $token->getUser();
 
if (!$user instanceof UserInterface) {
return false;
}
 
$username = $user->getUsername();
 
$ownerUsername = $task->getCategory()
->getUser()
->getUsername()
;
 
switch ($attribute) {
case self::SHOW:
if ($username === $ownerUsername) {
return true;
}
 
break;
 
case self::EDIT:
if ($username === $ownerUsername) {
return true;
}
 
break;
 
case self::DELETE:
if ($username === $ownerUsername) {
return true;
}
 
break;
}
 
Avoid too many `return` statements within this method.
return false;
}
}