Skip to content
This repository has been archived by the owner on Feb 7, 2019. It is now read-only.

Commit

Permalink
moved node related methods
Browse files Browse the repository at this point in the history
  • Loading branch information
doganoo committed Nov 3, 2018
1 parent f095bdf commit 441686d
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 47 deletions.
2 changes: 1 addition & 1 deletion appinfo/info.xml
Expand Up @@ -10,7 +10,7 @@
Design and Implementation of a Recommendation System Using Hybrid
Collaborative Filtering Techniques for the Nextcloud Platform
</description>
<version>1.0.2</version>
<version>1.0.3</version>

<licence>agpl</licence>
<author>Dogan Ucar</author>
Expand Down
4 changes: 2 additions & 2 deletions lib/Controller/RecommendationController.php
Expand Up @@ -25,7 +25,7 @@
use OCA\RecommendationAssistant\Db\Entity\Recommendation;
use OCA\RecommendationAssistant\Db\Mapper\RecommendationMapper;
use OCA\RecommendationAssistant\Log\Logger;
use OCA\RecommendationAssistant\Util\NodeUtil;
use OCA\RecommendationAssistant\Service\NodeService;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http\JSONResponse;
use OCP\Files\File;
Expand Down Expand Up @@ -88,7 +88,7 @@ public function index() {
foreach ($entities as $key => &$entity) {
$id = $entity->fileId;
/** @var File $node */
$node = NodeUtil::getFile($id, $this->userId);
$node = NodeService::getFile($id, $this->userId);
if (null === $node) {
unset($entities[$key]);
continue;
Expand Down
5 changes: 2 additions & 3 deletions lib/Db/ChangedFilesManager.php
Expand Up @@ -20,14 +20,13 @@
use OCA\RecommendationAssistant\Log\ConsoleLogger;
use OCA\RecommendationAssistant\Log\Logger;
use OCA\RecommendationAssistant\Objects\Item;
use OCA\RecommendationAssistant\Util\NodeUtil;
use OCA\RecommendationAssistant\Service\NodeService;
use OCA\RecommendationAssistant\Util\Util;
use OCP\Files\File;
use OCP\Files\InvalidPathException;
use OCP\Files\NotFoundException;
use OCP\IDBConnection;
use OCP\IUser;
use OCP\IUserManager;

/**
* Class that servers as a interface to the datastorage that the latest changes
Expand Down Expand Up @@ -85,7 +84,7 @@ public function allFiles(): ArrayList {
$fileId = $row[DbConstants::TB_CFL_FILE_ID];
$userId = $row[DbConstants::TB_CFL_USER_ID];
$createTs = $row[DbConstants::TB_CFL_CREATION_TS];
$file = NodeUtil::getFile(
$file = NodeService::getFile(
$fileId
, $userId
);
Expand Down
43 changes: 18 additions & 25 deletions lib/Hook/FileHook.php
Expand Up @@ -18,10 +18,10 @@

use OCA\RecommendationAssistant\Db\ChangedFilesManager;
use OCA\RecommendationAssistant\Log\Logger;
use OCA\RecommendationAssistant\Service\NodeService;
use OCP\Files\File;
use OCP\Files\IRootFolder;
use OCP\Files\Node;
use OCP\Files\NotFoundException;
use OCP\IRequest;
use OCP\IUser;
use OCP\IUserSession;
Expand Down Expand Up @@ -57,6 +57,13 @@ class FileHook {
*/
private $changedFilesManager = null;

/**
* @var NodeService $nodeService the service for all node
* related stuff
*
*/
private $nodeService = null;

/**
* Class constructor gets multiple instances injected
*
Expand All @@ -67,19 +74,23 @@ class FileHook {
* @param ChangedFilesManager $changedFilesManager database manager class
* to query changed files
*
* @param NodeService $nodeService
* @package OCA\RecommendationAssistant\AppInfo
* @since 1.0.0
*/
public function __construct(
IUserSession $userSession,
IRequest $request,
IRootFolder $rootFolder,
ChangedFilesManager $changedFilesManager
IUserSession $userSession
, IRequest $request
, IRootFolder $rootFolder
, ChangedFilesManager $changedFilesManager
, NodeService $nodeService

) {
$this->userSession = $userSession;
$this->request = $request;
$this->rootFolder = $rootFolder;
$this->changedFilesManager = $changedFilesManager;
$this->nodeService = $nodeService;
}

/**
Expand Down Expand Up @@ -132,7 +143,7 @@ public function run($parameters): bool {
return false;
}
/** @var Node $node */
$node = $this->getNode($path);
$node = $this->nodeService->getNode($path);
if (null === $node) {
Logger::info("node returned null. Cannot process file");
return false;
Expand All @@ -151,23 +162,5 @@ public function run($parameters): bool {
return false;
}

/**
* returns the Node instance that correspondents to $path
*
* @param string $path the path
*
* @return null|Node the node that is requested or null, if an error occures
* @since 1.0.0
*/
private function getNode($path) {
$node = null;
try {
$currentUserId = $this->userSession->getUser()->getUID();
$userFolder = $this->rootFolder->getUserFolder($currentUserId);
$node = $userFolder->get($path);
} catch (NotFoundException $e) {
Logger::error($e->getMessage());
}
return $node;
}

}
64 changes: 50 additions & 14 deletions lib/Util/NodeUtil.php → lib/Service/NodeService.php 100755 → 100644
@@ -1,6 +1,6 @@
<?php
/**
* @copyright Copyright (c) 2017, Dogan Ucar (dogan@dogan-ucar.de)
* @copyright Copyright (c) 2018, Dogan Ucar (dogan@dogan-ucar.de)
* @license GNU AGPL version 3 or any later version
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
Expand All @@ -14,26 +14,40 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

namespace OCA\RecommendationAssistant\Util;

namespace OCA\RecommendationAssistant\Service;


use OCA\RecommendationAssistant\Log\Logger;
use OCP\Files\File;
use OCP\Files\IRootFolder;
use OCP\Files\Node;
use OCP\Files\NotFoundException;
use OCP\IUserSession;

/**
* NodeUtil is a utility class for all \OCP\Files\Node objects. It defines
* all helper methods that are relevant for nodes. This class is not instantiable
* because all methods are static.
* Class NodeService
*
* @package OCA\RecommendationAssistant\Util
* @since 1.0.0
* @package OCA\RecommendationAssistant\Service
*/
class NodeUtil{
class NodeService {
/** @var IUserSession $userSession */
private $userSession = null;
/** @var IRootFolder $rootFolder */
private $rootFolder = null;

/**
* class constructor is private because all methods are public static.
* NodeService constructor.
*
* @since 1.0.0
* @param IUserSession $userSession
* @param IRootFolder $rootFolder
*/
private function __construct(){
public function __construct(
IUserSession $userSession
, IRootFolder $rootFolder
) {
$this->userSession = $userSession;
$this->rootFolder = $rootFolder;
}

/**
Expand All @@ -45,19 +59,41 @@ private function __construct(){
*
* @return null|File
* @since 1.0.0
*
* TODO make none static
*/
public static function getFile(string $nodeId, string $userId){
public static function getFile(string $nodeId, string $userId) {
/** @var \OCP\Files\Folder $rootFolder */
$userFolder = \OC::$server->getRootFolder()->getUserFolder($userId);
/** @var \OCP\Files\Node[] $nodeArray */
$nodeArray = $userFolder->getById($nodeId);
/** @var Node $return */
$return = empty($nodeArray[0]) ? null : $nodeArray[0];
if($return !== null && $return instanceof File){
if ($return !== null && $return instanceof File) {
/** @var File $return */
return $return;
} else{
} else {
return null;
}
}

/**
* returns the Node instance that correspondents to $path
*
* @param string $path the path
*
* @return null|Node the node that is requested or null, if an error occures
* @since 1.0.0
*/
public function getNode($path) {
$node = null;
try {
$currentUserId = $this->userSession->getUser()->getUID();
$userFolder = $this->rootFolder->getUserFolder($currentUserId);
$node = $userFolder->get($path);
} catch (NotFoundException $e) {
Logger::error($e->getMessage());
}
return $node;
}
}
3 changes: 1 addition & 2 deletions lib/Service/UserService.php
Expand Up @@ -23,7 +23,6 @@
namespace OCA\RecommendationAssistant\Service;


use OCA\RecommendationAssistant\Util\NodeUtil;
use OCP\Files\IRootFolder;
use OCP\IUserManager;

Expand Down Expand Up @@ -54,7 +53,7 @@ public function __construct(IUserManager $userManager, IRootFolder $rootFolder)
*/
public function hasAccess($uid, $fileId): bool {
$nodes = $this->rootFolder->getUserFolder($uid)->getById($fileId);
NodeUtil::getFile($fileId,$uid);
NodeService::getFile($fileId, $uid);
return null !== $nodes && \count($nodes) >= 1;
}

Expand Down

0 comments on commit 441686d

Please sign in to comment.