Skip to content

Commit 65d99c1

Browse files
Modelpage delete and update fn throws error instead of returning bool
1 parent 382da23 commit 65d99c1

13 files changed

+119
-99
lines changed

app/class/Controller.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -172,8 +172,11 @@ public function generate(string $route, array $params = [], string $get = ''): s
172172
/**
173173
* Redirect to URL and send 302 code
174174
* @param string $url to redirect to
175+
*
176+
* @throws void Indicate to PHPStan that no exception is
177+
* thrown despite the use of `never` return type
175178
*/
176-
public function redirect(string $url): void
179+
public function redirect(string $url): never
177180
{
178181
header('Location: ' . $url);
179182
exit;
@@ -182,10 +185,11 @@ public function redirect(string $url): void
182185
/**
183186
* @param array<string, mixed> $vars
184187
* @param array<string, mixed> $gets
188+
*
189+
* @throws InvalidArgumentException
185190
*/
186-
public function routedirect(string $route, array $vars = [], array $gets = []): void
191+
public function routedirect(string $route, array $vars = [], array $gets = []): never
187192
{
188-
189193
$get = empty($gets) ? "" : "?" . http_build_query($gets);
190194
$this->redirect($this->generate($route, $vars, $get));
191195
}

app/class/Controllerapipage.php

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
namespace Wcms;
44

55
use RuntimeException;
6+
use Wcms\Exception\Databaseexception;
7+
use Wcms\Exception\Filesystemexception;
68

79
class Controllerapipage extends Controllerapi
810
{
@@ -79,13 +81,15 @@ public function update(string $page): void
7981
$this->page->hydrate($datas);
8082

8183
if ($this->page->datemodif() == $oldpage->datemodif()) {
82-
$this->page->updateedited();
83-
if ($this->pagemanager->update($this->page)) {
84+
try {
85+
$this->page->updateedited();
86+
$this->pagemanager->update($this->page);
8487
http_response_code(200);
8588
header('Content-type: application/json; charset=utf-8');
8689
echo json_encode($this->page->dry(), JSON_PRETTY_PRINT);
87-
} else {
88-
$this->shortresponse(500, "Error while trying to save page in database");
90+
} catch (RuntimeException $e) {
91+
Logger::error("Error while trying to update Page '$page' through API: " . $e->getMessage());
92+
$this->shortresponse(500, $e->getMessage());
8993
}
9094
} else {
9195
$this->shortresponse(409, "Conflict : A more recent version of the page is stored in the database");
@@ -144,10 +148,17 @@ public function delete(string $page): void
144148
{
145149
if ($this->importpage($page)) {
146150
if ($this->user->issupereditor() || $this->page->authors() === [$this->user->id()]) {
147-
if ($this->pagemanager->delete($this->page)) {
148-
http_response_code(200);
149-
} else {
150-
http_response_code(500);
151+
try {
152+
$this->pagemanager->delete($this->page);
153+
$user = $this->user->id();
154+
Logger::info("User '$user' uccessfully deleted Page '$page'");
155+
$this->shortresponse(200);
156+
} catch (Filesystemexception $e) {
157+
Logger::warning("Error while deleting Page '$page'" . $e->getMessage());
158+
$this->shortresponse(200, $e->getMessage());
159+
} catch (Databaseexception $e) {
160+
Logger::error("Could not delete Page $page: " . $e->getMessage());
161+
$this->shortresponse(500, $e->getMessage());
151162
}
152163
} else {
153164
http_response_code(401);

app/class/Controllerhome.php

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use AltoRouter;
66
use RuntimeException;
77
use Wcms\Exception\Databaseexception;
8+
use Wcms\Exception\Filesystemexception;
89
use Wcms\Exception\Filesystemexception\Notfoundexception;
910

1011
class Controllerhome extends Controller
@@ -224,11 +225,9 @@ public function search(): void
224225
switch ($_POST['action']) {
225226
case 'read':
226227
$this->routedirect('pageread', ['page' => $_POST['id']]);
227-
break;
228228

229229
case 'edit':
230230
$this->routedirect('pageedit', ['page' => $_POST['id']]);
231-
break;
232231
}
233232
}
234233
} else {
@@ -362,11 +361,11 @@ public function multirender(): void
362361
$this->router,
363362
Config::urlchecker() ? new Serviceurlchecker(0) : 0
364363
);
365-
if ($this->pagemanager->update($page)) {
366-
$count++;
367-
}
364+
$this->pagemanager->update($page);
365+
$count++;
368366
} catch (RuntimeException $e) {
369-
Logger::errorex($e);
367+
$p = $page->id();
368+
Logger::error("Error while trying to render page $p: " . $e->getMessage());
370369
}
371370
}
372371
$this->sendstatflashmessage($count, $total, 'pages have been rendered');
@@ -378,12 +377,25 @@ public function multidelete(): void
378377
$pagelist = $_POST['pagesid'] ?? [];
379378
$total = count($pagelist);
380379
$count = 0;
380+
$unlinkerror = false;
381381
foreach ($pagelist as $id) {
382-
if ($this->pagemanager->delete(new Pagev2(['id' => $id]))) {
382+
try {
383+
$this->pagemanager->delete(new Pagev2(['id' => $id]));
384+
$user = $this->user->id();
385+
Logger::info("User '$user' successfully deleted Page '$id'");
386+
$count++;
387+
} catch (Filesystemexception $e) {
388+
Logger::warning("Error while deleting Page '$id'" . $e->getMessage());
389+
$unlinkerror = true;
383390
$count++;
391+
} catch (Databaseexception $e) {
392+
Logger::error("Could not delete Page $id: " . $e->getMessage());
384393
}
385394
}
386395
$this->sendstatflashmessage($count, $total, 'pages have been deleted');
396+
if ($unlinkerror) {
397+
$this->sendflashmessage('Could not delete some render files', self::FLASH_WARNING);
398+
}
387399
} else {
388400
$this->sendflashmessage('Confirm delete has not been cheked', self::FLASH_WARNING);
389401
}

app/class/Controllerpage.php

Lines changed: 37 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use DateTimeImmutable;
88
use DateTimeZone;
99
use RuntimeException;
10+
use Wcms\Exception\Databaseexception;
1011
use Wcms\Exception\Filesystemexception;
1112

1213
class Controllerpage extends Controller
@@ -46,6 +47,8 @@ protected function setpage(string $id, string $route): void
4647
* Try yo import by multiple way the called page
4748
*
4849
* @return bool If a page is found and stored in `$this->page`
50+
*
51+
* @todo maybe throw error instead of logging to let the upper code choose what to do
4952
*/
5053
protected function importpage(): bool
5154
{
@@ -59,7 +62,7 @@ protected function importpage(): bool
5962
return true;
6063
}
6164
} catch (RuntimeException $e) {
62-
Logger::errorex($e);
65+
Logger::warningex($e);
6366
return false;
6467
}
6568
}
@@ -191,18 +194,19 @@ public function read(string $page): void
191194
}
192195

193196
$this->pagemanager->templaterender($this->page, $this->router);
197+
198+
$this->page->adddisplaycount();
199+
if ($this->user->isvisitor()) {
200+
$this->page->addvisitcount();
201+
}
202+
$this->pagemanager->update($this->page);
194203
} catch (RuntimeException $e) {
195-
$msg = $e->getMessage();
196-
Logger::error("Error while trying to read page '$page': $msg");
204+
Logger::error("Error while trying to read page '$page':" . $e->getMessage());
197205
}
198206

199-
$this->page->adddisplaycount();
200-
if ($this->user->isvisitor()) {
201-
$this->page->addvisitcount();
202-
}
203-
$this->pagemanager->update($this->page);
204207

205208
// redirection using Location and 302
209+
// If redirecting to a dead page or an invalid URL, redirection is canceled
206210
if (!empty($this->page->redirection()) && $this->page->refresh() === 0 && $this->page->sleep() === 0) {
207211
try {
208212
if (Model::idcheck($this->page->redirection())) {
@@ -212,8 +216,7 @@ public function read(string $page): void
212216
$this->redirect($url);
213217
}
214218
} catch (RuntimeException $e) {
215-
// This mean the redirect field did'nt contain an existing page or URL
216-
// TODO : send syntax error to editor
219+
Logger::warning("Page '$page' redirect to existing page or a valid URL");
217220
}
218221
}
219222

@@ -481,16 +484,28 @@ public function delete(string $page): void
481484
}
482485
}
483486

487+
/**
488+
* @todo maybe show an error view if deletion failed
489+
*/
484490
public function confirmdelete(string $page): void
485491
{
486492
$this->setpage($page, 'pageconfirmdelete');
487-
if ($this->user->iseditor() && $this->importpage()) {
488-
$this->pagemanager->delete($this->page);
489-
$this->routedirect('pageread', ['page' => $this->page->id()]);
490-
} else {
493+
if (!$this->importpage() || !$this->candelete($this->page)) {
491494
http_response_code(403);
492495
$this->showtemplate('forbidden', ['route' => 'pageread', 'id' => $this->page->id()]);
496+
exit;
493497
}
498+
499+
try {
500+
$this->pagemanager->delete($this->page);
501+
$user = $this->user->id();
502+
Logger::info("User '$user' uccessfully deleted Page '$page'");
503+
} catch (Filesystemexception $e) {
504+
Logger::warning("Error while deleting Page '$page'" . $e->getMessage());
505+
} catch (Databaseexception $e) {
506+
Logger::error("Could not delete Page $page: " . $e->getMessage());
507+
}
508+
$this->routedirect('pageread', ['page' => $this->page->id()]);
494509
}
495510

496511
public function duplicate(string $page, string $duplicate): void
@@ -547,9 +562,14 @@ public function update(string $page): void
547562
$_SESSION['pageupdate']['id'] = $this->page->id();
548563
$this->routedirect('pageedit', ['page' => $this->page->id()]);
549564
} else {
550-
$this->page->updateedited();
551-
552-
$this->pagemanager->update($this->page);
565+
try {
566+
$this->page->updateedited();
567+
$this->pagemanager->update($this->page);
568+
$this->sendflashmessage('Page succesfully updated', self::FLASH_SUCCESS);
569+
} catch (RuntimeException $e) {
570+
$this->sendflashmessage('Error while trying to update', self::FLASH_ERROR);
571+
Logger::error("Error while trying to update Page '$page': " . $e->getMessage());
572+
}
553573
}
554574
} else {
555575
// If the editor session finished during the editing, let's try to reconnect to save the editing
@@ -575,8 +595,6 @@ public function pagepermanentredirect(string $page): void
575595

576596
/**
577597
* Send a `404` HTTP code and display 'Command not found'
578-
*
579-
* @todo use a dedicated view and suggest a list of W URL based command.
580598
*/
581599
public function commandnotfound(string $page, string $command): void
582600
{

app/class/Controllerrandom.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,6 @@ public function direct(): void
2828
} catch (RuntimeException $e) {
2929
$message = 'Origin page does not exist';
3030
}
31-
32-
if (isset($message)) {
33-
$this->showtemplate('alertrandom', ['message' => $message]);
34-
}
31+
$this->showtemplate('alertrandom', ['message' => $message]);
3532
}
3633
}

app/class/Controlleruser.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,13 +81,11 @@ public function edit(): void
8181
$this->usermanager->delete($user);
8282
$this->sendflashmessage('User successfully deleted', self::FLASH_SUCCESS);
8383
$this->routedirect('user');
84-
break;
8584

8685
case 'update':
8786
$this->update($_POST);
8887
$this->sendflashmessage('User successfully updated', self::FLASH_SUCCESS);
8988
$this->routedirect('user');
90-
break;
9189
}
9290
} catch (RuntimeException $e) {
9391
$this->sendflashmessage('Error : ' . $e->getMessage(), self::FLASH_ERROR);

app/class/Controllerworkspace.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,9 @@ public function update(): void
2323

2424
case 'home':
2525
$this->routedirect('home');
26-
break;
2726

2827
case 'media':
2928
$this->routedirect('media');
30-
break;
3129
}
3230
}
3331
}

app/class/Modelbookmark.php

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -143,10 +143,7 @@ public function add(Bookmark $bookmark): void
143143
}
144144
$bookmarkdata = new Document($bookmark->dry());
145145
$bookmarkdata->setId($bookmark->id());
146-
$success = $this->storedoc($bookmarkdata);
147-
if (!$success) {
148-
throw new Databaseexception("Error while adding Bookmark to database");
149-
}
146+
$this->storedoc($bookmarkdata);
150147
}
151148

152149
/**
@@ -170,10 +167,9 @@ public function delete(Bookmark $bookmark): void
170167

171168
/**
172169
* @param Bookmark $bookmark Bookmark to update
173-
* @return Bookmark updated Bookmark
174170
* @throws RuntimeException if Bookmark does not exist or if an error occured at database level
175171
*/
176-
public function update(Bookmark $bookmark): Bookmark
172+
public function update(Bookmark $bookmark): void
177173
{
178174
$oldbookmark = $this->get($bookmark);
179175
$bookmark->setuser($oldbookmark->user());
@@ -185,11 +181,7 @@ public function update(Bookmark $bookmark): Bookmark
185181

186182
$bookmarkdata = new Document($bookmark->dry());
187183
$bookmarkdata->setId($bookmark->id());
188-
$success = $this->updatedoc($bookmarkdata);
189-
if (!$success) {
190-
throw new RuntimeException("Error while updating Bookmark to database");
191-
}
192-
return $bookmark;
184+
$this->updatedoc($bookmarkdata);
193185
}
194186

195187
/**

0 commit comments

Comments
 (0)