-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPageService.php
More file actions
127 lines (115 loc) · 3.49 KB
/
PageService.php
File metadata and controls
127 lines (115 loc) · 3.49 KB
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
<?php
/**
* Definition of class Pages
*
* @copyright 2014-today Justso GmbH, Frankfurt, Germany
* @author j.schirrmacher@justso.de
*
* @package justso\service
*/
namespace justso\justtexts;
use justso\justapi\InvalidParameterException;
use justso\justapi\RestService;
use justso\justapi\SystemEnvironmentInterface;
/**
* Handles requests regarding generated website pages.
*
* @package justso\service
*/
class PageService extends RestService
{
/**
* @var PageList
*/
private $pageList;
/**
* Initializes the service.
*
* @param SystemEnvironmentInterface $environment
*/
public function __construct(SystemEnvironmentInterface $environment)
{
parent::__construct($environment);
$this->pageList = new PageList($environment);
}
/**
* Sets the pageList.
*
* @param PageList $pageList
*/
public function setPageList(PageList $pageList)
{
$this->pageList = $pageList;
}
/**
* Yields a list of pages or a single page, if a page name is specified in the service name.
*/
public function getAction()
{
$id = $this->getPageId();
if ($id !== null) {
$result = $this->pageList->getPage($id)->getJSON();
} else {
$result = array();
foreach ($this->pageList->getPages() as $pageName) {
$result[] = $this->pageList->getPage($pageName)->getJSON();
}
}
$this->environment->sendJSONResult($result);
}
/**
* Creates a new page
*/
public function postAction()
{
$request = $this->environment->getRequestHelper();
$id = $request->getIdentifierParam('name');
try {
$this->pageList->getPage($id);
} catch (InvalidParameterException $e) {
$page = $this->pageList->addPageFromRequest($id, $request);
$this->environment->sendJSONResult($page->getJSON());
return;
}
throw new InvalidParameterException("Page already exists");
}
public function putAction()
{
$request = $this->environment->getRequestHelper();
$id = $this->getPageId();
$this->pageList->getPage($id);
$newName = $request->getIdentifierParam('name');
if ($id != $newName) {
if ($this->pageList->getPage($newName)) {
throw new InvalidParameterException("Page already exists");
}
$this->pageList->renamePage($id, $newName);
}
$page = $this->pageList->changePageFromRequest($newName, $request);
$this->environment->sendJSONResult($page->getJSON());
}
public function deleteAction()
{
$id = $this->getPageId();
$this->pageList->deletePage($id);
$arguments = [$this->environment, $id];
/** @var \justso\justtexts\TextInterface $pageTexts */
$pageTexts = $this->environment->getDIC()->get('\justso\justtexts\Text', $arguments);
$pageTexts->removeAll();
$this->environment->sendJSONResult('ok');
}
/**
* Checks if the page is contained in service name or specified in parameter 'id' and returns the value then.
*
* @return mixed
*/
private function getPageId()
{
if (preg_match('/\/page\/(\w+)$/', $this->name, $matches)) {
$id = $matches[1];
} else {
$id = $this->environment->getRequestHelper()->getIdentifierParam('id', null, true);
}
return $id;
}
}