Skip to content
This repository was archived by the owner on Jul 11, 2023. It is now read-only.

Commit 23d0f90

Browse files
author
Tito Costa
committed
added event before storing entity
1 parent ec71eb9 commit 23d0f90

10 files changed

Lines changed: 2143 additions & 46 deletions

File tree

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/vendor
2+
/.idea

DependencyInjection/Configuration.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ public function getConfigTreeBuilder()
2323
$rootNode
2424
->children()
2525
->scalarNode('logger')->isRequired()->cannotBeEmpty()->end()
26+
->end()
27+
->children()
28+
->scalarNode('db_entity_class')->defaultValue('Oh\FormErrorLogBundle\Entity\FormErrorLog')->cannotBeEmpty()->end()
2629
->end();
2730

2831
return $treeBuilder;

DependencyInjection/OhFormErrorLogExtension.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ public function load(array $configs, ContainerBuilder $container)
2323
$config = $this->processConfiguration($configuration, $configs);
2424

2525
$container->setAlias('oh_form_error_log.logger.manager', $config['logger']);
26+
$container->setParameter('oh_form_error_log.db.entity.class', $config['db_entity_class']);
2627

2728
$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
2829
$loader->load('services.yml');

Entity/FormErrorLogEntityInterface.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,6 @@ public function setFormName($formName);
99
public function setField($field);
1010

1111
public function setError($error);
12-
12+
13+
public function setValue($value);
1314
}

Event/Events.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace Oh\FormErrorLogBundle\Event;
4+
5+
final class Events
6+
{
7+
const PRE_PERSIST = 'oh_form_error_log.entity.pre_persist';
8+
}

Event/PrePersistEntityEvent.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace Oh\FormErrorLogBundle\Event;
4+
5+
use Oh\FormErrorLogBundle\Entity\FormErrorLogEntityInterface;
6+
use Symfony\Component\EventDispatcher\Event;
7+
8+
class PrePersistEntityEvent extends Event
9+
{
10+
/** @var FormErrorLogEntityInterface */
11+
private $entity;
12+
13+
/**
14+
* @param FormErrorLogEntityInterface $entity
15+
*/
16+
public function __construct(FormErrorLogEntityInterface $entity)
17+
{
18+
$this->entity = $entity;
19+
}
20+
21+
/**
22+
* @return FormErrorLogEntityInterface
23+
*/
24+
public function getEntity()
25+
{
26+
return $this->entity;
27+
}
28+
}

Logger/DatabaseLogger.php

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,40 +2,55 @@
22

33
namespace Oh\FormErrorLogBundle\Logger;
44

5-
use Oh\FormErrorLogBundle\Logger\ErrorLogInterface;
5+
use Doctrine\ORM\EntityManagerInterface;
6+
use Oh\FormErrorLogBundle\Entity\FormErrorLogEntityInterface;
7+
use Oh\FormErrorLogBundle\Event\Events;
8+
use Oh\FormErrorLogBundle\Event\PrePersistEntityEvent;
69
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
10+
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
711

812
class DatabaseLogger implements ErrorLogInterface
913
{
1014
private $em;
15+
1116
private $entityClass;
12-
13-
public function __construct($em, $entityClass)
17+
18+
private $eventDispatcher;
19+
20+
/**
21+
* @param EntityManagerInterface $em
22+
* @param $entityClass
23+
* @param EventDispatcherInterface $eventDispatcher
24+
*/
25+
public function __construct(EntityManagerInterface $em, $entityClass, EventDispatcherInterface $eventDispatcher)
1426
{
1527
$this->em = $em;
1628
$this->entityClass = $entityClass;
29+
$this->eventDispatcher = $eventDispatcher;
1730
}
18-
31+
1932
public function log($formName, $key, $error, $value = '', $uri = '')
2033
{
21-
if($this->entityClass == 'Oh\FormErrorLogBundle\Entity\FormErrorLogEntityInterface') {
34+
if ($this->entityClass == 'Oh\FormErrorLogBundle\Entity\FormErrorLogEntityInterface') {
2235
throw new InvalidArgumentException('You need to update your %oh_form_error_log.db.entity.class% parameter to your own class. See the README for help.');
2336
}
37+
38+
/** @var FormErrorLogEntityInterface $entity */
2439
$entity = new $this->entityClass;
25-
40+
2641
$entity->setFormName($formName);
2742
$entity->setField($key);
2843
$entity->setError($error);
2944
$entity->setValue($value);
3045
// for BC
31-
if(method_exists($entity, 'setUri')) {
46+
if (method_exists($entity, 'setUri')) {
3247
$entity->setUri($uri);
3348
}
34-
49+
50+
$this->eventDispatcher->dispatch(Events::PRE_PERSIST, new PrePersistEntityEvent($entity));
51+
3552
$this->em->persist($entity);
36-
53+
3754
$this->em->flush($entity);
38-
3955
}
40-
4156
}

Resources/config/services.yml

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,23 @@
11
parameters:
2-
oh_form_error_log.listener.class: Oh\FormErrorLogBundle\EventListener\ErrorLogSubscriber
3-
oh_form_error_log.logger.class: Oh\FormErrorLogBundle\Logger\Logger
4-
oh_form_error_log.db.class: Oh\FormErrorLogBundle\Logger\DatabaseLogger
5-
oh_form_error_log.db.entity.class: Oh\FormErrorLogBundle\Entity\FormErrorLog
2+
oh_form_error_log.listener.class: Oh\FormErrorLogBundle\EventListener\ErrorLogSubscriber
3+
oh_form_error_log.logger.class: Oh\FormErrorLogBundle\Logger\Logger
4+
oh_form_error_log.db.class: Oh\FormErrorLogBundle\Logger\DatabaseLogger
65

76
services:
87
oh_form_error_log.logger.db:
9-
class: "%oh_form_error_log.db.class%"
10-
arguments: ["@doctrine.orm.default_entity_manager", "%oh_form_error_log.db.entity.class%"]
8+
class: "%oh_form_error_log.db.class%"
9+
arguments: ["@doctrine.orm.default_entity_manager", "%oh_form_error_log.db.entity.class%", "@event_dispatcher"]
1110
oh_form_error_log.logger:
12-
class: "%oh_form_error_log.logger.class%"
13-
arguments: ["@logger"]
11+
class: "%oh_form_error_log.logger.class%"
12+
arguments: ["@logger"]
1413
tags:
1514
- { name: monolog.logger, channel: formerror }
1615
oh_form_error_log.listener.db:
17-
class: "%oh_form_error_log.listener.class%"
18-
arguments: ["@oh_form_error_log.logger.db", "@request_stack"]
16+
class: "%oh_form_error_log.listener.class%"
17+
arguments: ["@oh_form_error_log.logger.db", "@request_stack"]
1918
oh_form_error_log.listener:
20-
class: "%oh_form_error_log.listener.class%"
21-
arguments: ["@oh_form_error_log.logger", "@request_stack"]
19+
class: "%oh_form_error_log.listener.class%"
20+
arguments: ["@oh_form_error_log.logger", "@request_stack"]
2221
oh_form_subscriber:
2322
class: Oh\FormErrorLogBundle\EventListener\ErrorLogSubscriber
2423
arguments:

composer.json

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,27 @@
11
{
2-
"name": "lendable/form-error-log-bundle",
3-
"type": "symfony-bundle",
4-
"description": "Log form errors",
5-
"keywords": ["form", "Symfony2"],
6-
"homepage": "https://github.com/ollieLtd/OhFormErrorLogBundle",
7-
"license": "MIT",
8-
"authors": [
9-
{
10-
"name": "Ollie Harridge",
11-
"email": "code@oll.ie"
12-
}
13-
],
14-
"require": {
15-
"php": ">=5.3.2",
16-
"symfony/framework-bundle": "~2.1|~3.0"
17-
},
18-
"autoload": {
19-
"psr-0": {
20-
"Oh\\FormErrorLogBundle": ""
21-
}
22-
},
23-
"target-dir": "Oh/FormErrorLogBundle"
2+
"name": "lendable/form-error-log-bundle",
3+
"type": "symfony-bundle",
4+
"description": "Log form errors",
5+
"keywords": [
6+
"form",
7+
"Symfony2"
8+
],
9+
"homepage": "https://github.com/ollieLtd/OhFormErrorLogBundle",
10+
"license": "MIT",
11+
"authors": [
12+
{
13+
"name": "Ollie Harridge",
14+
"email": "code@oll.ie"
15+
}
16+
],
17+
"require": {
18+
"php": ">=5.3.2",
19+
"symfony/framework-standard-edition": "~2.1|~3.0"
20+
},
21+
"autoload": {
22+
"psr-0": {
23+
"Oh\\FormErrorLogBundle": ""
24+
}
25+
},
26+
"target-dir": "Oh/FormErrorLogBundle"
2427
}

0 commit comments

Comments
 (0)