Skip to content

Commit

Permalink
Merge pull request #8 from lloc/add-di
Browse files Browse the repository at this point in the history
PHP DI integrated
  • Loading branch information
lloc authored Dec 6, 2024
2 parents df8e352 + fb4c1b2 commit d9da7a9
Show file tree
Hide file tree
Showing 62 changed files with 525 additions and 960 deletions.
30 changes: 30 additions & 0 deletions .distignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/.wordpress-org
/.git
/.github
/.idea
/.phpunit.cache
/bin
/wp-nowpayments-integration
/node_modules
/reports
/src
/tests
.distignore
.gitattributes
.gitignore
.scrutinizer.yml
Changelog.md
Diagrams.md
README.md
composer.json
composer.lock
package-lock.json
package.json
patchwork.json
phpcs.xml
phpdoc.xml
phpstan.neon
phpunit.xml
playwright.config.ts
setup.sh
multisite-language-switcher.zip
20 changes: 20 additions & 0 deletions .github/workflows/plugin-check.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
name: Plugin Check
on: # rebuild any PRs and main branch changes
pull_request:
push:
branches:
- main
jobs:
test:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Composer dependencies
run: composer install --no-dev --no-interaction --optimize-autoloader
- name: Build
run: composer run-script build
- name: Run plugin check
uses: wordpress/plugin-check-action@v1
with:
build-dir: './wp-nowpayments-integration'
13 changes: 8 additions & 5 deletions .wp-env.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
{
"core": "WordPress/WordPress",
"plugins": [
"."
]
}
"core": "WordPress/WordPress",
"plugins": [
"."
],
"config": {
"WP_ENVIRONMENT_TYPE": "development"
}
}
12 changes: 12 additions & 0 deletions bin/git-release.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/usr/bin/env bash

PROJECT_ROOT="$( cd "$( dirname "${BASH_SOURCE[0]}" )/.." && pwd )"
PLUGIN_NAME="wp-nowpayments-integration"
BUILD_PATH="$PROJECT_ROOT/$PLUGIN_NAME"
ZIP_ARCHIVE="$PROJECT_ROOT/$PLUGIN_NAME.zip"

rm -f $ZIP_ARCHIVE
rm -rf $BUILD_PATH && mkdir $BUILD_PATH

rsync -arvp --exclude-from=$PROJECT_ROOT/.distignore $PROJECT_ROOT/ $BUILD_PATH/
cd $PROJECT_ROOT && zip -r $ZIP_ARCHIVE $PLUGIN_NAME
14 changes: 9 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,23 @@
"php": "^8.1",
"ext-json": "*",
"composer/installers": "^2.3",
"monolog/monolog": "^3.8"
"monolog/monolog": "^3.8",
"php-di/php-di": "^7.0"
},
"require-dev": {
"phpunit/phpunit": "^10.5",
"brain/monkey": "^2.6",
"phpstan/phpstan": "^1.12",
"szepeviktor/phpstan-wordpress": "^1.3",
"phpstan/extension-installer": "^1.4",
"smeghead/php-class-diagram": "^1.4",
"dealerdirect/phpcodesniffer-composer-installer": "^1.0",
"wp-coding-standards/wpcs": "^3.1",
"phpcompatibility/phpcompatibility-wp": "^2.1",
"szepeviktor/phpstan-wordpress": "^1.3",
"phpstan/extension-installer": "^1.4",
"phpstan/phpstan-deprecation-rules": "^1.2",
"johnbillion/wp-compat": "^0.3.1",
"swissspidy/phpstan-no-private": "^0.2.1"
"swissspidy/phpstan-no-private": "^0.2.1",
"phpstan/phpstan-mockery": "^1.1"
},
"autoload": {
"psr-4": {
Expand All @@ -40,7 +42,9 @@
"phpstan": "vendor/bin/phpstan analyze --memory-limit 2048M",
"diagram": "vendor/bin/php-class-diagram --php7 includes > plantuml_gist.puml",
"format": "phpcbf --standard=phpcs.xml --report-summary --report-source",
"lint": "phpcs --standard=phpcs.xml"
"lint": "phpcs --standard=phpcs.xml",
"git-release": "bin/git-release.sh",
"build": "@git-release"
},
"authors": [
{
Expand Down
39 changes: 39 additions & 0 deletions config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php declare( strict_types=1 );

use lloc\Nowpayments\AdminWidget;
use lloc\Nowpayments\Logs\StructuredLogsFormatter;
use lloc\Nowpayments\Rest\Api;

use Monolog\Handler\StreamHandler;
use Monolog\Logger;

use Psr\Log\LogLevel;
use Psr\Log\LoggerInterface;
use Psr\Container\ContainerInterface;

use function DI\create;
use function DI\get;
use function DI\factory;

if ( ! defined( 'ABSPATH' ) ) {
exit;
}

return array(
'formatter' => create( StructuredLogsFormatter::class ),
'debug_handler' => create( StreamHandler::class )
->constructor( 'php://stdout', LogLevel::DEBUG )
->method( 'setFormatter', get( 'formatter' ) ),
'error_handler' => create( StreamHandler::class )
->constructor( 'php://stderr', LogLevel::ERROR )
->method( 'setFormatter', get( 'formatter' ) ),
LoggerInterface::class => function ( ContainerInterface $c ) {
$logger = new Logger( 'wp-nowpayments-integration-logs' );
$logger->pushHandler( $c->get( 'debug_handler' ) );
$logger->pushHandler( $c->get( 'error_handler' ) );

return $logger;
},
Api::class => factory( array( Api::class, 'create' ) ),
AdminWidget::class => factory( array( AdminWidget::class, 'create' ) ),
);
27 changes: 17 additions & 10 deletions includes/AdminWidget.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,40 @@

namespace lloc\Nowpayments;

use lloc\Nowpayments\Integration\ApiStatusInterface;
use lloc\Nowpayments\Services\ApiStatusService;

class AdminWidget {

public const WIDGET_ID = 'nowpayments_status_widget';

public function __construct(
protected readonly ApiStatusInterface $status
protected readonly ApiStatusService $api_status_service
) { }

public static function create( ApiStatusInterface $status ): AdminWidget {
$obj = new self( $status );
public static function init( ApiStatusService $api_status_service ): AdminWidget {
$obj = new self( $api_status_service );

$widget_name = __( 'Nowpayments Status', 'wp-nowpayments-integration' );

wp_add_dashboard_widget( self::WIDGET_ID, $widget_name, array( $obj, 'render' ) );
add_action( 'wp_dashboard_setup', array( $obj, 'add_dashboard_widget' ) );

return $obj;
}

public function add_dashboard_widget(): void {
$widget_name = __( 'Nowpayments Status', 'wp-nowpayments-integration' );

wp_add_dashboard_widget( self::WIDGET_ID, $widget_name, array( $this, 'render' ) );
}

public function render(): void {
$message = $this->status->get()['message'] ?? '';
$service = sprintf( '<strong>%s</strong>', $this->status->get_client()->get_service()->info() );
$data = $this->api_status_service->get_data();

/* translators: 1: service name, 2: message */
$format = __( '%1$s responds with "%2$s".', 'wp-nowpayments-integration' );

echo wp_kses_post( '<div>' . sprintf( $format, $service, $message ) . '</div>' );
echo wp_kses_post(
'<div>' .
sprintf( $format, '<strong>' . $data['info'] . '</strong>', $data['status'] ) .
'</div>'
);
}
}
88 changes: 0 additions & 88 deletions includes/FrontendWidget.php

This file was deleted.

12 changes: 6 additions & 6 deletions includes/Integration/ApiStatus.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
namespace lloc\Nowpayments\Integration;

use lloc\Nowpayments\Rest\Endpoint;
use lloc\Nowpayments\Rest\EndpointGetInterface;
use lloc\Nowpayments\Rest\ResponseInterface;

final class ApiStatus extends Endpoint implements ApiStatusInterface {
final class ApiStatus extends Endpoint implements EndpointGetInterface {

/**
* @return string[]
* @return ResponseInterface
*/
public function get(): array {
$response = $this->client->get( EndpointMethods::ApiStatus->value );

return $response->get();
public function get(): ResponseInterface {
return $this->client->get( EndpointMethods::ApiStatus->value );
}
}
14 changes: 0 additions & 14 deletions includes/Integration/ApiStatusInterface.php

This file was deleted.

38 changes: 10 additions & 28 deletions includes/Integration/AvailableCurrencies.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,20 @@
namespace lloc\Nowpayments\Integration;

use lloc\Nowpayments\Rest\Endpoint;
use lloc\Nowpayments\Rest\EndpointGetInterface;
use lloc\Nowpayments\Rest\ResponseInterface;

final class AvailableCurrencies extends Endpoint {
final class AvailableCurrencies extends Endpoint implements EndpointGetInterface {

/**
* @return array<string, string[]>
* @return ResponseInterface
*/
public function get(): array {
$result = wp_cache_get( __METHOD__ );

if ( false === $result ) {
$response = $this->client->get(
EndpointMethods::AvailableCurrencies->value,
$this->get_body(),
$this->get_headers()
);

$result = $response->get();
wp_cache_set( __METHOD__, $result );
}

return $result;
public function get(): ResponseInterface {
return $this->client->get(
EndpointMethods::AvailableCurrencies->value,
$this->get_body(),
$this->get_headers()
);
}

/**
* @param string $currency
*
* @return bool
*/
public function is_available( string $currency ): bool {
$result = $this->get();
$currencies = $result['currencies'] ?? array();

return in_array( $currency, $currencies );
}
}
2 changes: 1 addition & 1 deletion includes/Integration/EndpointMethods.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php
<?php declare( strict_types=1 );

namespace lloc\Nowpayments\Integration;

Expand Down
Loading

0 comments on commit d9da7a9

Please sign in to comment.