Skip to content

Commit fb4c1b2

Browse files
committed
PHP DI integrated
1 parent df8e352 commit fb4c1b2

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

62 files changed

+525
-960
lines changed

.distignore

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/.wordpress-org
2+
/.git
3+
/.github
4+
/.idea
5+
/.phpunit.cache
6+
/bin
7+
/wp-nowpayments-integration
8+
/node_modules
9+
/reports
10+
/src
11+
/tests
12+
.distignore
13+
.gitattributes
14+
.gitignore
15+
.scrutinizer.yml
16+
Changelog.md
17+
Diagrams.md
18+
README.md
19+
composer.json
20+
composer.lock
21+
package-lock.json
22+
package.json
23+
patchwork.json
24+
phpcs.xml
25+
phpdoc.xml
26+
phpstan.neon
27+
phpunit.xml
28+
playwright.config.ts
29+
setup.sh
30+
multisite-language-switcher.zip

.github/workflows/plugin-check.yml

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
name: Plugin Check
2+
on: # rebuild any PRs and main branch changes
3+
pull_request:
4+
push:
5+
branches:
6+
- main
7+
jobs:
8+
test:
9+
runs-on: ubuntu-latest
10+
steps:
11+
- name: Checkout
12+
uses: actions/checkout@v4
13+
- name: Composer dependencies
14+
run: composer install --no-dev --no-interaction --optimize-autoloader
15+
- name: Build
16+
run: composer run-script build
17+
- name: Run plugin check
18+
uses: wordpress/plugin-check-action@v1
19+
with:
20+
build-dir: './wp-nowpayments-integration'

.wp-env.json

+8-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
{
2-
"core": "WordPress/WordPress",
3-
"plugins": [
4-
"."
5-
]
6-
}
2+
"core": "WordPress/WordPress",
3+
"plugins": [
4+
"."
5+
],
6+
"config": {
7+
"WP_ENVIRONMENT_TYPE": "development"
8+
}
9+
}

bin/git-release.sh

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/usr/bin/env bash
2+
3+
PROJECT_ROOT="$( cd "$( dirname "${BASH_SOURCE[0]}" )/.." && pwd )"
4+
PLUGIN_NAME="wp-nowpayments-integration"
5+
BUILD_PATH="$PROJECT_ROOT/$PLUGIN_NAME"
6+
ZIP_ARCHIVE="$PROJECT_ROOT/$PLUGIN_NAME.zip"
7+
8+
rm -f $ZIP_ARCHIVE
9+
rm -rf $BUILD_PATH && mkdir $BUILD_PATH
10+
11+
rsync -arvp --exclude-from=$PROJECT_ROOT/.distignore $PROJECT_ROOT/ $BUILD_PATH/
12+
cd $PROJECT_ROOT && zip -r $ZIP_ARCHIVE $PLUGIN_NAME

composer.json

+9-5
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,23 @@
88
"php": "^8.1",
99
"ext-json": "*",
1010
"composer/installers": "^2.3",
11-
"monolog/monolog": "^3.8"
11+
"monolog/monolog": "^3.8",
12+
"php-di/php-di": "^7.0"
1213
},
1314
"require-dev": {
1415
"phpunit/phpunit": "^10.5",
1516
"brain/monkey": "^2.6",
1617
"phpstan/phpstan": "^1.12",
17-
"szepeviktor/phpstan-wordpress": "^1.3",
18-
"phpstan/extension-installer": "^1.4",
1918
"smeghead/php-class-diagram": "^1.4",
2019
"dealerdirect/phpcodesniffer-composer-installer": "^1.0",
2120
"wp-coding-standards/wpcs": "^3.1",
2221
"phpcompatibility/phpcompatibility-wp": "^2.1",
22+
"szepeviktor/phpstan-wordpress": "^1.3",
23+
"phpstan/extension-installer": "^1.4",
2324
"phpstan/phpstan-deprecation-rules": "^1.2",
2425
"johnbillion/wp-compat": "^0.3.1",
25-
"swissspidy/phpstan-no-private": "^0.2.1"
26+
"swissspidy/phpstan-no-private": "^0.2.1",
27+
"phpstan/phpstan-mockery": "^1.1"
2628
},
2729
"autoload": {
2830
"psr-4": {
@@ -40,7 +42,9 @@
4042
"phpstan": "vendor/bin/phpstan analyze --memory-limit 2048M",
4143
"diagram": "vendor/bin/php-class-diagram --php7 includes > plantuml_gist.puml",
4244
"format": "phpcbf --standard=phpcs.xml --report-summary --report-source",
43-
"lint": "phpcs --standard=phpcs.xml"
45+
"lint": "phpcs --standard=phpcs.xml",
46+
"git-release": "bin/git-release.sh",
47+
"build": "@git-release"
4448
},
4549
"authors": [
4650
{

config.php

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php declare( strict_types=1 );
2+
3+
use lloc\Nowpayments\AdminWidget;
4+
use lloc\Nowpayments\Logs\StructuredLogsFormatter;
5+
use lloc\Nowpayments\Rest\Api;
6+
7+
use Monolog\Handler\StreamHandler;
8+
use Monolog\Logger;
9+
10+
use Psr\Log\LogLevel;
11+
use Psr\Log\LoggerInterface;
12+
use Psr\Container\ContainerInterface;
13+
14+
use function DI\create;
15+
use function DI\get;
16+
use function DI\factory;
17+
18+
if ( ! defined( 'ABSPATH' ) ) {
19+
exit;
20+
}
21+
22+
return array(
23+
'formatter' => create( StructuredLogsFormatter::class ),
24+
'debug_handler' => create( StreamHandler::class )
25+
->constructor( 'php://stdout', LogLevel::DEBUG )
26+
->method( 'setFormatter', get( 'formatter' ) ),
27+
'error_handler' => create( StreamHandler::class )
28+
->constructor( 'php://stderr', LogLevel::ERROR )
29+
->method( 'setFormatter', get( 'formatter' ) ),
30+
LoggerInterface::class => function ( ContainerInterface $c ) {
31+
$logger = new Logger( 'wp-nowpayments-integration-logs' );
32+
$logger->pushHandler( $c->get( 'debug_handler' ) );
33+
$logger->pushHandler( $c->get( 'error_handler' ) );
34+
35+
return $logger;
36+
},
37+
Api::class => factory( array( Api::class, 'create' ) ),
38+
AdminWidget::class => factory( array( AdminWidget::class, 'create' ) ),
39+
);

includes/AdminWidget.php

+17-10
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,40 @@
22

33
namespace lloc\Nowpayments;
44

5-
use lloc\Nowpayments\Integration\ApiStatusInterface;
5+
use lloc\Nowpayments\Services\ApiStatusService;
66

77
class AdminWidget {
88

99
public const WIDGET_ID = 'nowpayments_status_widget';
1010

1111
public function __construct(
12-
protected readonly ApiStatusInterface $status
12+
protected readonly ApiStatusService $api_status_service
1313
) { }
1414

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

18-
$widget_name = __( 'Nowpayments Status', 'wp-nowpayments-integration' );
19-
20-
wp_add_dashboard_widget( self::WIDGET_ID, $widget_name, array( $obj, 'render' ) );
18+
add_action( 'wp_dashboard_setup', array( $obj, 'add_dashboard_widget' ) );
2119

2220
return $obj;
2321
}
2422

23+
public function add_dashboard_widget(): void {
24+
$widget_name = __( 'Nowpayments Status', 'wp-nowpayments-integration' );
25+
26+
wp_add_dashboard_widget( self::WIDGET_ID, $widget_name, array( $this, 'render' ) );
27+
}
28+
2529
public function render(): void {
26-
$message = $this->status->get()['message'] ?? '';
27-
$service = sprintf( '<strong>%s</strong>', $this->status->get_client()->get_service()->info() );
30+
$data = $this->api_status_service->get_data();
2831

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

32-
echo wp_kses_post( '<div>' . sprintf( $format, $service, $message ) . '</div>' );
35+
echo wp_kses_post(
36+
'<div>' .
37+
sprintf( $format, '<strong>' . $data['info'] . '</strong>', $data['status'] ) .
38+
'</div>'
39+
);
3340
}
3441
}

includes/FrontendWidget.php

-88
This file was deleted.

includes/Integration/ApiStatus.php

+6-6
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@
33
namespace lloc\Nowpayments\Integration;
44

55
use lloc\Nowpayments\Rest\Endpoint;
6+
use lloc\Nowpayments\Rest\EndpointGetInterface;
7+
use lloc\Nowpayments\Rest\ResponseInterface;
68

7-
final class ApiStatus extends Endpoint implements ApiStatusInterface {
9+
final class ApiStatus extends Endpoint implements EndpointGetInterface {
810

911
/**
10-
* @return string[]
12+
* @return ResponseInterface
1113
*/
12-
public function get(): array {
13-
$response = $this->client->get( EndpointMethods::ApiStatus->value );
14-
15-
return $response->get();
14+
public function get(): ResponseInterface {
15+
return $this->client->get( EndpointMethods::ApiStatus->value );
1616
}
1717
}

includes/Integration/ApiStatusInterface.php

-14
This file was deleted.

includes/Integration/AvailableCurrencies.php

+10-28
Original file line numberDiff line numberDiff line change
@@ -3,38 +3,20 @@
33
namespace lloc\Nowpayments\Integration;
44

55
use lloc\Nowpayments\Rest\Endpoint;
6+
use lloc\Nowpayments\Rest\EndpointGetInterface;
7+
use lloc\Nowpayments\Rest\ResponseInterface;
68

7-
final class AvailableCurrencies extends Endpoint {
9+
final class AvailableCurrencies extends Endpoint implements EndpointGetInterface {
810

911
/**
10-
* @return array<string, string[]>
12+
* @return ResponseInterface
1113
*/
12-
public function get(): array {
13-
$result = wp_cache_get( __METHOD__ );
14-
15-
if ( false === $result ) {
16-
$response = $this->client->get(
17-
EndpointMethods::AvailableCurrencies->value,
18-
$this->get_body(),
19-
$this->get_headers()
20-
);
21-
22-
$result = $response->get();
23-
wp_cache_set( __METHOD__, $result );
24-
}
25-
26-
return $result;
14+
public function get(): ResponseInterface {
15+
return $this->client->get(
16+
EndpointMethods::AvailableCurrencies->value,
17+
$this->get_body(),
18+
$this->get_headers()
19+
);
2720
}
2821

29-
/**
30-
* @param string $currency
31-
*
32-
* @return bool
33-
*/
34-
public function is_available( string $currency ): bool {
35-
$result = $this->get();
36-
$currencies = $result['currencies'] ?? array();
37-
38-
return in_array( $currency, $currencies );
39-
}
4022
}

includes/Integration/EndpointMethods.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?php
1+
<?php declare( strict_types=1 );
22

33
namespace lloc\Nowpayments\Integration;
44

0 commit comments

Comments
 (0)