1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Swm\Bundle\MailHookBundle\Controller; |
4
|
|
|
|
5
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method; |
6
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; |
7
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller; |
8
|
|
|
use Symfony\Component\HttpFoundation\Response; |
9
|
|
|
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @Route("/webhook") |
13
|
|
|
*/ |
14
|
|
|
class MailHookController extends Controller |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @Route("/{secretSalt}/{service}/catch", name="swm_mailhook_catcher_for_service") |
18
|
|
|
* @Method({"POST","GET"}) |
19
|
|
|
*/ |
20
|
|
View Code Duplication |
public function catcherAction($secretSalt, $service = null) |
|
|
|
|
21
|
|
|
{ |
22
|
|
|
// check if request is granted |
23
|
|
|
$this->checkSecret($secretSalt); |
24
|
|
|
|
25
|
|
|
$mailHookService = $this->get('swm.mail_hook.service.mail_hook'); |
26
|
|
|
|
27
|
|
|
// get hooks |
28
|
|
|
$hooks = $mailHookService->getHooksForService($service); |
29
|
|
|
|
30
|
|
|
$eventHydrator = $this->get('swm.mail_hook.hydrator.default'); |
31
|
|
|
|
32
|
|
|
foreach ($hooks as $hook) { |
33
|
|
|
$event = $eventHydrator->hydrate($hook, 'Swm\Bundle\MailHookBundle\Event\MailHookEvent'); |
34
|
|
|
$this->get('event_dispatcher')->dispatch($hook->getEventDispatched(), $event); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
return new Response(); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @Route("/{secretSalt}/{service}/catchuser", name="swm_mailhook_user_catcher_for_service") |
42
|
|
|
* @Method({"POST","GET"}) |
43
|
|
|
*/ |
44
|
|
View Code Duplication |
public function catchUserAction($secretSalt, $service = null) |
|
|
|
|
45
|
|
|
{ |
46
|
|
|
// check if request is granted |
47
|
|
|
$this->checkSecret($secretSalt); |
48
|
|
|
|
49
|
|
|
$mailHookService = $this->get('swm.mail_hook.service.mail_hook'); |
50
|
|
|
|
51
|
|
|
// get hooks |
52
|
|
|
$hooks = $mailHookService->getHooksForService($service); |
53
|
|
|
|
54
|
|
|
$eventHydrator = $this->get('swm.mail_hook.hydrator.fos_user'); |
55
|
|
|
|
56
|
|
|
foreach ($hooks as $hook) { |
57
|
|
|
$event = $eventHydrator->hydrate($hook, 'Swm\Bundle\MailHookBundle\Event\UserMailHookEvent'); |
58
|
|
|
$this->get('event_dispatcher')->dispatch($hook->getEventDispatched(), $event); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
return new Response(); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
private function checkSecret($secretSalt) |
65
|
|
|
{ |
66
|
|
|
if ($this->container->getParameter('swm_mailhook.secretsalt') !== $secretSalt) { |
67
|
|
|
throw new AccessDeniedHttpException("You are not welcome here"); |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.