/
kev
/
Nextcloud_File-Changes-Tracker
Обзор
Документация
Войти
/
kev
/
Nextcloud_File-Changes-Tracker
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
lib/Service/FolderConfigService.php
162 строки
5 KB
Evgeny Konovalov
first_commit
16 фев 2026, 17:45
16 фев 2026, 17:45
476f06c
Код
Авторство
О чём код?
<?php declare(strict_types=1); namespace OCA\FileChangesTracker\Service; use OCA\FileChangesTracker\Db\FolderConfig; use OCA\FileChangesTracker\Db\FolderConfigMapper; use OCP\AppFramework\Db\DoesNotExistException; use OCP\Files\IRootFolder; use OCP\Files\NotFoundException; use OCP\IUserSession; /** * Сервис для работы с конфигурацией отслеживаемых папок */ class FolderConfigService { private FolderConfigMapper $mapper; private IRootFolder $rootFolder; private IUserSession $userSession; public function __construct( FolderConfigMapper $mapper, IRootFolder $rootFolder, IUserSession $userSession ) { $this->mapper = $mapper; $this->rootFolder = $rootFolder; $this->userSession = $userSession; } /** * Получить все конфигурации */ public function findAll(): array { return $this->mapper->findAll(); } /** * Получить конфигурацию по ID */ public function find(int $id): ?FolderConfig { try { return $this->mapper->find($id); } catch (DoesNotExistException $e) { return null; } } /** * Получить конфигурации для конкретного ответственного */ public function findByResponsible(string $responsibleId, string $responsibleType): array { return $this->mapper->findByResponsible($responsibleId, $responsibleType); } /** * Создать новую конфигурацию */ public function create( string $folderPath, string $responsibleType, string $responsibleId, bool $notificationsEnabled, string $createdBy ): FolderConfig { // Проверяем существование папки и получаем её ID $folderId = $this->getFolderIdByPath($folderPath); // Проверяем, не отслеживается ли уже эта папка $existing = $this->mapper->findByFolderPath($folderPath); if ($existing !== null) { throw new \Exception('Эта папка уже отслеживается'); } $config = new FolderConfig(); $config->setFolderPath($folderPath); $config->setFolderId($folderId); $config->setResponsibleType($responsibleType); $config->setResponsibleId($responsibleId); $config->setNotificationsEnabled($notificationsEnabled); $config->setCreatedBy($createdBy); $now = date('Y-m-d H:i:s'); $config->setCreatedAt($now); $config->setUpdatedAt($now); return $this->mapper->insert($config); } /** * Обновить конфигурацию */ public function update( int $id, string $folderPath, string $responsibleType, string $responsibleId, bool $notificationsEnabled ): FolderConfig { $config = $this->mapper->find($id); // Если путь изменился, проверяем папку if ($config->getFolderPath() !== $folderPath) { $folderId = $this->getFolderIdByPath($folderPath); $config->setFolderPath($folderPath); $config->setFolderId($folderId); } $config->setResponsibleType($responsibleType); $config->setResponsibleId($responsibleId); $config->setNotificationsEnabled($notificationsEnabled); $config->setUpdatedAt(date('Y-m-d H:i:s')); return $this->mapper->update($config); } /** * Удалить конфигурацию */ public function delete(int $id): void { $config = $this->mapper->find($id); $this->mapper->delete($config); } /** * Найти конфигурации для файла по его пути */ public function findMatchingConfigsForFile(string $filePath): array { return $this->mapper->findMatchingConfigs($filePath); } /** * Получить ID папки по пути */ private function getFolderIdByPath(string $folderPath): ?int { try { // Получаем текущего пользователя $user = $this->userSession->getUser(); if (!$user) { throw new \Exception('Пользователь не авторизован'); } // Убираем начальный слэш если есть $path = ltrim($folderPath, '/'); // Ищем папку в файловой системе пользователя $userFolder = $this->rootFolder->getUserFolder($user->getUID()); // Если путь пустой, возвращаем ID корневой папки пользователя if (empty($path)) { return $userFolder->getId(); } $folder = $userFolder->get($path); return $folder->getId(); } catch (NotFoundException $e) { throw new \Exception('Папка не найдена: ' . $folderPath); } } }