Skip to content

Commit

Permalink
Adding a Config class (#52)
Browse files Browse the repository at this point in the history
* Adding a new Config class
* Adding a default DK API key to the test environment
  • Loading branch information
aldavigdis authored Mar 23, 2024
1 parent c9d7a6e commit 0817dd2
Show file tree
Hide file tree
Showing 7 changed files with 202 additions and 68 deletions.
16 changes: 9 additions & 7 deletions phpcs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@
and heresy.
-->
<exclude name="WordPress.WP.EnqueuedResourceParameters.NotInFooter" />

<!--
Disable checks for class property types being dockblocked when it's
sufficient to declare their type.
-->
<exclude name="Squiz.Commenting.VariableComment.MissingVar"></exclude>

<exclude-pattern>./tests/**</exclude-pattern>
</rule>

<!--
Expand All @@ -49,13 +57,6 @@
<exclude-pattern>./js/**</exclude-pattern>
</rule>

<!--
Not escaping outputted strings should be allowed in tests, because why not.
-->
<rule ref="WordPress.Security.EscapeOutput.OutputNotEscaped">
<exclude-pattern>./tests/**</exclude-pattern>
</rule>

<!--
Excluding commenting rules from tests, as we are using TestDox instead
-->
Expand All @@ -82,6 +83,7 @@

<!-- Enforce type hinting for function parameters -->
<rule ref="SlevomatCodingStandard.TypeHints.ParameterTypeHint">
<exclude name="SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingTraversableTypeHintSpecification" />
</rule>

<!-- Enforce type hinting for function return values -->
Expand Down
1 change: 1 addition & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
>
<php>
<env name="WP_VERSION" value="6.2.1" />
<env name="DK_API_KEY" value="3541031f-baf2-4737-a7e8-c66396e5a5e3" />
</php>
<testsuites>
<testsuite name="Project Test Suite">
Expand Down
103 changes: 103 additions & 0 deletions src/Config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
<?php

declare(strict_types = 1);

namespace NineteenEightyFour\NineteenEightyWoo;

use stdClass;

/**
* The Config class
*
* This class is for handling configuration values and options.
**/
class Config {
const DK_API_KEY_REGEX = '^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$';

/**
* Get the DK API key
*
* The order of priority when determening the API key value is:
*
* 1. The DK_API_KEY constant (defined in wp-config.php)
* 2. The DK_API_KEY environment variable
* 3. The 1984_woo_dk_api_key WP option
*/
public static function get_dk_api_key(): string|false {
if ( true === defined( 'DK_API_KEY' ) ) {
return constant( 'DK_API_KEY' );
}

if ( false !== getenv( 'DK_API_KEY' ) ) {
return getenv( 'DK_API_KEY' );
}

return get_option( '1984_woo_dk_api_key' );
}

/**
* Set the DK API key option
*
* Note that this will not override the constant or environment variable
* value.
*
* @param string $value The API key value.
*/
public static function set_dk_api_key( string $value ): bool {
if ( 0 === preg_match( '/' . self::DK_API_KEY_REGEX . '/', $value ) ) {
return false;
}
return update_option( '1984_woo_dk_api_key', $value );
}

/**
* Map a WooCommerce payment gateway to a DK payment method
*
* @param string $woo_id The alphanumeric WooCommerce payment ID.
* @param int $dk_id The payment method ID in DK.
* @param string $dk_name The payment method name in DK.
*
* @return bool True if the mapping is saved in the wp_options table, false if not.
*/
public static function set_payment_mapping(
string $woo_id,
int $dk_id,
string $dk_name
): bool {
return update_option(
'1984_woo_dk_payment_method_' . $woo_id,
(object) array(
'woo_id' => $woo_id,
'dk_id' => $dk_id,
'dk_name' => $dk_name,
)
);
}

/**
* Get a payment mapping from a WooCommerce payment gateway ID
*
* @param string $woo_id The WooCommerce payment gateway ID.
* @param bool $empty_object Populates a default value as an object with
* empty properties. If false, it will return
* false if no mapping is found.
*
* @return stdClass An object containing woo_id, dk_id and dk_name properties.
*/
public static function get_payment_mapping( string $woo_id, bool $empty_object = true ): stdClass {
if ( true === $empty_object ) {
$default = (object) array(
'woo_id' => '',
'dk_id' => '',
'dk_name' => '',
);
} else {
$default = false;
}

return get_option(
'1984_woo_dk_payment_method_' . $woo_id,
$default
);
}
}
16 changes: 4 additions & 12 deletions src/Rest/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace NineteenEightyFour\NineteenEightyWoo\Rest;

use NineteenEightyFour\NineteenEightyWoo\Config;

use WP_Error;
use WP_REST_Request;
use WP_REST_Response;
Expand Down Expand Up @@ -91,20 +93,10 @@ public static function rest_api_callback(
);
}

update_option( '1984_woo_dk_api_key', $rest_json->api_key );

foreach ( $rest_json->payment_methods as $p ) {
update_option(
'1984_woo_dk_payment_method_' . $p->woo_id,
$p
);
}
Config::set_dk_api_key( $rest_json->api_key );

foreach ( $rest_json->payment_methods as $p ) {
update_option(
'1984_woo_dk_payment_method_' . $p->woo_id,
$p
);
Config::set_payment_mapping( $p->woo_id, $p->dk_id, $p->dk_name );
}

return new WP_REST_Response( array( 'status' => 200 ) );
Expand Down
33 changes: 33 additions & 0 deletions tests/ConfigTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types = 1);

namespace NineteenEightyFour\NineteenEightyWoo\Tests;

use NineteenEightyFour\NineteenEightyWoo\Config;
use PHPUnit\Framework\TestCase;
use PHPUnit\Framework\Attributes\TestDox;

use function PHPUnit\Framework\assertEquals;

#[TestDox( 'The Config class' )]
final class ConfigTest extends TestCase {
#[TestDox( 'gets the DK API key configuration value from ENV variables' )]
public function testGetDkApiKeyFromENV(): void {
assertEquals( 36, strlen( getenv( 'DK_API_KEY' ) ) );
assertEquals( getenv( 'DK_API_KEY' ), Config::get_dk_api_key() );
}

#[TestDox( 'gets the DK API key configuration value from the wp_options table if not set in an environment variable' )]
public function testGetDkApiKeyFromWpOptions(): void {
$original_key = getenv( 'DK_API_KEY' );
$new_key = '0c72b041-f8fc-473b-b745-00fed510ea0f';

assertEquals( $original_key, Config::get_dk_api_key() );

putenv( 'DK_API_KEY' );
update_option( '1984_woo_dk_api_key', $new_key );

assertEquals( $new_key, Config::get_dk_api_key() );
}
}
2 changes: 1 addition & 1 deletion tests/RestSettingsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
#[TestDox( 'The Rest Settings JSON API endpoint class' )]
final class RestSettingstest extends TestCase {
const VALID_POST_BODY = [
'api_key' => 'MwmTtCTfhVQvlEOKCqFxfRAuPYHgy17E',
'api_key' => '3541031f-baf2-4737-a7e8-c66396e5a5e3',
'payment_methods' => [
[
'woo_id' => 'bacs',
Expand Down
99 changes: 51 additions & 48 deletions views/admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

declare(strict_types = 1);

use NineteenEightyFour\NineteenEightyWoo\Config;

$wc_payment_gateways = new WC_Payment_Gateways();

?>
Expand Down Expand Up @@ -29,8 +31,8 @@ class="wrap nineteen-eighty-woo-wrap"
class="regular-text api-key-input"
name="api_key"
type="text"
value="<?php echo esc_attr( get_option( '1984_woo_dk_api_key' ) ); ?>"
pattern="(a|b|c|d|e|f|[0-9]|-)+"
value="<?php echo esc_attr( Config::get_dk_api_key() ); ?>"
pattern="<?php echo esc_attr( Config::DK_API_KEY_REGEX ); ?>"
required
/>

Expand All @@ -57,52 +59,53 @@ class="regular-text api-key-input"
<table id="payment-gateway-id-map-table" class="form-table">
<tbody>
<?php foreach ( $wc_payment_gateways->payment_gateways as $p ) : ?>
<tr data-gateway-id="<?php echo esc_attr( $p->id ); ?>">
<th span="row" class="column-title column-primary">
<span class="payment-gateway-title"><?php echo esc_html( $p->title ); ?></span>
<?php if ( 'yes' === $p->enabled ) : ?>
<span class="payment-gateway-status enabled">
<?php esc_html_e( 'Enabled in WC', 'NineteenEightyWoo' ); ?>
</span>
<?php else : ?>
<span class="payment-gateway-status enabled">
<?php esc_html_e( 'Disabled in WC', 'NineteenEightyWoo' ); ?>
</span>
<?php endif ?>
</th>
<td class="method-id">
<label for="payment_id_input_<?php echo esc_attr( $p->id ); ?>">
<?php esc_html_e( 'Method ID', 'NineteenEightyWoo' ); ?>
</label>
<input
id="payment_id_input_<?php echo esc_attr( $p->id ); ?>"
class="regular-text payment-id"
name="payment_id"
type="text"
value="<?php echo esc_attr( get_option( '1984_woo_dk_payment_method_' . $p->id )->dk_id ); ?>"
inputmode="numeric"
pattern="[0-9]+"
required
/>
<p class="validity valid"><?php esc_html_e( 'Valid', 'NineteenEightyWoo' ); ?><span class="dashicons dashicons-yes"></span></p>
<p class="validity invalid"><?php esc_html_e( 'Needs to be numeric', 'NineteenEightyWoo' ); ?></p>
</td>
<td>
<label for="payment_name_input_<?php echo esc_attr( $p->id ); ?>">
<?php esc_html_e( 'DK Payment Method Name', 'NineteenEightyWoo' ); ?>
</label>
<input
id="payment_name_input_<?php echo esc_attr( $p->id ); ?>"
class="regular-text payment-name"
name="payment_name"
value="<?php echo esc_attr( get_option( '1984_woo_dk_payment_method_' . $p->id )->dk_name ); ?>"
type="text"
required
/>
<p class="validity valid"><?php esc_html_e( 'Valid', 'NineteenEightyWoo' ); ?><span class="dashicons dashicons-yes"></span></p>
<p class="validity invalid"><?php esc_html_e( 'This is a required field', 'NineteenEightyWoo' ); ?></p>
</td>
</tr>
<?php $payment_map = Config::get_payment_mapping( $p->id ); ?>
<tr data-gateway-id="<?php echo esc_attr( $p->id ); ?>">
<th span="row" class="column-title column-primary">
<span class="payment-gateway-title"><?php echo esc_html( $p->title ); ?></span>
<?php if ( 'yes' === $p->enabled ) : ?>
<span class="payment-gateway-status enabled">
<?php esc_html_e( 'Enabled in WC', 'NineteenEightyWoo' ); ?>
</span>
<?php else : ?>
<span class="payment-gateway-status enabled">
<?php esc_html_e( 'Disabled in WC', 'NineteenEightyWoo' ); ?>
</span>
<?php endif ?>
</th>
<td class="method-id">
<label for="payment_id_input_<?php echo esc_attr( $p->id ); ?>">
<?php esc_html_e( 'Method ID', 'NineteenEightyWoo' ); ?>
</label>
<input
id="payment_id_input_<?php echo esc_attr( $p->id ); ?>"
class="regular-text payment-id"
name="payment_id"
type="text"
value="<?php echo esc_attr( $mapping->dk_id ); ?>"
inputmode="numeric"
pattern="[0-9]+"
required
/>
<p class="validity valid"><?php esc_html_e( 'Valid', 'NineteenEightyWoo' ); ?><span class="dashicons dashicons-yes"></span></p>
<p class="validity invalid"><?php esc_html_e( 'Needs to be numeric', 'NineteenEightyWoo' ); ?></p>
</td>
<td>
<label for="payment_name_input_<?php echo esc_attr( $p->id ); ?>">
<?php esc_html_e( 'DK Payment Method Name', 'NineteenEightyWoo' ); ?>
</label>
<input
id="payment_name_input_<?php echo esc_attr( $p->id ); ?>"
class="regular-text payment-name"
name="payment_name"
value="<?php echo esc_attr( $mapping->dk_name ); ?>"
type="text"
required
/>
<p class="validity valid"><?php esc_html_e( 'Valid', 'NineteenEightyWoo' ); ?><span class="dashicons dashicons-yes"></span></p>
<p class="validity invalid"><?php esc_html_e( 'This is a required field', 'NineteenEightyWoo' ); ?></p>
</td>
</tr>
<?php endforeach ?>
</tbody>
</table>
Expand Down

0 comments on commit 0817dd2

Please sign in to comment.