Example #1
0
 public function it_can_halt_future_actions_if_one_returns_false(\Vivait\Voter\Model\VoterInterface $voter, \Vivait\Voter\Model\ActionInterface $action1, \Vivait\Voter\Model\ActionInterface $action2)
 {
     $entity = new \stdClass();
     $action1->requires()->willReturn('stdClass')->shouldBeCalled();
     $action2->requires()->willReturn('stdClass')->shouldBeCalled();
     $action1->perform([$entity])->willReturn(false)->shouldBeCalled();
     $action2->perform([$entity])->shouldNotBeCalled();
     $this->addActions([$action1, $action2]);
     $voter->supports(['stdClass'])->willReturn(true)->shouldBeCalled();
     $voter->result([$entity])->willReturn(true)->shouldBeCalled();
     $this->perform([$entity]);
 }
Example #2
0
 public function perform($entity)
 {
     $entity_map = is_array($entity) ? array_map(function ($obj) {
         return get_class($obj);
     }, $entity) : [get_class($entity)];
     if (!$this->voter->supports($entity_map)) {
         throw new \RuntimeException(sprintf('Inspection "%s" with voter "%s" does not support entities: %s', $this->name, get_class($this->voter), implode(', ', $entity_map)));
     }
     $result = $this->voter->result($entity);
     $this->logger->debug(sprintf('Inspection "%s" with voter "%s" returned result: %s', $this->name, get_class($this->voter), $result ? 'true' : 'false'));
     if ($result) {
         $this->performActions($entity);
     }
 }