Skip to content

Commit

Permalink
Merge pull request #4 from Bura42/Export-tools-added
Browse files Browse the repository at this point in the history
Export tools added
  • Loading branch information
jorisros authored Nov 19, 2019
2 parents 3bfade2 + c33109c commit eb2fa0b
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 39 deletions.
87 changes: 53 additions & 34 deletions Command/TransferFileCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,74 +14,90 @@

class TransferFileCommand extends AbstractCommand
{
protected static $defaultName = 'transfer:file';
/**
* @var FileTransferService
*/
private $fileTransferService;

protected function configure()
{
$this
->setName('transfer:file')
->setDescription('Transfer a file')
->addArgument(
'targetserverid',
InputArgument::REQUIRED,
'target server identifier')
'target server identifier'
)
->addArgument(
'sourcefile',
InputArgument::REQUIRED,
'source file')
'source file'
)
->addArgument(
'targetfile',
InputArgument::REQUIRED,
'target file')
->addOption('method',
'target file'
)
->addOption(
'method',
'm',
InputOption::VALUE_OPTIONAL,
"Determen if the service retreive files or push it to the server. Options: put,get",
FileTransferService::MODE_PUT)
->addOption('ignore',
FileTransferService::MODE_PUT
)
->addOption(
'ignore',
'i',
InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY,
'',
[])
;
[]
);
}

/**
* TransferFileCommand constructor.
* @param FileTransferService $fileTransferService
*/
public function __construct(FileTransferService $fileTransferService)
{
$this->fileTransferService = $fileTransferService;
parent::__construct();
}

protected function execute(InputInterface $input, OutputInterface $output)
{

$sourcefile = $input->getArgument('sourcefile');
$targetfile = $input->getArgument('targetfile');
$targetserverid = $input->getArgument('targetserverid');


/** @var FileTransferService $service */
$service = $this->getContainer()->get('FileTransferBundle\Service\FileTransferService');
$service = $this->fileTransferService;
$service->setMode($input->getOption('method'));

if ($service->getMode() === FileTransferService::MODE_GET) {
$files = $service->getRemoteFiles($targetserverid, $sourcefile, $this->input->getOption('ignore', []));

if (is_array($files)) {
foreach ($files as $file) {
$service->transferFile(
$targetserverid,
$sourcefile . $file,
$targetfile . $file);
$targetserverid,
$sourcefile.$file,
$targetfile.$file
);
}
}

} else {

if ($this->useDirectoryMode($sourcefile)) {
$this->transferDirectory($service, $targetserverid, $sourcefile, $targetfile);
} else {
$service->transferFile($targetserverid, $sourcefile, $targetfile);
}

}
}

/**
* Checks if the transfer is in directory mode
*
* @param string $source
* @param string $target
* @return bool
Expand All @@ -91,40 +107,43 @@ private function useDirectoryMode(string $source): bool
if (is_dir($source)) {
return true;
}

return false;
}

/**
* Transfers the a complete directory to a remote server
*
* @param FileTransferService $service
* @param string $serverId
* @param string $source
* @param string $target
* @param string $serverId
* @param string $source
* @param string $target
*/
private function transferDirectory(FileTransferService $service, string $serverId, string $source, string $target): void
{
private function transferDirectory(
FileTransferService $service,
string $serverId,
string $source,
string $target
): void {
$finder = new Finder();
$finder->files()->in($source);

/** @var ApplicationLogger $logger */
$logger = $this->getContainer()->get('monolog.logger.admin');
if (!$finder->hasResults()) {
$logger->notice('no files found', [
'component' => 'FileTransfer'
]);
$logger->notice(
'no files found',
[
'component' => 'FileTransfer',
]
);
return;
}

foreach ($finder as $file) {
$destination = $target . $file->getFilename();

$destination = $target.$file->getFilename();
$service->transferFile(
$serverId,
$file,
$destination);
$destination
);
}
}

}
9 changes: 9 additions & 0 deletions Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,15 @@ services:
# if you need to do this, you can override this setting on individual services
public: false

FileTransferBundle\Command\:
resource: '../../Command/*'
tags: ['console.command']

FileTransferBundle\Service\FileTransferService:
public: true
arguments: ["%file_transfer%"]

file_transfer.command.transfer_file_command:
class: FileTransferBundle\Command\TransferFileCommand
arguments:
- '@FileTransferBundle\Service\FileTransferService'
8 changes: 3 additions & 5 deletions Service/FileTransferService.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function getMode()
return $this->mode;
}

public function getRemoteFiles($serverId, $source, $ignore = []): ?array
public function getRemoteFiles($serverId, $source, $ignore = [".", ".."]): ?array
{
if ($this->mode === self::MODE_PUT) {
return null;
Expand All @@ -60,7 +60,6 @@ public function getRemoteFiles($serverId, $source, $ignore = []): ?array
$this->logger->error($e);
} else {
foreach ($files as $file) {

if (in_array($file, $ignore)) {
continue;
}
Expand All @@ -76,7 +75,7 @@ public function transferFile($serverid, $sourcefile, $targetfile)
{
$sftp = $this->loginInSftp($serverid);

$sftpFolderName = dirname($targetfile);
$sftpFolderName = dirname($sourcefile);

$this->logger->debug("Sftp folder name is $sftpFolderName");

Expand All @@ -97,7 +96,6 @@ public function transferFile($serverid, $sourcefile, $targetfile)
}

$sftp->disconnect();

}

/**
Expand All @@ -114,7 +112,7 @@ private function loginInSftp(string $serverid)
$e = "Sftp login failed for user " . $username;
$this->logger->error($e);
throw new \RuntimeException($e);
}
}

return $sftp;
}
Expand Down

0 comments on commit eb2fa0b

Please sign in to comment.