. // }}} namespace Component\LeftPanel; use App\Core\Cache; use App\Core\DB; use App\Core\Event; use function App\Core\I18n\_m; use App\Core\Modules\Component; use App\Core\Router; use App\Entity\Actor; use App\Entity\Feed; use App\Util\Exception\ClientException; use App\Util\Exception\NotFoundException; use Component\LeftPanel\Controller as C; class LeftPanel extends Component { public function onAddRoute(Router $r): bool { $r->connect('edit_feeds', '/edit-feeds', C\EditFeeds::class); return Event::next; } /** * @throws \App\Util\Exception\DuplicateFoundException * @throws \App\Util\Exception\ServerException * @throws ClientException */ public function onAppendFeed(Actor $actor, string $title, string $route, array $route_params): bool { $cache_key = Feed::cacheKey($actor); $feeds = Feed::getFeeds($actor); $ordering = end($feeds)->getOrdering(); $url = Router::url($route, $route_params); try { $feed = DB::findOneBy('feed', ['actor_id' => $actor->getId(), 'url' => $url]); throw new ClientException(_m( 'Cannot add feed with url "{url}" because it already exists with title "{title}"', ['{url}' => $url, '{title}' => $feed->getTitle()], )); } catch (NotFoundException) { DB::persist(Feed::create([ 'actor_id' => $actor->getId(), 'url' => $url, 'route' => $route, 'title' => $title, 'ordering' => $ordering + 1, ])); DB::flush(); Cache::delete($cache_key); return Event::stop; } } }