22
33namespace 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 ;
69use Symfony \Component \DependencyInjection \Exception \InvalidArgumentException ;
10+ use Symfony \Component \EventDispatcher \EventDispatcherInterface ;
711
812class 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}
0 commit comments