Skip to content

Commit

Permalink
feature(php): Display the exception details
Browse files Browse the repository at this point in the history
Now, we display the exception details about the error occ command
(create and import).

Signed-off-by: Baptiste Fotia <[email protected]>
  • Loading branch information
zak39 committed Oct 27, 2023
1 parent 0ed8c17 commit 85ef0df
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 18 deletions.
49 changes: 44 additions & 5 deletions lib/Commands/Create.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
use Symfony\Component\Console\Output\OutputInterface;

class Create extends Command {

public const OUTPUT_FORMAT_PLAIN = 'plain';
public const OUTPUT_FORMAT_JSON_PRETTY = 'json_pretty';

public function __construct(private SpaceManager $spaceManager,
private AdminGroup $adminGroup,
private UserPresenceChecker $userChecker,
Expand All @@ -22,11 +26,25 @@ public function __construct(private SpaceManager $spaceManager,
}

protected function configure(): void {
$this
->addOption(
'output',
null,
InputOption::VALUE_OPTIONAL,
'Output format (plain, json or json_pretty, default is plain)',
self::OUTPUT_FORMAT_PLAIN
);

$this
->setName('workspace:create')
->setDescription('This command allows you to create a workspace')
->addArgument('name', InputArgument::REQUIRED, 'The name of your workspace.')
->addOption('user-workspace-manager', 'uwm', InputOption::VALUE_REQUIRED, 'The user will be workspace manager of your workspace. Please, use its uid or email address');
->addOption(
'user-workspace-manager',
'uwm',
InputOption::VALUE_REQUIRED,
'The user will be workspace manager of your workspace. Please, use its uid or email address'
);

parent::configure();
}
Expand All @@ -36,9 +54,10 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$spacename = $input->getArgument('name');

if ($input->hasParameterOption('--user-workspace-manager')) {
$this->userChecker->checkUserExist(
$input->getOption('user-workspace-manager')
);
$pattern = $input->getOption('user-workspace-manager');
if (!$this->userChecker->checkUserExist($pattern)) {
throw new \Exception("The $pattern user or email is not exist.");
}
}

$workspace = $this->spaceManager->create($spacename);
Expand All @@ -51,7 +70,9 @@ protected function execute(InputInterface $input, OutputInterface $output): int
);
}

return $workspace['id_space'];
$output->writeln($this->formatOutput($input, $workspace));

return 0;
}

private function addUserToAdminGroupManager(string $username, array $workspace): bool {
Expand All @@ -61,4 +82,22 @@ private function addUserToAdminGroupManager(string $username, array $workspace):

return true;
}

private function formatOutput(InputInterface $input, array $items): string {
switch ($input->getOption('output')) {
case self::OUTPUT_FORMAT_JSON_PRETTY:
return json_encode($items, JSON_PRETTY_PRINT);
break;
case self::OUTPUT_FORMAT_PLAIN:
if (!is_array($items)) {
return (string)$items;
} else {
return json_encode($items);
}
break;
default:
return json_encode($items);
break;
}
}
}
51 changes: 48 additions & 3 deletions lib/Commands/Import.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,11 @@ protected function execute(InputInterface $input, OutputInterface $output): int

$dataFormated = $this->csvCreatingWorkspaces->parser($path);

foreach ($dataFormated as $data) {
$this->workspaceCheckService->isExist($data['workspace_name']);
$this->userChecker->checkUserExist($data['user_uid']);
$message = $this->getSpacenamesDuplicated($dataFormated);
$message .= $this->getUsersArentExist($dataFormated);

if (!empty($message)) {
throw new \Exception($message);
}

foreach ($dataFormated as $data) {
Expand All @@ -59,6 +61,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$this->adminGroup->addUser($user, $groupname);
}

$output->writeln("The import is done.");

return 0;
}

Expand All @@ -69,4 +73,45 @@ protected function configure(): void {
->addArgument('path', InputArgument::REQUIRED, 'The path of the csv file.');
parent::configure();
}

private function getSpacenamesDuplicated(array $dataResponse): ?string {
$workspacesAreNotExist = [];
$message = "";

foreach ($dataResponse as $data) {
if ($this->workspaceCheckService->isExist($data['workspace_name'])) {
$workspacesAreNotExist[] = $data['workspace_name'];
}
}

if (!empty($workspacesAreNotExist)) {
$workspacesAreNotExist = array_map(fn ($spacename) => " - $spacename\n", $workspacesAreNotExist);
$message .= "Workspace names below already exist :\n" . implode('', $workspacesAreNotExist);
$message .= "\n";

return $message;
}

return null;
}

private function getUsersArentExist(array $dataResponse): ?string {
$usersAreNotExist = [];
$message = "";

foreach ($dataResponse as $data) {
if (!$this->userChecker->checkUserExist($data['user_uid'])) {
$usersAreNotExist[] = $data['user_uid'];
}
}

if (!empty($usersAreNotExist)) {
$usersAreNotExist = array_map(fn ($username) => " - $username\n", $usersAreNotExist);
$message .= "Users below aren't known :\n" . implode('', $usersAreNotExist);

return $message;
}

return null;
}
}
6 changes: 5 additions & 1 deletion lib/Controller/WorkspaceController.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
use OCA\Workspace\Exceptions\BadRequestException;
use OCA\Workspace\Exceptions\CreateGroupException;
use OCA\Workspace\Exceptions\CreateWorkspaceException;
use OCA\Workspace\Exceptions\WorkspaceNameExistException;
use OCA\Workspace\Service\Group\GroupFormatter;
use OCA\Workspace\Service\Group\ManagersWorkspace;
use OCA\Workspace\Service\Group\UserGroup;
Expand Down Expand Up @@ -92,7 +93,10 @@ public function createWorkspace(string $spaceName,
}

$this->workspaceCheck->containSpecialChar($spaceName);
$this->workspaceCheck->isExist($spaceName);

if ($this->workspaceCheck->isExist($spaceName)) {
throw new WorkspaceNameExistException("The $spaceName space name already exist", Http::STATUS_CONFLICT);
}

$spaceName = $this->deleteBlankSpaceName($spaceName);

Expand Down
9 changes: 3 additions & 6 deletions lib/Service/Workspace/WorkspaceCheckService.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@
namespace OCA\Workspace\Service\Workspace;

use OCA\Workspace\Exceptions\BadRequestException;
use OCA\Workspace\Exceptions\WorkspaceNameExistException;
use OCA\Workspace\Service\SpaceService;
use OCP\AppFramework\Http;

class WorkspaceCheckService {
public function __construct(private SpaceService $spaceService) {
Expand All @@ -50,13 +48,12 @@ public function containSpecialChar(string $spacename): void {

/**
* Check if the space name exist in groupfolders or workspace
* @throws WorkspaceNameExistException
*/
public function isExist(string $spacename): void {
public function isExist(string $spacename): bool {
if ($this->spaceService->checkSpaceNameExist($spacename)) {
throw new WorkspaceNameExistException('The ' . $spacename . ' space name already exist', Http::STATUS_CONFLICT);
return true;
}

return;
return false;
}
}
6 changes: 5 additions & 1 deletion lib/Space/SpaceManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
use OCA\Workspace\Db\SpaceMapper;
use OCA\Workspace\Exceptions\BadRequestException;
use OCA\Workspace\Exceptions\CreateWorkspaceException;
use OCA\Workspace\Exceptions\WorkspaceNameExistException;
use OCA\Workspace\Helper\GroupfolderHelper;
use OCA\Workspace\Service\Group\GroupFormatter;
use OCA\Workspace\Service\Group\UserGroup;
Expand Down Expand Up @@ -56,7 +57,10 @@ public function create(string $spacename): array {
}

$this->workspaceCheck->containSpecialChar($spacename);
$this->workspaceCheck->isExist($spacename);

if ($this->workspaceCheck->isExist($spacename)) {
throw new WorkspaceNameExistException("The $spacename space name already exist", Http::STATUS_CONFLICT);
}

$spacename = $this->deleteBlankSpaceName($spacename);

Expand Down
4 changes: 2 additions & 2 deletions lib/User/UserPresenceChecker.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ public function __construct(private IUserManager $userManager,
public function checkUserExist(string $pattern): bool {
$user = $this->userFinder->findUser($pattern);
if (is_null($user)) {
throw new \Exception("The $pattern user or email is not exist.", 1);
return false;
}
return false;
return true;
}
}

0 comments on commit 85ef0df

Please sign in to comment.