Skip to content

Commit

Permalink
Merge pull request #52 from mcaskill/feature/improve-error-handling
Browse files Browse the repository at this point in the history
Improve error handling and abstract download classes
  • Loading branch information
junaidbhura authored Mar 6, 2023
2 parents 4c6f414 + 3c1f728 commit 6b0284c
Show file tree
Hide file tree
Showing 10 changed files with 140 additions and 211 deletions.
17 changes: 9 additions & 8 deletions Installer.php
Original file line number Diff line number Diff line change
Expand Up @@ -238,34 +238,35 @@ protected function filterDistUrl( $url, PackageInterface $package ) {
protected function getDownloadUrl( PackageInterface $package ) {
$plugin = null;
$package_name = $package->getName();
$plugin_name = str_replace( 'junaidbhura/', '', $package_name );

switch ( $package_name ) {
case 'junaidbhura/acf-extended-pro':
$plugin = new Plugins\AcfExtendedPro( $package->getPrettyVersion() );
$plugin = new Plugins\AcfExtendedPro( $package->getPrettyVersion(), $plugin_name );
break;

case 'junaidbhura/advanced-custom-fields-pro':
$plugin = new Plugins\AcfPro( $package->getPrettyVersion() );
$plugin = new Plugins\AcfPro( $package->getPrettyVersion(), $plugin_name );
break;

case 'junaidbhura/polylang-pro':
$plugin = new Plugins\PolylangPro( $package->getPrettyVersion() );
$plugin = new Plugins\PolylangPro( $package->getPrettyVersion(), $plugin_name );
break;

case 'junaidbhura/wp-all-import-pro':
case 'junaidbhura/wp-all-export-pro':
$plugin = new Plugins\WpAiPro( $package->getPrettyVersion(), str_replace( 'junaidbhura/', '', $package_name ) );
$plugin = new Plugins\WpAiPro( $package->getPrettyVersion(), $plugin_name );
break;

default:
if ( 0 === strpos( $package_name, 'junaidbhura/gravityforms' ) ) {
$plugin = new Plugins\GravityForms( $package->getPrettyVersion(), str_replace( 'junaidbhura/', '', $package_name ) );
$plugin = new Plugins\GravityForms( $package->getPrettyVersion(), $plugin_name );
} elseif ( 0 === strpos( $package_name, 'junaidbhura/ninja-forms-' ) ) {
$plugin = new Plugins\NinjaForms( $package->getPrettyVersion(), str_replace( 'junaidbhura/', '', $package_name ) );
$plugin = new Plugins\NinjaForms( $package->getPrettyVersion(), $plugin_name );
} elseif ( 0 === strpos( $package_name, 'junaidbhura/publishpress-' ) ) {
$plugin = new Plugins\PublishPressPro( $package->getPrettyVersion(), str_replace( 'junaidbhura/', '', $package_name ) );
$plugin = new Plugins\PublishPressPro( $package->getPrettyVersion(), $plugin_name );
} elseif ( 0 === strpos( $package_name, 'junaidbhura/wpai-' ) || 0 === strpos( $package_name, 'junaidbhura/wpae-' ) ) {
$plugin = new Plugins\WpAiPro( $package->getPrettyVersion(), str_replace( 'junaidbhura/', '', $package_name ) );
$plugin = new Plugins\WpAiPro( $package->getPrettyVersion(), $plugin_name );
}
}

Expand Down
51 changes: 51 additions & 0 deletions plugins/AbstractEddPlugin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php
/**
* Abstract Easy Digital Downloads (EDD) Plugin.
*
* @package Junaidbhura\Composer\WPProPlugins\Plugins
*/

namespace Junaidbhura\Composer\WPProPlugins\Plugins;

use Composer\Semver\Semver;
use UnexpectedValueException;

/**
* AbstractEddPlugin class.
*/
abstract class AbstractEddPlugin extends AbstractPlugin {

/**
* Get the download URL for this plugin.
*
* @param array<string, mixed> $response The EDD API response.
* @return string
*/
protected function extractDownloadUrl( array $response ) {
if ( empty( $response['download_link'] ) || ! is_string( $response['download_link'] ) ) {
throw new UnexpectedValueException( sprintf(
'Expected a valid download URL for package %s',
'junaidbhura/' . $this->slug
) );
}

if ( empty( $response['new_version'] ) || ! is_scalar( $response['new_version'] ) ) {
throw new UnexpectedValueException( sprintf(
'Expected a valid download version number for package %s',
'junaidbhura/' . $this->slug
) );
}

if ( ! Semver::satisfies( $response['new_version'], $this->version ) ) {
throw new UnexpectedValueException( sprintf(
'Expected download version (%s) to match installed version (%s) of package %s',
$response['new_version'],
$this->version,
'junaidbhura/' . $this->slug
) );
}

return $response['download_link'];
}

}
46 changes: 46 additions & 0 deletions plugins/AbstractPlugin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php
/**
* Abstract Plugin.
*
* @package Junaidbhura\Composer\WPProPlugins\Plugins
*/

namespace Junaidbhura\Composer\WPProPlugins\Plugins;

/**
* AbstractPlugin class.
*/
abstract class AbstractPlugin {

/**
* The version number of the plugin to download.
*
* @var string Version number.
*/
protected $version = '';

/**
* The slug of which plugin to download.
*
* @var string Plugin slug.
*/
protected $slug = '';

/**
* AbstractPlugin constructor.
*
* @param string $version
*/
public function __construct( $version = '', $slug = '' ) {
$this->version = $version;
$this->slug = $slug;
}

/**
* Get the download URL for this plugin.
*
* @return string
*/
abstract public function getDownloadUrl();

}
33 changes: 2 additions & 31 deletions plugins/AcfExtendedPro.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,12 @@

namespace Junaidbhura\Composer\WPProPlugins\Plugins;

use Composer\Semver\Semver;
use Junaidbhura\Composer\WPProPlugins\Http;

/**
* AcfExtendedPro class.
*/
class AcfExtendedPro {

/**
* The version number of the plugin to download.
*
* @var string Version number.
*/
protected $version = '';

/**
* AcfExtendedPro constructor.
*
* @param string $version
*/
public function __construct( $version = '' ) {
$this->version = $version;
}
class AcfExtendedPro extends AbstractEddPlugin {

/**
* Get the download URL for this plugin.
Expand All @@ -46,19 +29,7 @@ public function getDownloadUrl() {
'version' => $this->version,
) ), true );

if ( empty( $response['download_link'] ) ) {
return '';
}

if ( empty( $response['new_version'] ) ) {
return '';
}

if ( ! Semver::satisfies( $response['new_version'], $this->version ) ) {
return '';
}

return $response['download_link'];
return $this->extractDownloadUrl( $response );
}

}
18 changes: 1 addition & 17 deletions plugins/AcfPro.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,7 @@
/**
* AcfPro class.
*/
class AcfPro {

/**
* The version number of the plugin to download.
*
* @var string Version number.
*/
protected $version = '';

/**
* AcfPro constructor.
*
* @param string $version
*/
public function __construct( $version = '' ) {
$this->version = $version;
}
class AcfPro extends AbstractPlugin {

/**
* Get the download URL for this plugin.
Expand Down
31 changes: 11 additions & 20 deletions plugins/GravityForms.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,12 @@
namespace Junaidbhura\Composer\WPProPlugins\Plugins;

use Junaidbhura\Composer\WPProPlugins\Http;
use UnexpectedValueException;

/**
* GravityForms class.
*/
class GravityForms {

/**
* The version number of the plugin to download.
*
* @var string Version number.
*/
protected $version = '';

/**
* The slug of which Gravity Forms plugin to download.
*
* @var string Plugin slug.
*/
protected $slug = '';
class GravityForms extends AbstractPlugin {

/**
* GravityForms constructor.
Expand All @@ -35,8 +22,7 @@ class GravityForms {
* @param string $slug
*/
public function __construct( $version = '', $slug = 'gravityforms' ) {
$this->version = $version;
$this->slug = $slug;
parent::__construct( $version, $slug );
}

/**
Expand All @@ -47,10 +33,15 @@ public function __construct( $version = '', $slug = 'gravityforms' ) {
public function getDownloadUrl() {
$http = new Http();
$response = unserialize( $http->post( 'https://gravityapi.com/wp-content/plugins/gravitymanager/api.php?op=get_plugin&slug=' . $this->slug . '&key=' . getenv( 'GRAVITY_FORMS_KEY' ) ) );
if ( ! empty( $response['download_url_latest'] ) ) {
return str_replace( 'http://', 'https://', $response['download_url_latest'] );

if ( empty( $response['download_url_latest'] ) || ! is_string( $response['download_url_latest'] ) ) {
throw new UnexpectedValueException( sprintf(
'Expected a valid download URL for package %s',
'junaidbhura/' . $this->slug
) );
}
return '';

return str_replace( 'http://', 'https://', $response['download_url_latest'] );
}

}
48 changes: 7 additions & 41 deletions plugins/NinjaForms.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,38 +7,13 @@

namespace Junaidbhura\Composer\WPProPlugins\Plugins;

use Composer\Semver\Semver;
use Junaidbhura\Composer\WPProPlugins\Http;
use UnexpectedValueException;

/**
* NinjaForms class.
*/
class NinjaForms {

/**
* The version number of the plugin to download.
*
* @var string Version number.
*/
protected $version = '';

/**
* The slug of which plugin to download.
*
* @var string Plugin slug.
*/
protected $slug = '';

/**
* WpAiPro constructor.
*
* @param string $version
* @param string $slug
*/
public function __construct( $version = '', $slug = '' ) {
$this->version = $version;
$this->slug = $slug;
}
class NinjaForms extends AbstractEddPlugin {

/**
* Get the download URL for this plugin.
Expand Down Expand Up @@ -321,7 +296,10 @@ public function getDownloadUrl() {
break;

default:
return '';
throw new UnexpectedValueException( sprintf(
'Could not find a matching package for %s. Check the package spelling and that the package is supported',
'junaidbhura/' . $this->slug
) );
}

if ( $env ) {
Expand All @@ -341,19 +319,7 @@ public function getDownloadUrl() {
'version' => $this->version,
) ), true );

if ( empty( $response['download_link'] ) ) {
return '';
}

if ( empty( $response['new_version'] ) ) {
return '';
}

if ( ! Semver::satisfies( $response['new_version'], $this->version ) ) {
return '';
}

return $response['download_link'];
return $this->extractDownloadUrl( $response );
}

}
Loading

0 comments on commit 6b0284c

Please sign in to comment.