Skip to content

Commit

Permalink
Added a few exceptions (#42) and fixed wrong docblocks and return types.
Browse files Browse the repository at this point in the history
  • Loading branch information
thelfensdrfer committed Oct 5, 2018
1 parent e34d17f commit e893d88
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 39 deletions.
104 changes: 66 additions & 38 deletions src/AutoUpdate.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
use Monolog\Logger;
use Monolog\Handler\NullHandler;

use VisualAppeal\Exceptions\DownloadException;

/**
* Auto update class.
*/
Expand Down Expand Up @@ -172,7 +174,7 @@ class AutoUpdate
*
* @param string $tempDir
* @param string $installDir
* @param int $maxExecutionTime
* @param int $maxExecutionTime
*/
public function __construct($tempDir = null, $installDir = null, $maxExecutionTime = 60)
{
Expand All @@ -196,7 +198,7 @@ public function __construct($tempDir = null, $installDir = null, $maxExecutionTi
* Set the temporary download directory.
*
* @param string $dir
* @return $this|void
* @return bool
*/
public function setTempDir($dir)
{
Expand All @@ -208,20 +210,20 @@ public function setTempDir($dir)
if (!mkdir($dir, 0755, true)) {
$this->_log->addCritical(sprintf('Could not create temporary directory "%s"', $dir));

return;
return false;
}
}

$this->_tempDir = $dir;

return $this;
return true;
}

/**
* Set the install directory.
*
* @param string $dir
* @return $this|void
* @return bool
*/
public function setInstallDir($dir)
{
Expand All @@ -233,13 +235,13 @@ public function setInstallDir($dir)
if (!mkdir($dir, 0755, true)) {
$this->_log->addCritical(sprintf('Could not create install directory "%s"', $dir));

return;
return false;
}
}

$this->_installDir = $dir;

return $this;
return true;
}

/**
Expand Down Expand Up @@ -300,8 +302,7 @@ public function setCache($adapter, $ttl = 3600)
* Set the version of the current installed software.
*
* @param string $currentVersion
*
* @return bool
* @return $this
*/
public function setCurrentVersion($currentVersion)
{
Expand All @@ -311,18 +312,23 @@ public function setCurrentVersion($currentVersion)
}

/**
* Set authentication
* @param $username
* @param $password
* Set username and password for basic authentication.
*
* @param string $username
* @param string $password
* @return $this
*/
public function setBasicAuth($username, $password)
{
$this->_username = $username;
$this->_password = $password;

return $this;
}

/**
* Set authentication in update method of users and password exist
* Set authentication header if username and password exist.
*
* @return null|resource
*/
private function _useBasicAuth()
Expand Down Expand Up @@ -391,7 +397,6 @@ public function getSimulationResults()
* Remove directory recursively.
*
* @param string $dir
*
* @return void
*/
private function _removeDir($dir)
Expand Down Expand Up @@ -447,27 +452,31 @@ public function checkUpdate()
// Read update file from update server
if (function_exists('curl_version') && $this->_isValidUrl($updateFile)) {
$update = $this->_downloadCurl($updateUrl);

if ($update === false) {
return false;
$this->_log->addError(sprintf('Could not download update file "%s" via curl!', $updateFile));

throw new DownloadException($updateFile);
}
} else {
$update = @file_get_contents($updateFile, false, $this->_useBasicAuth());

if ($update === false) {
$this->_log->addError(sprintf('Could not download update file "%s"!', $updateFile));
$this->_log->addError(sprintf('Could not download update file "%s" via file_get_contents!', $updateFile));

return false;
throw new DownloadException($updateFile);
}
}

// Parse update file
$updateFileExtension = substr(strrchr($this->_updateFile, '.'), 1);
switch ($updateFileExtension) {
case 'ini':
$versions = @parse_ini_string($update, true);
$versions = parse_ini_string($update, true);
if (!is_array($versions)) {
$this->_log->addError('Unable to parse ini update file!');

return false;
throw new ParserException;
}

$versions = array_map(function ($block) {
Expand All @@ -476,18 +485,18 @@ public function checkUpdate()

break;
case 'json':
$versions = (array)@json_decode($update);
$versions = (array) json_decode($update);
if (!is_array($versions)) {
$this->_log->addError('Unable to parse json update file!');

return false;
throw new ParserException;
}

break;
default:
$this->_log->addError(sprintf('Unknown file extension "%s"', $updateFileExtension));

return false;
throw new ParserException;
}

$this->_cache->set('update-versions', $versions);
Expand Down Expand Up @@ -549,7 +558,6 @@ public function newVersionAvailable()
* Check if url is valid.
*
* @param string $url
*
* @return boolean
*/
protected function _isValidUrl($url)
Expand All @@ -561,7 +569,6 @@ protected function _isValidUrl($url)
* Download file via curl.
*
* @param string $url URL to file
*
* @return string|false
*/
protected function _downloadCurl($url)
Expand Down Expand Up @@ -594,7 +601,6 @@ protected function _downloadCurl($url)
*
* @param string $updateUrl Url where to download from
* @param string $updateFile Path where to save the download
*
* @return bool
*/
protected function _downloadUpdate($updateUrl, $updateFile)
Expand All @@ -607,10 +613,11 @@ protected function _downloadUpdate($updateUrl, $updateFile)
}
} elseif (ini_get('allow_url_fopen')) {
$update = @file_get_contents($updateUrl, false, $this->_useBasicAuth());

if ($update === false) {
$this->_log->addError(sprintf('Could not download update "%s"!', $updateUrl));

return false;
throw new DownloadException($updateUrl);
}
}

Expand All @@ -637,7 +644,6 @@ protected function _downloadUpdate($updateUrl, $updateFile)
* Simulate update process.
*
* @param string $updateFile
*
* @return bool
*/
protected function _simulateInstall($updateFile)
Expand Down Expand Up @@ -748,8 +754,7 @@ protected function _simulateInstall($updateFile)
* Install update.
*
* @param string $updateFile Path to the update file
* @param bool $simulateInstall Check for directory and file permissions before copying files
*
* @param bool $simulateInstall Check for directory and file permissions instead of installing the update
* @return bool
*/
protected function _install($updateFile, $simulateInstall, $version)
Expand Down Expand Up @@ -830,7 +835,7 @@ protected function _install($updateFile, $simulateInstall, $version)
$this->_log->addDebug(sprintf('File "%s" created', $absoluteFilename));
}

$updateHandle = @fopen($absoluteFilename, 'w');
$updateHandle = fopen($absoluteFilename, 'w');

if (!$updateHandle) {
$this->_log->addError(sprintf('Could not open file "%s"!', $absoluteFilename));
Expand Down Expand Up @@ -874,8 +879,7 @@ protected function _install($updateFile, $simulateInstall, $version)
*
* @param bool $simulateInstall Check for directory and file permissions before copying files (Default: true)
* @param bool $deleteDownload Delete download after update (Default: true)
*
* @return mixed integer|bool
* @return integer|bool
*/
public function update($simulateInstall = true, $deleteDownload = true)
{
Expand Down Expand Up @@ -937,7 +941,7 @@ public function update($simulateInstall = true, $deleteDownload = true)
$this->runOnEachUpdateFinishCallbacks($update['version']);
if ($deleteDownload) {
$this->_log->addDebug(sprintf('Trying to delete update file "%s" after successfull update', $updateFile));
if (@unlink($updateFile)) {
if (unlink($updateFile)) {
$this->_log->addInfo(sprintf('Update file "%s" deleted after successfull update', $updateFile));
} else {
$this->_log->addError(sprintf('Could not delete update file "%s" after successfull update!', $updateFile));
Expand All @@ -948,7 +952,7 @@ public function update($simulateInstall = true, $deleteDownload = true)
} else {
if ($deleteDownload) {
$this->_log->addDebug(sprintf('Trying to delete update file "%s" after failed update', $updateFile));
if (@unlink($updateFile)) {
if (unlink($updateFile)) {
$this->_log->addInfo(sprintf('Update file "%s" deleted after failed update', $updateFile));
} else {
$this->_log->addError(sprintf('Could not delete update file "%s" after failed update!', $updateFile));
Expand All @@ -958,7 +962,9 @@ public function update($simulateInstall = true, $deleteDownload = true)
return $result;
}
}

$this->runOnAllUpdateFinishCallbacks($this->getVersionsToUpdate());

return true;
}

Expand All @@ -978,29 +984,51 @@ public function addTrailingSlash($dir)
}

/**
* @param array $callback
* Add callback which is executed after each update finished.
*
* @param callable $callback
* @return $this
*/
public function onEachUpdateFinish($callback)
{
$this->onEachUpdateFinishCallbacks[] = $callback;

return $this;
}

/**
* @param array $callback
* Add callback which is executed after all updates finished.
*
* @param callable $callback
* @return $this
*/
public function setOnAllUpdateFinishCallbacks($callback)
{
$this->onAllUpdateFinishCallbacks[] = $callback;

return $this;
}

public function runOnEachUpdateFinishCallbacks($updateVersion)
/**
* Run callbacks after each update finished.
*
* @param string $updateVersion
* @return void
*/
private function runOnEachUpdateFinishCallbacks($updateVersion)
{
foreach ($this->onEachUpdateFinishCallbacks as $callback) {
call_user_func($callback, $updateVersion);
}
}

public function runOnAllUpdateFinishCallbacks($updatedVersions)
/**
* Run callbacks after all updates finished.
*
* @param string $updatedVersions]
* @return void
*/
private function runOnAllUpdateFinishCallbacks($updatedVersions)
{
foreach ($this->onAllUpdateFinishCallbacks as $callback) {
call_user_func($callback, $updatedVersions);
Expand Down
5 changes: 5 additions & 0 deletions src/Exceptions/DownloadException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php namespace VisualAppeal\Exceptions;

use Exception;

class DownloadException extends Exception {}
5 changes: 5 additions & 0 deletions src/Exceptions/ParserException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php namespace VisualAppeal\Exceptions;

use Exception;

class ParserException extends Exception {}
4 changes: 3 additions & 1 deletion tests/AutoUpdateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use PHPUnit\Framework\TestCase;
use VisualAppeal\AutoUpdate;
use VisualAppeal\Exceptions\DownloadException;

class AutoUpdateTest extends TestCase
{
Expand Down Expand Up @@ -47,10 +48,11 @@ public function testInit()
*/
public function testErrorUpdateCheck()
{
$this->expectException(DownloadException::class);

$this->_update->setUpdateFile('404.json');
$response = $this->_update->checkUpdate();

$this->assertEquals(false, $response);
$this->assertFalse($this->_update->newVersionAvailable());
$this->assertEquals(0, count($this->_update->getVersionsToUpdate()));
}
Expand Down

0 comments on commit e893d88

Please sign in to comment.