From eaaa2dcce8b3ac536d73bf3dc704bcc5265ac9b3 Mon Sep 17 00:00:00 2001 From: Will Rossiter Date: Wed, 22 May 2024 14:17:18 +1200 Subject: [PATCH] fix: update module for support with php8 and newer --- .editorconfig | 17 ++++++ .github/workflows/ci.yml | 17 ++++++ .gitignore | 4 ++ _config/config.yml | 3 +- composer.json | 53 ++++++++++++------- phpunit.xml.dist | 16 ++++++ src/Connection.php | 6 ++- src/Exceptions/SetupException.php | 1 + src/Exceptions/TokenException.php | 1 + src/SilverStripe/Admin.php | 3 +- src/SilverStripe/Authorise_Controller.php | 16 +++--- src/SilverStripe/SetupForm.php | 23 +++++--- ...SiteConfig.php => SiteConfigExtension.php} | 9 ++-- src/SilverStripe/VendToken.php | 9 ++-- src/TokenManager.php | 18 +++++-- tests/SetupFormTest.php | 20 +++++++ 16 files changed, 158 insertions(+), 58 deletions(-) create mode 100644 .editorconfig create mode 100644 .github/workflows/ci.yml create mode 100644 .gitignore create mode 100644 phpunit.xml.dist rename src/SilverStripe/{SiteConfig.php => SiteConfigExtension.php} (69%) create mode 100644 tests/SetupFormTest.php diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..a5ea69a --- /dev/null +++ b/.editorconfig @@ -0,0 +1,17 @@ +# For more information about the properties used in this file, +# please see the EditorConfig documentation: +# http://editorconfig.org + +[*] +charset = utf-8 +end_of_line = lf +indent_size = 4 +indent_style = space +insert_final_newline = true +trim_trailing_whitespace = true + +[{*.yml,package.json,*.js,*.scss,*.feature}] +indent_size = 2 + +# The indent size used in the package.json file cannot be changed: +# https://github.com/npm/npm/pull/3180#issuecomment-16336516 diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..f998e4c --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,17 @@ +name: CI + +on: + push: + pull_request: + workflow_dispatch: + +jobs: + ci: + name: CI + uses: silverstripe/gha-ci/.github/workflows/ci.yml@v1 + with: + dynamic_matrix: false + extra_jobs: | + - php: '8.1' + db: mysql80 + phpunit: true diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3f79c8d --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +/vendor +/composer.lock +/public +.phpunit.result.cache diff --git a/_config/config.yml b/_config/config.yml index ebefd86..1ea12da 100644 --- a/_config/config.yml +++ b/_config/config.yml @@ -1,9 +1,8 @@ SilverStripe\SiteConfig\SiteConfig: extensions: - - Heyday\Vend\SilverStripe\SiteConfig + - Heyday\Vend\SilverStripe\SiteConfigExtension SilverStripe\Core\Injector\Injector: - Heyday\Vend\TokenManager: class: Heyday\Vend\TokenManager diff --git a/composer.json b/composer.json index d6f42fb..4852d4e 100644 --- a/composer.json +++ b/composer.json @@ -1,23 +1,36 @@ { - "name": "heyday/silverstripe-vend", - "description": "Provides Vend API integration for SilverStripe", - "type": "silverstripe-module", - "homepage": "http://github.com/heyday/silverstripe-vend", - "keywords": ["silverstripe", "Vend", "VendAPI"], - "license": "BSD-3-Clause", - "authors": [ - { - "name": "Ben Dubuisson", - "email": "ben.dubuisson@heyday.co.nz" + "name": "heyday/silverstripe-vend", + "description": "Provides Vend API integration for SilverStripe", + "type": "silverstripe-vendormodule", + "homepage": "http://github.com/heyday/silverstripe-vend", + "keywords": [ + "silverstripe", + "Vend", + "VendAPI" + ], + "license": "BSD-3-Clause", + "support": { + "issues": "http://github.com/heyday/silverstripe-vend/issues" + }, + "require": { + "silverstripe/framework": "^4 || ^5", + "silverstripe/admin": "*", + "silverstripe/siteconfig": "*", + "anytech/vendapi": "dev-master" + }, + "autoload": { + "psr-4": { + "Heyday\\Vend\\": "src/", + "Heyday\\Vend\\Tests\\": "tests/" + } + }, + "config": { + "allow-plugins": { + "composer/installers": true, + "silverstripe/vendor-plugin": true + } + }, + "require-dev": { + "phpunit/phpunit": "^9.6" } - ], - "support": { - "issues": "http://github.com/heyday/silverstripe-vend/issues" - }, - "require": { - "vendapi/vendapi": "dev-ss4-upgrade" - }, - "autoload": { - "psr-4": { "Heyday\\Vend\\": "src/" } - } } diff --git a/phpunit.xml.dist b/phpunit.xml.dist new file mode 100644 index 0000000..eb36a4f --- /dev/null +++ b/phpunit.xml.dist @@ -0,0 +1,16 @@ + + + + + src/. + + + tests/ + + + + + tests + + + diff --git a/src/Connection.php b/src/Connection.php index 6b8cd2d..7daa245 100644 --- a/src/Connection.php +++ b/src/Connection.php @@ -1,4 +1,5 @@ VendShopName; $url = "https://$shopName.vendhq.com"; + parent::__construct($url, 'Bearer', $tokenManager->getToken()); } - } diff --git a/src/Exceptions/SetupException.php b/src/Exceptions/SetupException.php index eaab37d..fb1c3fc 100644 --- a/src/Exceptions/SetupException.php +++ b/src/Exceptions/SetupException.php @@ -1,4 +1,5 @@ get(VendAPI::class, 'clientID'); $client_secret = Config::inst()->get(VendAPI::class, 'clientSecret'); $redirect_uri = Config::inst()->get(VendAPI::class, 'redirectURI'); + if (is_null($client_id) || is_null($client_secret) || is_null($redirect_uri)) { throw new SetupException; } return new SetupForm($this, 'EditForm'); } - - } diff --git a/src/SilverStripe/Authorise_Controller.php b/src/SilverStripe/Authorise_Controller.php index 545cbf1..45325a0 100644 --- a/src/SilverStripe/Authorise_Controller.php +++ b/src/SilverStripe/Authorise_Controller.php @@ -1,19 +1,13 @@ request->getVar('code'); + if (isset($code) && !empty($code)) { if ($this->getFirstToken($code)) { return $this->redirect('/admin/vend'); @@ -46,6 +43,7 @@ public function index() return 'There has been an error'; } } + return $this->redirect('/admin'); } diff --git a/src/SilverStripe/SetupForm.php b/src/SilverStripe/SetupForm.php index b605e25..2e61f0b 100644 --- a/src/SilverStripe/SetupForm.php +++ b/src/SilverStripe/SetupForm.php @@ -1,4 +1,5 @@ addExtraClass('vend-form'); + $this->controller = $controller; $config = SiteConfig::current_site_config(); + $vendToken = VendToken::get()->first(); - $vendAccessToken = $vendToken->AccessToken; - $vendShopName = $config->VendShopName; + $vendAccessToken = false; + $vendShopName = false; + + if ($vendToken) { + $vendAccessToken = $vendToken->AccessToken; + $vendShopName = $config->VendShopName; + } + $fields = FieldList::create(); $actions = FieldList::create(); $fields->add( @@ -69,6 +75,7 @@ public function __construct($controller, $name) ); } } + $fields->add( TextField::create( 'VendShopName', @@ -76,6 +83,7 @@ public function __construct($controller, $name) $vendShopName ) ); + $actions->push(FormAction::create('doSave', 'Save')); // Reduce attack surface by enforcing POST requests @@ -108,4 +116,3 @@ public function doSave($data) $this->controller->redirectBack(); } } - diff --git a/src/SilverStripe/SiteConfig.php b/src/SilverStripe/SiteConfigExtension.php similarity index 69% rename from src/SilverStripe/SiteConfig.php rename to src/SilverStripe/SiteConfigExtension.php index cefb072..56d2e9c 100644 --- a/src/SilverStripe/SiteConfig.php +++ b/src/SilverStripe/SiteConfigExtension.php @@ -1,4 +1,5 @@ 'Varchar(255)' - ); - + ]; } - diff --git a/src/SilverStripe/VendToken.php b/src/SilverStripe/VendToken.php index 0d506c3..a449abe 100644 --- a/src/SilverStripe/VendToken.php +++ b/src/SilverStripe/VendToken.php @@ -4,16 +4,13 @@ use SilverStripe\ORM\DataObject; -/** - * Class Token - */ class VendToken extends DataObject { + private static $table_name = 'VendToken'; - private static $db = array( + private static $db = [ 'AccessToken' => 'Varchar(255)', 'RefreshToken' => 'Varchar(255)', 'AccessTokenExpiry' => 'Varchar(255)' - ); + ]; } - diff --git a/src/TokenManager.php b/src/TokenManager.php index 42341a2..e37195b 100644 --- a/src/TokenManager.php +++ b/src/TokenManager.php @@ -140,8 +140,13 @@ public static function hasTokenExpired($tokenExpiry) */ public function getFirstToken($code) { - $body = sprintf("code=%s&client_id=%s&client_secret=%s&grant_type=authorization_code&redirect_uri=%s", - $code, $this->client_id, $this->client_secret, $this->redirect_uri); + $body = sprintf( + "code=%s&client_id=%s&client_secret=%s&grant_type=authorization_code&redirect_uri=%s", + $code, + $this->client_id, + $this->client_secret, + $this->redirect_uri + ); return $this->send($body); } @@ -154,10 +159,13 @@ public function refreshToken() { $vendToken = VendToken::get()->first(); $refresh_token = $vendToken->RefreshToken; - $body = sprintf("refresh_token=%s&client_id=%s&client_secret=%s&grant_type=refresh_token", - $refresh_token, $this->client_id, $this->client_secret); + $body = sprintf( + "refresh_token=%s&client_id=%s&client_secret=%s&grant_type=refresh_token", + $refresh_token, + $this->client_id, + $this->client_secret + ); return $this->send($body); } - } diff --git a/tests/SetupFormTest.php b/tests/SetupFormTest.php new file mode 100644 index 0000000..40b5051 --- /dev/null +++ b/tests/SetupFormTest.php @@ -0,0 +1,20 @@ +assertNotNull($form); + $this->assertNotNull($form->Fields()); + } +}