public function it_evaluates_the_rule_based_on_subject_to_false_when_exception_is_thrown(RuleInterface $rule, RuleSubjectInterface $subject, LoggerInterface $logger, ExpressionLanguage $expression) { $rule->getExpression()->shouldBeCalled(); $subject->getSubjectType()->shouldBeCalled(); $expression->evaluate(Argument::type('string'), Argument::type('array'))->willReturn(false); $logger->error(Argument::type('string'))->shouldBeCalled(); $this->evaluate($rule, $subject)->shouldReturn(false); }
/** * {@inheritdoc} */ public function evaluate(RuleInterface $rule, RuleSubjectInterface $subject) { try { return (bool) $this->expression->evaluate($rule->getExpression(), [$subject->getSubjectType() => $subject]); } catch (\Exception $e) { $this->logger->error(sprintf('%s Trace: %s', $e->getMessage(), $e->getTraceAsString())); } return false; }
public function it_applies_rule(RuleInterface $rule, Article $subject, RouteProviderInterface $routeProvider, RouteInterface $route, LoggerInterface $logger, ArticleServiceInterface $articleService) { $rule->getConfiguration()->willReturn(['route' => 'some/route', 'templateName' => 'template.twig.html', 'published' => 'true']); $rule->getExpression()->willReturn('article.getSomething("something") matches /something/'); $routeProvider->getOneById('some/route')->willReturn($route); $subject->setRoute($route)->shouldBeCalled(); $subject->setTemplateName('template.twig.html')->shouldBeCalled(); $articleService->publish($subject)->shouldBeCalled(); $logger->info(Argument::any('string'))->shouldBeCalled(); $this->apply($rule, $subject)->shouldReturn(null); }
/** * {@inheritdoc} */ public function apply(RuleInterface $rule, RuleSubjectInterface $subject) { $configuration = $this->validateRuleConfiguration($rule->getConfiguration()); if (!$this->isAllowedType($subject) || empty($configuration)) { return; } /* @var ArticleInterface $subject */ if (isset($configuration['route'])) { $route = $this->routeProvider->getOneById($configuration['route']); if (null === $route) { $this->logger->warning('Route not found! Make sure the rule defines an existing route!'); return; } $subject->setRoute($route); } $subject->setTemplateName($configuration['templateName']); if ((bool) $configuration['published']) { $this->articleService->publish($subject); } $this->logger->info(sprintf('Configuration: "%s" for "%s" rule has been applied!', json_encode($configuration), $rule->getExpression())); }