Skip to content

Commit

Permalink
Merge pull request #227 from xabbuh/patch-1
Browse files Browse the repository at this point in the history
ensure compatibility with EventDispatcher 5.x
  • Loading branch information
romainneutron committed Jun 18, 2020
2 parents 89ac385 + 3053155 commit 13f7b28
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions Controller/ContentSecurityPolicyController.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,18 @@
use Nelmio\SecurityBundle\ContentSecurityPolicy\Violation\Log\Logger;
use Nelmio\SecurityBundle\ContentSecurityPolicy\Violation\Report;
use Psr\Log\LoggerInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface as LegacyEventDispatcherInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;

class ContentSecurityPolicyController
{
protected $logger;
private $filter;
private $dispatcher;

public function __construct($logger, EventDispatcherInterface $dispatcher = null, Filter $filter = null)
public function __construct($logger, $dispatcher = null, Filter $filter = null)
{
$this->logger = $logger;
$this->filter = $filter;
Expand All @@ -44,6 +45,10 @@ public function __construct($logger, EventDispatcherInterface $dispatcher = null
if (null === $filter) {
trigger_error(sprintf('%s\'s takes an %s instance as third argument since version 2.1; it will be required in version 3', self::class, Filter::class), E_USER_DEPRECATED);
}

if (null !== $dispatcher && !$dispatcher instanceof LegacyEventDispatcherInterface && !$dispatcher instanceof EventDispatcherInterface) {
throw new \TypeError(sprintf('The second argument of %s() must be an instance of "%s" or "%s" ("%s" given).', __METHOD__, EventDispatcherInterface::class, LegacyEventDispatcherInterface::class, is_object($dispatcher) ? get_class($object))):
}
}

public function indexAction(Request $request)
Expand All @@ -66,7 +71,11 @@ public function indexAction(Request $request)
}

if ($this->dispatcher) {
$this->dispatcher->dispatch(Events::VIOLATION_REPORT, new Event($report));
if ($this->dispatcher instanceof EventDispatcherInterface) {
$this->dispatcher->dispatch(new Event($report), Events::VIOLATION_REPORT);
} else {
$this->dispatcher->dispatch(Events::VIOLATION_REPORT, new Event($report));
}
}

if ($this->logger instanceof LoggerInterface) {
Expand Down

0 comments on commit 13f7b28

Please sign in to comment.