Skip to content

Commit

Permalink
fix: update module for support with php8 and newer
Browse files Browse the repository at this point in the history
  • Loading branch information
wilr committed May 22, 2024
1 parent e20d3b7 commit eaaa2dc
Show file tree
Hide file tree
Showing 16 changed files with 158 additions and 58 deletions.
17 changes: 17 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -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
17 changes: 17 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -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
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/vendor
/composer.lock
/public
.phpunit.result.cache
3 changes: 1 addition & 2 deletions _config/config.yml
Original file line number Diff line number Diff line change
@@ -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

Expand Down
53 changes: 33 additions & 20 deletions composer.json
Original file line number Diff line number Diff line change
@@ -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": "[email protected]"
"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/" }
}
}
16 changes: 16 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" bootstrap="vendor/silverstripe/framework/tests/bootstrap.php" colors="true" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
<coverage includeUncoveredFiles="true">
<include>
<directory suffix=".php">src/.</directory>
</include>
<exclude>
<directory suffix=".php">tests/</directory>
</exclude>
</coverage>
<testsuites>
<testsuite name="Default">
<directory>tests</directory>
</testsuite>
</testsuites>
</phpunit>
6 changes: 5 additions & 1 deletion src/Connection.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
<?php

namespace Heyday\Vend;

use VendAPI\VendAPI;
use SilverStripe\SiteConfig\SiteConfig;

/**
* Class Connector
*
* Making calls to the VendAPI to request the access token and refresh token
*/
class Connection extends VendAPI
{
protected TokenManager $tokenManager;

/**
* instantiating the VendAPI object and passing the required parameters
*/
Expand All @@ -19,7 +23,7 @@ public function __construct(TokenManager $tokenManager)
$config = SiteConfig::current_site_config();
$shopName = $config->VendShopName;
$url = "https://$shopName.vendhq.com";

parent::__construct($url, 'Bearer', $tokenManager->getToken());
}

}
1 change: 1 addition & 0 deletions src/Exceptions/SetupException.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

namespace Heyday\Vend\Exceptions;

/**
Expand Down
1 change: 1 addition & 0 deletions src/Exceptions/TokenException.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

namespace Heyday\Vend\Exceptions;

/**
Expand Down
3 changes: 1 addition & 2 deletions src/SilverStripe/Admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,11 @@ public function getEditForm($id = null, $fields = null)
$client_id = Config::inst()->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');
}


}
16 changes: 7 additions & 9 deletions src/SilverStripe/Authorise_Controller.php
Original file line number Diff line number Diff line change
@@ -1,19 +1,13 @@
<?php

namespace Heyday\Vend\SilverStripe;
/**
* This controller is hit by Vend after Shop owner has authorised your app.
* The authorisation code is used to make the first token request.
* This token as well is saved in the siteconfig along with the refresh token
* Class VendAuthorise_Controller
*/

use Heyday\Vend\TokenManager;
use SilverStripe\Security\Member;
use SilverStripe\Security\Permission;
use SilverStripe\Control\Controller;
use SilverStripe\Security\Security;

/**
* Class Authorise_Controller
* @package Heyday\Vend\SilverStripe
*/
class Authorise_Controller extends Controller
Expand All @@ -36,8 +30,11 @@ public function setTokenManager($tokenManager)
*/
public function index()
{
if (Member::currentUserID() && Permission::check('ADMIN')) {
$member = Security::getCurrentUser();

if ($member && Permission::checkMember($member, 'ADMIN')) {
$code = $this->request->getVar('code');

if (isset($code) && !empty($code)) {
if ($this->getFirstToken($code)) {
return $this->redirect('/admin/vend');
Expand All @@ -46,6 +43,7 @@ public function index()
return 'There has been an error';
}
}

return $this->redirect('/admin');
}

Expand Down
23 changes: 15 additions & 8 deletions src/SilverStripe/SetupForm.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

namespace Heyday\Vend\SilverStripe;

use SilverStripe\SiteConfig\SiteConfig;
Expand All @@ -17,23 +18,28 @@
*/
class SetupForm extends Form
{

/**
* @var array
*/
private static $allowed_actions = array('doSave');
private static $allowed_actions = ['doSave'];


/**
* @param \Controller $controller
*/
public function __construct($controller, $name)
{
$this->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(
Expand Down Expand Up @@ -69,13 +75,15 @@ public function __construct($controller, $name)
);
}
}

$fields->add(
TextField::create(
'VendShopName',
'Vend Shop Name (as in: <Name>.vendhq.com)',
$vendShopName
)
);

$actions->push(FormAction::create('doSave', 'Save'));

// Reduce attack surface by enforcing POST requests
Expand Down Expand Up @@ -108,4 +116,3 @@ public function doSave($data)
$this->controller->redirectBack();
}
}

Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
<?php

namespace Heyday\Vend\SilverStripe;

use SilverStripe\ORM\DataExtension;

/**
* Class VendConfig
*/
class SiteConfig extends DataExtension
class SiteConfigExtension extends DataExtension
{
/**
* @var array
*/
private static $db = array(
private static $db = [
'VendShopName' => 'Varchar(255)'
);

];
}

9 changes: 3 additions & 6 deletions src/SilverStripe/VendToken.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)'
);
];
}

18 changes: 13 additions & 5 deletions src/TokenManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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);
}

}
20 changes: 20 additions & 0 deletions tests/SetupFormTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Heyday\Vend\Tests;

use Heyday\Vend\SilverStripe\SetupForm;
use SilverStripe\Control\Controller;
use SilverStripe\Dev\SapphireTest;

class SetupFormTest extends SapphireTest
{
protected $usesDatabase = true;

public function testRenderForm()
{
$form = new SetupForm(new Controller(), 'VendSetupForm');

$this->assertNotNull($form);
$this->assertNotNull($form->Fields());
}
}

0 comments on commit eaaa2dc

Please sign in to comment.