-
Notifications
You must be signed in to change notification settings - Fork 2
/
AbstractController.php
107 lines (91 loc) · 3.25 KB
/
AbstractController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
<?php
/*
* This file is part of the edm-bundle package.
*
* (c) 2017 WEBEWEB
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace WBW\Bundle\EDMBundle\Controller;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Throwable;
use WBW\Bundle\BootstrapBundle\Controller\AbstractController as BaseController;
use WBW\Bundle\EDMBundle\Entity\Document;
use WBW\Bundle\EDMBundle\Event\DocumentEvent;
use WBW\Bundle\EDMBundle\Model\DocumentInterface;
use WBW\Bundle\EDMBundle\WBWEDMBundle;
use WBW\Library\Symfony\Response\SimpleJsonResponseData;
use WBW\Library\Symfony\Response\SimpleJsonResponseDataInterface;
/**
* Abstract controller.
*
* @author webeweb <https://github.com/webeweb>
* @package WBW\Bundle\EDMBundle\Controller
* @abstract
*/
abstract class AbstractController extends BaseController {
/**
* Build a redirect route.
*
* @param DocumentInterface $document The document.
* @return array Returns the redirect route.
*/
protected function buildRedirectRoute(DocumentInterface $document): array {
return [
"wbw_edm_document_index",
[
"id" => null === $document->getParent() ? null : $document->getParent()->getId(),
],
];
}
/**
* Dispatch an event.
*
* @param string $eventName The event name.
* @param DocumentInterface $document The document.
* @return DocumentEvent|null Returns the document event.
* @throws Throwable Throws an exception if an error occurs.
*/
protected function dispatchDocumentEvent(string $eventName, DocumentInterface $document): ?DocumentEvent {
return $this->dispatchEvent($eventName, new DocumentEvent($eventName, $document));
}
/**
* Find a document.
*
* @param int|null $id The document.
* @param bool $ex Throws exception ?
* @return DocumentInterface|null Returns the document.
* @throws Throwable Throws an exception if an error occurs.
*/
protected function findDocument(?int $id, bool $ex): ?DocumentInterface {
$document = $this->getEntityManager()->getRepository(Document::class)->find($id);
if (null === $document && true === $ex) {
throw new NotFoundHttpException();
}
return $document;
}
/**
* Prepare an action response.
*
* @param int $status The status.
* @param string $notify The notify.
* @return SimpleJsonResponseDataInterface Returns the action response.
* @throws Throwable Throws an exception if an error occurs.
*/
protected function prepareActionResponse(int $status, string $notify): SimpleJsonResponseDataInterface {
$response = new SimpleJsonResponseData();
$response->setStatus($status);
$response->setNotify($this->translate($notify));
return $response;
}
/**
* {@inheritdoc}
*/
protected function translate(string $id, array $parameters = [], string $domain = null, string $locale = null): string {
if (null === $domain) {
$domain = WBWEDMBundle::getTranslationDomain();
}
return parent::translate($id, $parameters, $domain, $locale);
}
}