Skip to content

Commit 0e699e0

Browse files
authored
Merge pull request #235 from Novactive/feat-menu-load-exception
feat: add exception throwing to menu service
2 parents ed4ae18 + 7cb4877 commit 0e699e0

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Novactive\EzMenuManager\Exception;
6+
7+
use Exception;
8+
use Throwable;
9+
10+
class MenuNotFoundException extends Exception
11+
{
12+
public function __construct($identifier, Throwable $previous = null)
13+
{
14+
$message = sprintf("Could not find menu with identifier '%s'", $identifier);
15+
parent::__construct($message, 404, $previous);
16+
}
17+
}

components/MenuManagerBundle/src/lib/Service/MenuService.php

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Doctrine\ORM\EntityManagerInterface;
1818
use Ibexa\Contracts\Core\Repository\LocationService;
1919
use Ibexa\Contracts\Core\Repository\Values\Content\Location;
20+
use Novactive\EzMenuManager\Exception\MenuNotFoundException;
2021
use Novactive\EzMenuManager\MenuItem\MenuItemTypeRegistry;
2122
use Novactive\EzMenuManagerBundle\Entity\Menu;
2223
use Novactive\EzMenuManagerBundle\Entity\MenuItem;
@@ -80,18 +81,32 @@ public function getLocationMenuItemsInMenu(Location $location, Menu $menu)
8081
/**
8182
* @param $menuId
8283
*
84+
* @throws \Novactive\EzMenuManager\Exception\MenuNotFoundException
85+
*
8386
* @return Menu|object|null
8487
*/
8588
public function loadMenu($menuId)
8689
{
87-
return $this->em->getRepository(Menu::class)->find($menuId);
90+
$menu = $this->em->getRepository(Menu::class)->find($menuId);
91+
if (!$menu) {
92+
throw new MenuNotFoundException($menuId);
93+
}
94+
95+
return $menu;
8896
}
8997

9098
/**
99+
* @throws \Novactive\EzMenuManager\Exception\MenuNotFoundException
100+
*
91101
* @return Menu|object|null
92102
*/
93103
public function loadMenuByRemoteId(string $remoteId)
94104
{
95-
return $this->em->getRepository(Menu::class)->findOneBy(['remoteId' => $remoteId]);
105+
$menu = $this->em->getRepository(Menu::class)->findOneBy(['remoteId' => $remoteId]);
106+
if (!$menu) {
107+
throw new MenuNotFoundException($remoteId);
108+
}
109+
110+
return $menu;
96111
}
97112
}

0 commit comments

Comments
 (0)