app/Plugin/CustomerGroup/Security/Authorization/Voter/ProductVoter.php line 25

Open in your IDE?
  1. <?php
  2. /**
  3.  * This file is part of CustomerGroup
  4.  *
  5.  * Copyright(c) Akira Kurozumi <info@a-zumi.net>
  6.  *
  7.  * https://a-zumi.net
  8.  *
  9.  * For the full copyright and license information, please view the LICENSE
  10.  * file that was distributed with this source code.
  11.  */
  12. namespace Plugin\CustomerGroup\Security\Authorization\Voter;
  13. use Eccube\Entity\Product;
  14. use Plugin\CustomerGroup\Utils\Accessible;
  15. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  16. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  17. /**
  18.  * Class ProductVoter
  19.  * @package Plugin\CustomerGroup\Security\Voter
  20.  */
  21. class ProductVoter extends Voter
  22. {
  23.     const VIEW 'view';
  24.     const CART 'cart';
  25.     /**
  26.      * @var Accessible
  27.      */
  28.     private $accessible;
  29.     public function __construct(Accessible $accessible)
  30.     {
  31.         $this->accessible $accessible;
  32.     }
  33.     /**
  34.      * @param string $attribute
  35.      * @param mixed $subject
  36.      * @return bool
  37.      */
  38.     protected function supports($attribute$subject)
  39.     {
  40.         if (!in_array($attribute, [self::VIEWself::CART])) {
  41.             return false;
  42.         }
  43.         if (!$subject instanceof Product) {
  44.             return false;
  45.         }
  46.         return true;
  47.     }
  48.     /**
  49.      * @param string $attribute
  50.      * @param Product $subject
  51.      * @param TokenInterface $token
  52.      * @return bool|void
  53.      */
  54.     protected function voteOnAttribute($attribute$subjectTokenInterface $token)
  55.     {
  56.         if (self::VIEW === $attribute) {
  57.             return $this->accessible->can($attribute$subject$token);
  58.         }
  59.         if (self::CART === $attribute) {
  60.             return $this->accessible->can($attribute$subject$token);
  61.         }
  62.         throw new \LogicException();
  63.     }
  64. }