Skip to content

Commit

Permalink
Merge pull request DATA-DOG#31 from chancegarcia/documentation
Browse files Browse the repository at this point in the history
Resolves Example for labeler DATA-DOG#11
  • Loading branch information
l3pp4rd authored Dec 21, 2017
2 parents 27e10c4 + 19f0cc5 commit dbabba9
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
52 changes: 52 additions & 0 deletions docs/labeler.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#Setting a Labeler

Create a Labeler class/method.

\AppBundle\Labler\AuditLabler

namespace AppBundle\Labler;

use Symfony\Component\Security\Core\User\UserInterface;

class AuditLabler
{
public static function getLabel( $entity) {
if ($entity instanceof UserInterface) {
return $entity->getUsername();
}

return 'Unlabeled';
}
}

Re-define the audit subscriber service to call the `setLabler` method of the AuditSubscriber with the [callable](http://php.net/manual/en/language.types.callable.php).

app/config/config.yml

services:
...
datadog.event_subscriber.audit:
class: 'DataDog\AuditBundle\EventSubscriber\AuditSubscriber'
arguments: ["@security.token_storage"]
tags:
- { name: doctrine.event_subscriber, connection: default }
calls:
- ['setLabeler', [['\AppBundle\Labler\AuditLabler', 'getLabel']]]
...
or define in your services file

app/config/services.xml

...
<service id="datadog.event_subscriber.audit" class="DataDog\AuditBundle\EventSubscriber\AuditSubscriber">
<argument type="service" id="security.token_storage"/>
<tag name="doctrine.event_subscriber"/>
<call method="setLabeler">
<argument type="collection">
<argument>\AppBundle\Labler\AuditLabler</argument>
<argument>getLabel</argument>
</argument>
</call>
</service>
...
13 changes: 13 additions & 0 deletions example/src/AppBundle/Controller/DoctrineControllerTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,17 @@

namespace AppBundle\Controller;

use Doctrine\ORM\EntityManager;

/**
* Trait DoctrineControllerTrait
* @package AppBundle\Controller
*
* @method \Doctrine\Bundle\DoctrineBundle\Registry getDoctrine()
*/
trait DoctrineControllerTrait
{

private function persist(...$entities)
{
foreach ($entities as $entity) {
Expand All @@ -23,6 +32,10 @@ private function remove(...$entities)
}
}

/**
* @param $class
* @return \Doctrine\Common\Persistence\ObjectRepository|EntityManager
*/
private function repo($class)
{
return $this->getDoctrine()->getManager()->getRepository($class);
Expand Down

0 comments on commit dbabba9

Please sign in to comment.