Skip to content

Commit

Permalink
refactor(filesize): inverting the flag and applying psr
Browse files Browse the repository at this point in the history
  • Loading branch information
Michel Paiva authored and stavarengo committed May 1, 2020
1 parent c851f1a commit c422f57
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 15 deletions.
2 changes: 1 addition & 1 deletion src/Command/TransferFileCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
$serverId = $input->getArgument('targetserverid');
$method = $input->getOption('method');
$preserveModifiedTime = $input->getOption('preservemodifiedtime');
$disableFileSizeCheck = $input->getOption('disablesizecheck');
$disableFileSizeCheck = !($input->getOption('disablesizecheck'));

$ftp = $this->ftpServiceBuilder->login($serverId);

Expand Down
6 changes: 5 additions & 1 deletion src/Service/Exception/NonMatchingFileSize.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ class NonMatchingFileSize extends \Exception
public static function create(int $remoteFileSize, int $localFileSize): self
{
return new self(
sprintf('The download file size is diferent from the remote file size. Remote file has "%d" bytes, and the local file has "%d" bytes.', $remoteFileSize, $localFileSize)
sprintf(
'The download file size is different from the remote file size. Remote file has "%d" bytes, and the local file has "%d" bytes.',
$remoteFileSize,
$localFileSize
)
);
}
}
18 changes: 9 additions & 9 deletions src/Service/FTPService.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function download(
string $remotePath,
string $localPath,
bool $preserveModifiedTime,
bool $disableFileSizeCheck
bool $checkFileSize
): void {
$remoteFiles = (array)$remotePath;
if ($this->ftp->is_dir($remotePath)) {
Expand All @@ -39,7 +39,7 @@ public function download(
$remoteFile,
rtrim($localPath, DIRECTORY_SEPARATOR) . $remoteFile,
$preserveModifiedTime,
$disableFileSizeCheck
$checkFileSize
);
} else {
$realLocalPath = str_replace(
Expand Down Expand Up @@ -69,7 +69,7 @@ public function download(
);
}

$this->checkSize($disableFileSizeCheck, $remoteFile, $realLocalPath);
$this->checkSize($checkFileSize, $remoteFile, $realLocalPath);

if ($preserveModifiedTime) {
$originalFileModifiedTime = $this->ftp->filemtime($remoteFile);
Expand All @@ -82,7 +82,7 @@ public function download(
/**
* @inheritDoc
*/
public function upload(string $localPath, string $remotePath, bool $disableFileSizeCheck): void
public function upload(string $localPath, string $remotePath, bool $checkFileSize): void
{
$localFiles = (array)$localPath;
if (is_dir($localPath)) {
Expand All @@ -101,7 +101,7 @@ function (string $item) {

foreach ($localFiles as $localFile) {
if (is_dir($localFile)) {
$this->upload($localFile, rtrim($remotePath, DIRECTORY_SEPARATOR) . $localFile, $disableFileSizeCheck);
$this->upload($localFile, rtrim($remotePath, DIRECTORY_SEPARATOR) . $localFile, $checkFileSize);
} else {
$realRemotePath = str_replace(
rtrim($localPath, DIRECTORY_SEPARATOR),
Expand Down Expand Up @@ -129,7 +129,7 @@ function (string $item) {
);
}

$this->checkSize($disableFileSizeCheck, $realRemotePath, $localPath);
$this->checkSize($checkFileSize, $realRemotePath, $localPath);
}
}
}
Expand Down Expand Up @@ -160,17 +160,17 @@ function (string $file) {
}

/**
* @param bool $disableFileSizeCheck
* @param bool $checkFileSize
* @param $remoteFile
* @param $realLocalPath
* @throws NonMatchingFileSize
*/
public function checkSize(bool $disableFileSizeCheck, string $remoteFile, string $realLocalPath): void
private function checkSize(bool $checkFileSize, string $remoteFile, string $realLocalPath): void
{
$sizeRemote = $this->ftp->size($remoteFile);
$sizeLocal = filesize($realLocalPath);

if (!$disableFileSizeCheck && $sizeRemote !== $sizeLocal) {
if ($checkFileSize && $sizeRemote !== $sizeLocal) {
throw NonMatchingFileSize::create($sizeRemote, $sizeLocal);
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/Service/FTPServiceInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,23 @@ interface FTPServiceInterface
* @param string $remotePath
* @param string $localPath
* @param bool $preserveModifiedTime
* @param bool $disableFileSizeCheck
* @param bool $checkFileSize
* @throws FTPCommandFailed
* @throws FTPTransferFileFailed
*/
public function download(
string $remotePath,
string $localPath,
bool $preserveModifiedTime,
bool $disableFileSizeCheck
bool $checkFileSize
): void;

/**
* @param string $localPath
* @param string $remotePath
* @param bool $disableFileSizeCheck
* @param bool $checkFileSize
* @throws FTPCommandFailed
* @throws FTPTransferFileFailed
*/
public function upload(string $localPath, string $remotePath, bool $disableFileSizeCheck): void;
public function upload(string $localPath, string $remotePath, bool $checkFileSize): void;
}

0 comments on commit c422f57

Please sign in to comment.