Skip to content

Commit

Permalink
Add an Abstract Command class
Browse files Browse the repository at this point in the history
  • Loading branch information
FlorentPoinsaut committed Oct 12, 2022
1 parent 08e1bc0 commit eb47fe9
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 92 deletions.
78 changes: 78 additions & 0 deletions lib/Command/AbstractCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php
declare(strict_types=1);
/**
* @copyright Copyright (c) 2022 Solution Libre SAS
* @copyright Copyright (c) 2018 Roeland Jago Douma <[email protected]>
*
* @author Florent Poinsaut <[email protected]>
* @author Roeland Jago Douma <[email protected]>
* @author John Molakvoæ <[email protected]>
*
* @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
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/

namespace OCA\ShareListing\Command;

use OCA\ShareListing\Service\SharesList;
use OC\Core\Command\Base;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;

abstract class AbstractCommand extends Base {
/** @var SharesList */
protected $sharesList;

public function __construct(SharesList $sharesList) {
parent::__construct();

$this->sharesList = $sharesList;
}

public function configure() {
$this->addOption(
'user',
'u',
InputOption::VALUE_OPTIONAL,
'Will list shares of the given user'
)
->addOption(
'path',
'p',
InputOption::VALUE_OPTIONAL,
'Will only consider the given path'
)->addOption(
'token',
't',
InputOption::VALUE_OPTIONAL,
'Will only consider the given token'
)->addOption(
'filter',
'f',
InputOption::VALUE_OPTIONAL,
'Filter shares, possible values: owner, initiator, recipient, token, has-expiration, no-expiration'
);
}

protected function getOptions(InputInterface $input): array {
$user = $input->getOption('user');
$path = $input->getOption('path');
$token = $input->getOption('token');
$filter = $this->sharesList->filterStringToInt($input->getOption('filter'));

return [$user, $path, $token, $filter];
}
}
69 changes: 10 additions & 59 deletions lib/Command/ListShares.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,69 +27,23 @@
namespace OCA\ShareListing\Command;

use iter;
use OCA\ShareListing\Service\SharesList;
use OCP\Files\IRootFolder;
use OCP\IUser;
use OCP\IUserManager;
use OCP\Share\IManager as ShareManager;
use OC\Core\Command\Base;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

class ListShares extends Base {

/** @var ShareManager */
private $shareManager;

/** @var IUserManager */
private $userManager;

/** @var IRootFolder */
private $rootFolder;

/** @var SharesList */
private $sharesList;

public function __construct(ShareManager $shareManager,
IUserManager $userManager,
IRootFolder $rootFolder,
SharesList $sharesList) {
parent::__construct();

$this->shareManager = $shareManager;
$this->userManager = $userManager;
$this->rootFolder = $rootFolder;
$this->sharesList = $sharesList;

}

class ListShares extends AbstractCommand {
public function configure() {
parent::configure();

$this->setName('sharing:list')
->setDescription('List who has access to shares by owner')
->addOption(
'user',
'u',
InputOption::VALUE_OPTIONAL,
'Will list shares of the given user'
)
->addOption(
'path',
'p',
InputOption::VALUE_OPTIONAL,
'Will only consider the given path'
)->addOption(
'token',
't',
InputOption::VALUE_OPTIONAL,
'Will only consider the given token'
)->addOption(
'filter',
'f',
'output',
'o',
InputOption::VALUE_OPTIONAL,
'Filter shares, possible values: owner, initiator, recipient, token, has-expiration, no-expiration'
)
'Output format (json or csv, default is json)',
'json'
)
->addOption(
'output',
'o',
Expand All @@ -98,12 +52,9 @@ public function configure() {
'json'
);
}

protected function execute(InputInterface $input, OutputInterface $output): int {
$user = $input->getOption('user');
$path = $input->getOption('path');
$token = $input->getOption('token');
$filter = $this->sharesList->filterStringToInt($input->getOption('filter'));
[$user, $path, $token, $filter] = $this->getOptions($input);
$outputOpt = $input->getOption('output');

$shares = iter\toArray($this->sharesList->getFormattedShares($user, $filter, $path, $token));
Expand Down
38 changes: 5 additions & 33 deletions lib/Command/SendShares.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,34 +30,31 @@
use OCA\ShareListing\Service\ReportSender;
use OCA\ShareListing\Service\SharesList;
use OCP\IUserManager;
use OC\Core\Command\Base;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

class SendShares extends Base {
class SendShares extends AbstractCommand {
/** @var IUserManager */
private $userManager;

/** @var ReportSender */
private $reportSender;

/** @var SharesList */
private $sharesList;

public function __construct(
IUserManager $userManager,
ReportSender $reportSender,
SharesList $sharesList
) {
parent::__construct();
parent::__construct($sharesList);

$this->userManager = $userManager;
$this->reportSender = $reportSender;
$this->sharesList = $sharesList;
}

public function configure() {
parent::configure();

$this->setName('sharing:send')
->setDescription('Send list who has access to shares by owner')
->addOption(
Expand All @@ -71,38 +68,13 @@ public function configure() {
'x',
InputOption::VALUE_REQUIRED,
'Generated reports will be stored on this path'
)
->addOption(
'user',
'u',
InputOption::VALUE_OPTIONAL,
'Will list shares of the given user'
)
->addOption(
'path',
'p',
InputOption::VALUE_OPTIONAL,
'Will only consider the given path'
)->addOption(
'token',
't',
InputOption::VALUE_OPTIONAL,
'Will only consider the given token'
)->addOption(
'filter',
'f',
InputOption::VALUE_OPTIONAL,
'Filter shares, possible values: owner, initiator, recipient, token, has-expiration, no-expiration'
);
}

protected function execute(InputInterface $input, OutputInterface $output): int {
$this->checkAllRequiredOptionsAreNotEmpty($input);

$user = $input->getOption('user');
$path = $input->getOption('path');
$token = $input->getOption('token');
$filter = $this->sharesList->filterStringToInt($input->getOption('filter'));
[$user, $path, $token, $filter] = $this->getOptions($input);
$recipients = $input->getOption('recipients');
$targetPath = $input->getOption('target-path');

Expand Down

0 comments on commit eb47fe9

Please sign in to comment.