Skip to content

Commit

Permalink
Add utility methods to PostUploadEvents to generate mapping-specifi…
Browse files Browse the repository at this point in the history
…c event names (#350)
  • Loading branch information
mark-gerarts authored and bytehead committed Nov 17, 2018
1 parent 9ea61f3 commit 1920b46
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 2 deletions.
13 changes: 11 additions & 2 deletions Resources/doc/events.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,17 @@ Moreover this bundles also dispatches some special kind of generic events you ca
* `oneup_uploader.post_upload.{mapping}`
* `oneup_uploader.post_persist.{mapping}`
* `oneup_uploader.post_chunk_upload.{mapping}`
* `oneup_uploader.validation.{mapping}`

The `{mapping}` part is the key of your configured mapping. The examples in this documentation always uses the mapping key `gallery`. So the dispatched event would be called `oneup_uploader.post_upload.gallery`.
Using these generic events can save you some time and coding lines, as you don't have to check for the correct type in the `EventListener`.
Using these generic events can save you some time and coding lines, as you don't have to check for the correct type in the `EventListener`. The `UploadEvents` class provides some utility methods to generate these
mapping-specific event names. For example:

See the [custom logic section](custom_logic.md) of this documentation for specific examples on how to use these Events.
```php
public static function getSubscribedEvents()
{
return [UploadEvents::postUpload('gallery') => ['onPostUpload']];
}
```

See the [custom logic section](custom_logic.md) of this documentation for specific examples on how to use these Events.
44 changes: 44 additions & 0 deletions Tests/UploadEventsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace Oneup\UploaderBundle\Tests;

use Oneup\UploaderBundle\UploadEvents;
use PHPUnit\Framework\TestCase;

class UploadEventsTest extends TestCase
{
public function testPreUploadCanBePassedAMapping()
{
$event = UploadEvents::preUpload('gallery');

$this->assertEquals(UploadEvents::PRE_UPLOAD . '.gallery', $event);
}

public function testPostUploadCanBePassedAMapping()
{
$event = UploadEvents::postUpload('gallery');

$this->assertEquals(UploadEvents::POST_UPLOAD . '.gallery', $event);
}

public function testPostPersistCanBePassedAMapping()
{
$event = UploadEvents::postPersist('gallery');

$this->assertEquals(UploadEvents::POST_PERSIST . '.gallery', $event);
}

public function testPostChunkUploadCanBePassedAMapping()
{
$event = UploadEvents::postChunkUpload('gallery');

$this->assertEquals(UploadEvents::POST_CHUNK_UPLOAD . '.gallery', $event);
}

public function testValidationCanBePassedAMapping()
{
$event = UploadEvents::validation('gallery');

$this->assertEquals(UploadEvents::VALIDATION . '.gallery', $event);
}
}
30 changes: 30 additions & 0 deletions UploadEvents.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,34 @@ final class UploadEvents
const POST_PERSIST = 'oneup_uploader.post_persist';
const POST_CHUNK_UPLOAD = 'oneup_uploader.post_chunk_upload';
const VALIDATION = 'oneup_uploader.validation';

public static function preUpload(string $mapping): string
{
return self::withMapping(self::PRE_UPLOAD, $mapping);
}

public static function postUpload(string $mapping): string
{
return self::withMapping(self::POST_UPLOAD, $mapping);
}

public static function postPersist(string $mapping): string
{
return self::withMapping(self::POST_PERSIST, $mapping);
}

public static function postChunkUpload(string $mapping): string
{
return self::withMapping(self::POST_CHUNK_UPLOAD, $mapping);
}

public static function validation(string $mapping): string
{
return self::withMapping(self::VALIDATION, $mapping);
}

private static function withMapping(string $event, string $mapping): string
{
return "{$event}.{$mapping}";
}
}

0 comments on commit 1920b46

Please sign in to comment.