Skip to content

Commit

Permalink
Merge pull request #1 from frozenminds/feature/event-virus-detection
Browse files Browse the repository at this point in the history
Dispatch an event when virus is uploaded
  • Loading branch information
balazscsaba2006 authored Nov 20, 2018
2 parents 9ff9486 + 295da90 commit 575416d
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 4 deletions.
2 changes: 1 addition & 1 deletion DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public function getConfigTreeBuilder(): TreeBuilder
->validate()
->ifArray()
->then(function ($v) {
if ($v['alias'] === 'clamavphp' && !class_exists('\CL\Tissue\Adapter\ClamAVPHP\ClamAVPHPAdapter')) {
if ('clamavphp' === $v['alias'] && !class_exists('\CL\Tissue\Adapter\ClamAVPHP\ClamAVPHPAdapter')) {
throw new InvalidConfigurationException('If you want to use the `clamavphp` adapter, you need to add the `cleentfaar/tissue-clamavphp-adapter` package to your composer.json');
}

Expand Down
2 changes: 1 addition & 1 deletion DependencyInjection/TissueExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ private function setParameters(array $config, ContainerBuilder $container)
}

foreach ($config['adapter'] as $key => $val) {
if ($key === 'options') {
if ('options' === $key) {
foreach ($val as $k => $v) {
$container->setParameter(sprintf('tissue.adapter.options.%s', $k), $v);
}
Expand Down
58 changes: 58 additions & 0 deletions Event/DetectionVirusEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

namespace CL\Bundle\TissueBundle\Event;

use CL\Bundle\TissueBundle\Validator\Constraints\CleanFile;
use Symfony\Component\EventDispatcher\Event;
use Symfony\Component\HttpFoundation\File\UploadedFile;

/**
* Virus Detection Event.
*
* @copyright Evozon Systems SRL (http://www.evozon.com/)
* @author Constantin Bejenaru <constantin.bejenaru@evozon.com>
*/
class DetectionVirusEvent extends Event
{
/**
* @var UploadedFile
*/
private $uploadedFile;

/**
* @var CleanFile
*/
private $constraint;

/**
* Constructor.
*
* @param UploadedFile $uploadedFile
* @param CleanFile $constraint
*/
public function __construct(UploadedFile $uploadedFile, CleanFile $constraint)
{
$this->uploadedFile = $uploadedFile;
$this->constraint = $constraint;
}

/**
* Get UploadedFile.
*
* @return UploadedFile
*/
public function getUploadedFile(): UploadedFile
{
return $this->uploadedFile;
}

/**
* Get Constraint.
*
* @return CleanFile
*/
public function getConstraint(): CleanFile
{
return $this->constraint;
}
}
17 changes: 17 additions & 0 deletions Events.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace CL\Bundle\TissueBundle;

/**
* Events.
*
* @copyright Evozon Systems SRL (http://www.evozon.com/)
* @author Constantin Bejenaru <constantin.bejenaru@evozon.com>
*/
final class Events
{
/**
* Dispatched when a virus detection is found during an upload file validation.
*/
const DETECTION_VIRUS = 'tissue.detection.virus';
}
1 change: 1 addition & 0 deletions Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ services:

CL\Bundle\TissueBundle\Validator\Constraints\CleanFileValidator:
arguments:
- "@event_dispatcher"
- "@tissue.scanner"
tags:
- { name: validator.constraint_validator, alias: clean_file }
19 changes: 17 additions & 2 deletions Validator/Constraints/CleanFileValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@

namespace CL\Bundle\TissueBundle\Validator\Constraints;

use CL\Bundle\TissueBundle\Event\DetectionVirusEvent;
use CL\Bundle\TissueBundle\Events;
use CL\Tissue\Adapter\AdapterInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpFoundation\File\File;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\Constraints\FileValidator;
Expand All @@ -22,16 +25,25 @@
*/
class CleanFileValidator extends FileValidator
{
/**
* @var EventDispatcherInterface
*/
private $eventDispatcher;

/**
* @var AdapterInterface|null
*/
private $adapter;

/**
* @param AdapterInterface|null $adapter
* Constructor.
*
* @param EventDispatcherInterface $eventDispatcher
* @param AdapterInterface|null $adapter
*/
public function __construct(AdapterInterface $adapter = null)
public function __construct(EventDispatcherInterface $eventDispatcher, ?AdapterInterface $adapter)
{
$this->eventDispatcher = $eventDispatcher;
$this->adapter = $adapter;
}

Expand All @@ -57,6 +69,9 @@ public function validate($value, Constraint $constraint): void
}
$this->context->buildViolation($constraint->virusDetectedMessage)->addViolation();

$event = new DetectionVirusEvent($value, $constraint);
$this->eventDispatcher->dispatch(Events::DETECTION_VIRUS, $event);

return;
}

Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"symfony/symfony": "^3.0 || ^4.0",
"symfony/options-resolver": "^3.0 || ^4.0",
"symfony/validator": "^3.0 || ^4.0",
"symfony/event-dispatcher": "^3.0 || ^4.0",
"evozon-php/tissue": "*",
"evozon-php/tissue-clamav-adapter": "*"
},
Expand Down

0 comments on commit 575416d

Please sign in to comment.