Skip to content

Commit

Permalink
Merge pull request #73 from codedge/#69-fix-count-error
Browse files Browse the repository at this point in the history
Resolve #69
  • Loading branch information
codedge authored Feb 4, 2020
2 parents ab8fcf2 + 65ab58a commit 8ee7976
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 36 deletions.
21 changes: 17 additions & 4 deletions src/AbstractRepositoryType.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
namespace Codedge\Updater;

use Codedge\Updater\Events\HasWrongPermissions;
use File;
use GuzzleHttp\Client;
use Illuminate\Support\Facades\File;
use Symfony\Component\Finder\Finder;

/**
Expand Down Expand Up @@ -45,7 +45,7 @@ abstract class AbstractRepositoryType
*
* @return bool
*/
protected function unzipArchive($file = '', $targetDir = '', $deleteZipArchive = true) : bool
protected function unzipArchive($file = '', $targetDir = '', $deleteZipArchive = true): bool
{
if (empty($file) || ! File::exists($file)) {
throw new \InvalidArgumentException("Archive [{$file}] cannot be found or is empty.");
Expand Down Expand Up @@ -80,7 +80,7 @@ protected function unzipArchive($file = '', $targetDir = '', $deleteZipArchive =
*
* @return bool
*/
protected function hasCorrectPermissionForUpdate() : bool
protected function hasCorrectPermissionForUpdate(): bool
{
if (! $this->pathToUpdate) {
throw new \Exception('No directory set for update. Please set the update with: setPathToUpdate(path).');
Expand Down Expand Up @@ -133,7 +133,7 @@ protected function downloadRelease(Client $client, $source, $storagePath)
*
* @return bool
*/
protected function isSourceAlreadyFetched($version) : bool
protected function isSourceAlreadyFetched($version): bool
{
$storagePath = $this->config['download_path'].'/'.$version;
if (! File::exists($storagePath) || empty(File::directories($storagePath))
Expand Down Expand Up @@ -217,4 +217,17 @@ public function hasAccessToken(): bool
{
return ! empty($this->accessToken);
}

/**
* Check if files in one array (i. e. directory) are also exist in a second one.
*
* @param array $directory
* @param array $excludedDirs
*
* @return bool
*/
public function isDirectoryExcluded(array $directory, array $excludedDirs): bool
{
return count(array_intersect($directory, $excludedDirs)) ? true : false;
}
}
8 changes: 4 additions & 4 deletions src/Contracts/SourceRepositoryTypeContract.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public function fetch($version = '');
*
* @return bool
*/
public function update() : bool;
public function update(): bool;

/**
* Check repository if a newer version than the installed one is available.
Expand All @@ -29,7 +29,7 @@ public function update() : bool;
*
* @return bool
*/
public function isNewVersionAvailable($currentVersion = '') : bool;
public function isNewVersionAvailable($currentVersion = ''): bool;

/**
* Get the version that is currenly installed.
Expand All @@ -40,7 +40,7 @@ public function isNewVersionAvailable($currentVersion = '') : bool;
*
* @return string
*/
public function getVersionInstalled($prepend = '', $append = '') : string;
public function getVersionInstalled($prepend = '', $append = ''): string;

/**
* Get the latest version that has been published in a certain repository.
Expand All @@ -51,5 +51,5 @@ public function getVersionInstalled($prepend = '', $append = '') : string;
*
* @return string
*/
public function getVersionAvailable($prepend = '', $append = '') : string;
public function getVersionAvailable($prepend = '', $append = ''): string;
}
8 changes: 4 additions & 4 deletions src/SourceRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public function fetch($version = '')
*
* @return bool
*/
public function update($version = '', $forceFetching = true) : bool
public function update($version = '', $forceFetching = true): bool
{
$version = $version ?: $this->getVersionAvailable();

Expand All @@ -72,7 +72,7 @@ public function update($version = '', $forceFetching = true) : bool
*
* @return bool
*/
public function isNewVersionAvailable($currentVersion = '') : bool
public function isNewVersionAvailable($currentVersion = ''): bool
{
return $this->sourceRepository->isNewVersionAvailable($currentVersion);
}
Expand All @@ -86,7 +86,7 @@ public function isNewVersionAvailable($currentVersion = '') : bool
*
* @return string
*/
public function getVersionInstalled($prepend = '', $append = '') : string
public function getVersionInstalled($prepend = '', $append = ''): string
{
return $this->sourceRepository->getVersionInstalled($prepend, $append);
}
Expand All @@ -100,7 +100,7 @@ public function getVersionInstalled($prepend = '', $append = '') : string
*
* @return string
*/
public function getVersionAvailable($prepend = '', $append = '') : string
public function getVersionAvailable($prepend = '', $append = ''): string
{
return $this->sourceRepository->getVersionAvailable($prepend, $append);
}
Expand Down
25 changes: 13 additions & 12 deletions src/SourceRepositoryTypes/GithubRepositoryType.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
use Codedge\Updater\Events\UpdateAvailable;
use Codedge\Updater\Events\UpdateFailed;
use Codedge\Updater\Events\UpdateSucceeded;
use File;
use GuzzleHttp\Client;
use Illuminate\Support\Facades\File;
use Storage;
use Symfony\Component\Finder\Finder;

Expand Down Expand Up @@ -56,7 +56,7 @@ public function __construct(Client $client, array $config)
*
* @return bool
*/
public function isNewVersionAvailable($currentVersion = '') : bool
public function isNewVersionAvailable($currentVersion = ''): bool
{
$version = $currentVersion ?: $this->getVersionInstalled();

Expand Down Expand Up @@ -124,7 +124,7 @@ public function fetch($version = '')
*
* @return bool
*/
public function update($version = '') : bool
public function update($version = ''): bool
{
$this->setPathToUpdate(base_path(), $this->config['exclude_folders']);

Expand All @@ -138,15 +138,16 @@ public function update($version = '') : bool
// Move all directories first
collect((new Finder())->in($sourcePath)->exclude($this->config['exclude_folders'])->directories()->sort(function ($a, $b) {
return strlen($b->getRealpath()) - strlen($a->getRealpath());
}))->each(function ($directory) { /** @var \SplFileInfo $directory */
if (count(array_intersect(File::directories(
$directory->getRealPath()), $this->config['exclude_folders']) == 0)
}))->each(function (/** @var \SplFileInfo $directory */ $directory) {
if (! $this->isDirectoryExcluded(
File::directories($directory->getRealPath()), $this->config['exclude_folders'])
) {
File::copyDirectory(
$directory->getRealPath(),
base_path($directory->getRelativePath()).'/'.$directory->getBasename()
);
}

File::deleteDirectory($directory->getRealPath());
});

Expand Down Expand Up @@ -178,7 +179,7 @@ public function update($version = '') : bool
*
* @return string
*/
public function getVersionInstalled($prepend = '', $append = '') : string
public function getVersionInstalled($prepend = '', $append = ''): string
{
return $prepend.$this->config['version_installed'].$append;
}
Expand All @@ -192,7 +193,7 @@ public function getVersionInstalled($prepend = '', $append = '') : string
*
* @return string
*/
public function getVersionAvailable($prepend = '', $append = '') : string
public function getVersionAvailable($prepend = '', $append = ''): string
{
if ($this->versionFileExists()) {
$version = $prepend.$this->getVersionFile().$append;
Expand Down Expand Up @@ -240,7 +241,7 @@ protected function getRepositoryReleases()
*
* @return bool
*/
protected function versionFileExists() : bool
protected function versionFileExists(): bool
{
return Storage::exists(static::NEW_VERSION_FILE);
}
Expand All @@ -252,7 +253,7 @@ protected function versionFileExists() : bool
*
* @return bool
*/
protected function setVersionFile(string $content) : bool
protected function setVersionFile(string $content): bool
{
return Storage::put(static::NEW_VERSION_FILE, $content);
}
Expand All @@ -262,7 +263,7 @@ protected function setVersionFile(string $content) : bool
*
* @return string
*/
protected function getVersionFile() : string
protected function getVersionFile(): string
{
return Storage::get(static::NEW_VERSION_FILE);
}
Expand All @@ -272,7 +273,7 @@ protected function getVersionFile() : string
*
* @return bool
*/
protected function deleteVersionFile() : bool
protected function deleteVersionFile(): bool
{
return Storage::delete(static::NEW_VERSION_FILE);
}
Expand Down
24 changes: 13 additions & 11 deletions src/SourceRepositoryTypes/HttpRepositoryType.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
use Codedge\Updater\Events\UpdateAvailable;
use Codedge\Updater\Events\UpdateFailed;
use Codedge\Updater\Events\UpdateSucceeded;
use File;
use GuzzleHttp\Client;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Support\Facades\File;
use Psr\Http\Message\ResponseInterface;
use Storage;
use Symfony\Component\Finder\Finder;
Expand Down Expand Up @@ -70,7 +70,7 @@ public function __construct(Client $client, array $config)
*
* @return bool
*/
public function isNewVersionAvailable($currentVersion = '') : bool
public function isNewVersionAvailable($currentVersion = ''): bool
{
$version = $currentVersion ?: $this->getVersionInstalled();

Expand Down Expand Up @@ -139,7 +139,7 @@ public function fetch($version = '')
*
* @return bool
*/
public function update($version = '') : bool
public function update($version = ''): bool
{
$this->setPathToUpdate(base_path(), $this->config['exclude_folders']);

Expand All @@ -153,13 +153,15 @@ public function update($version = '') : bool
collect((new Finder())->in($sourcePath)->exclude($this->config['exclude_folders'])->directories()->sort(function ($a, $b) {
return strlen($b->getRealpath()) - strlen($a->getRealpath());
}))->each(function ($directory) { /** @var \SplFileInfo $directory */
if (count(array_intersect(File::directories(
$directory->getRealPath()), $this->config['exclude_folders'])) == 0) {
if (! $this->isDirectoryExcluded(
File::directories($directory->getRealPath()), $this->config['exclude_folders'])
) {
File::copyDirectory(
$directory->getRealPath(),
base_path($directory->getRelativePath()).'/'.$directory->getBasename()
);
}

File::deleteDirectory($directory->getRealPath());
});

Expand Down Expand Up @@ -189,7 +191,7 @@ public function update($version = '') : bool
*
* @return string
*/
public function getVersionInstalled($prepend = '', $append = '') : string
public function getVersionInstalled($prepend = '', $append = ''): string
{
return $this->config['version_installed'];
}
Expand All @@ -205,7 +207,7 @@ public function getVersionInstalled($prepend = '', $append = '') : string
*
* @return string
*/
public function getVersionAvailable($prepend = '', $append = '') : string
public function getVersionAvailable($prepend = '', $append = ''): string
{
$version = '';
if ($this->versionFileExists()) {
Expand Down Expand Up @@ -264,7 +266,7 @@ protected function getPackageReleases()
*
* @return bool
*/
protected function versionFileExists() : bool
protected function versionFileExists(): bool
{
return Storage::exists(static::NEW_VERSION_FILE);
}
Expand All @@ -276,7 +278,7 @@ protected function versionFileExists() : bool
*
* @return bool
*/
protected function setVersionFile(string $content) : bool
protected function setVersionFile(string $content): bool
{
return Storage::put(static::NEW_VERSION_FILE, $content);
}
Expand All @@ -286,7 +288,7 @@ protected function setVersionFile(string $content) : bool
*
* @return string
*/
protected function getVersionFile() : string
protected function getVersionFile(): string
{
return Storage::get(static::NEW_VERSION_FILE);
}
Expand All @@ -296,7 +298,7 @@ protected function getVersionFile() : string
*
* @return bool
*/
protected function deleteVersionFile() : bool
protected function deleteVersionFile(): bool
{
return Storage::delete(static::NEW_VERSION_FILE);
}
Expand Down
2 changes: 1 addition & 1 deletion src/UpdaterManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function __construct(Application $app)
*
* @return SourceRepository
*/
public function source($name = '') : SourceRepository
public function source($name = ''): SourceRepository
{
$name = $name ?: $this->getDefaultSourceRepository();

Expand Down
21 changes: 21 additions & 0 deletions tests/AbstractRepositoryTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Codedge\Updater\Tests;

use Codedge\Updater\AbstractRepositoryType;

class AbstractRepositoryTypeTest extends TestCase
{
/**
Expand All @@ -18,6 +20,25 @@ public function testCreateReleaseFolder($storagePath, $releaseName)
$this->assertFileNotExists($dir, 'Release folder ['.$dir.'] does not exist.');
}

public function testFilesAllExcluded()
{
/** @var AbstractRepositoryType $mock */
$mock = $this->getMockBuilder(AbstractRepositoryType::class)->getMock();
$mock->method('isDirectoryExcluded')->willReturn(true);
$this->assertTrue($mock->isDirectoryExcluded(['a', 'b'], ['a']));
$this->assertTrue($mock->isDirectoryExcluded(['a', 'b'], ['a', 'b']));
$this->assertTrue($mock->isDirectoryExcluded(['a', 'b'], ['a', 'b', 'c']));
}

public function testFilesNotAllExcluded()
{
/** @var AbstractRepositoryType $mock */
$mock = $this->getMockBuilder(AbstractRepositoryType::class)->getMock();
$mock->method('isDirectoryExcluded')->willReturn(false);
$this->assertFalse($mock->isDirectoryExcluded(['a', 'b', 'c'], ['c']));
$this->assertFalse($mock->isDirectoryExcluded(['a', 'b', 'c'], ['a', 'c']));
}

public function pathProvider()
{
return [
Expand Down

0 comments on commit 8ee7976

Please sign in to comment.