Controller::dispatchEntityEventRunner()   A
last analyzed

Complexity

Conditions 5
Paths 8

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.4888
c 0
b 0
f 0
cc 5
nc 8
nop 3
1
<?php
2
3
namespace Vivait\BootstrapBundle\Controller;
4
5
use Doctrine\ORM\UnitOfWork;
6
use Symfony\Component\HttpFoundation\RedirectResponse;
7
use Symfony\Component\HttpFoundation\Request;
8
use Symfony\Component\HttpFoundation\Response;
9
use Viva\AuthBundle\Entity\User;
10
use Vivait\Common\Event\EntityEvent;
11
12
class Controller extends \Symfony\Bundle\FrameworkBundle\Controller\Controller
0 ignored issues
show
Deprecated Code introduced by
The class Symfony\Bundle\Framework...e\Controller\Controller has been deprecated with message: since Symfony 4.2, use {@see AbstractController} instead.

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
13
{
14
	
15
	/**
16
	 * @param Request $request
17
	 * @param bool $avoidLoop Avoid redirecting back to the current page
18
     *
19
	 * @return RedirectResponse|Response
20
	 */
21
	protected function redirectBack(Request $request, $avoidLoop = false)
22
    {
23
		$current = $request->attributes->get('_route');
24
		$parent = $request->query->get('parent', $request->request->get('parent', $request->headers->get('referer')));
25
26
		if ( ! $parent || ($avoidLoop && $parent == $current)) {
27
			$parent = $this->generateUrl('viva_app_homepage');
28
		}
29
30
		return $this->redirectTo($request, $parent);
31
	}
32
33
    /**
34
     * @param Request $request
35
     * @param string  $to
36
     *
37
     * @return mixed
38
     */
39
    protected function redirectTo(Request $request, $to)
40
    {
41
		if ($request->headers->get('X-REQUESTED-WITH') == 'XMLHttpRequest') {
42
			return $this->render('VivaitBootstrapBundle:Default:redirect.html.twig', array(
43
				'redirect' => $to
44
			));
45
		}
46
47
		return $this->redirect($to);
48
	}
49
50
    /**
51
     * @param mixed $events
52
     */
53
    protected function dispatchEntityEvent($events)
54
    {
55
		$em         = $this->getDoctrine()->getManager();
56
		$dispatcher = $this->get('event_dispatcher');
57
58
		/**
59
         * @var $uow \Doctrine\ORM\UnitOfWork
60
         */
61
		$uow = $em->getUnitOfWork();
62
		$uow->computeChangeSets();
63
64
		if (is_array($events)) {
65
			// Dispatch each event
66
			array_walk($events, function(EntityEvent $event) use ($uow, $dispatcher) {
67
				$this->dispatchEntityEventRunner($event, $uow, $dispatcher);
68
			});
69
		} else {
70
			$this->dispatchEntityEventRunner($events, $uow, $dispatcher);
71
		}
72
	}
73
74
    /**
75
     * @param EntityEvent $event
76
     * @param UnitOfWork  $uow
77
     * @param null        $dispatcher
78
     */
79
	protected function dispatchEntityEventRunner(EntityEvent $event, UnitOfWork $uow, $dispatcher = null)
80
    {
81
		if ( ! $dispatcher) {
82
			$dispatcher = $this->get('event_dispatcher');
83
		}
84
85
		if ($uow->isScheduledForUpdate($event->getEntity())) {
86
			$dispatcher->dispatch($event::EVENT_ENTITY_MODIFIED, $event);
87
		} else if ($uow->isScheduledForInsert($event->getEntity())) {
88
			$dispatcher->dispatch($event::EVENT_ENTITY_CREATED, $event);
89
		} else if ($uow->isScheduledForDelete($event->getEntity())) {
90
			$dispatcher->dispatch($event::EVENT_ENTITY_DELETED, $event);
91
		}
92
	}
93
94
    /**
95
     * @return User
0 ignored issues
show
Documentation introduced by
Should the return type not be User|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
96
     */
97
    protected function getUser()
98
    {
99
        return parent::getUser();
100
    }
101
}
102