Skip to content

Commit

Permalink
Update coding standards, fix phpstan errors
Browse files Browse the repository at this point in the history
  • Loading branch information
PavelJurasek committed Feb 9, 2022
1 parent 005452c commit 5ef801f
Show file tree
Hide file tree
Showing 21 changed files with 909 additions and 950 deletions.
11 changes: 7 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,21 @@
"ext-curl": "*",
"ext-json": "*",
"nette/di": "^3.0",
"nette/application": "3.0",
"nette/application": "^3.0",
"paypal/rest-api-sdk-php": "^1.14"
},
"require-dev": {
"nette/bootstrap": "^3.0",
"tracy/tracy": "^2.6",
"nette/tester": "^2.2",
"mockery/mockery": "^1.2"
"mockery/mockery": "^1.2",
"phpstan/phpstan": "^0.11",
"phpstan/phpstan-nette": "^0.11"
},
"autoload": {
"psr-0": {
"MetisFW\\PayPal\\": "src/"
"psr-4": {
"MetisFW\\PayPal\\": "src/MetisFW/PayPal",
"Tests\\PayPal\\": "tests/PayPal"
}
},
"archive": {
Expand Down
12 changes: 12 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
parameters:
level: 7
paths:
- src
- tests/PayPal
ignoreErrors:
- '#Cannot call method getParameter\(\) on Nette\\Application\\UI\\Presenter|null#'
# - '#Cannot call method redirectUrl\(\) on Nette\\Application\\UI\\Presenter|null#'

includes:
- vendor/phpstan/phpstan-nette/extension.neon
- vendor/phpstan/phpstan-nette/rules.neon
116 changes: 63 additions & 53 deletions src/MetisFW/PayPal/DI/PayPalExtension.php
Original file line number Diff line number Diff line change
@@ -1,61 +1,71 @@
<?php
<?php declare(strict_types=1);

namespace MetisFW\PayPal\DI;

use Nette\Configurator;
use Nette\DI\Compiler;
use Nette\DI\CompilerExtension;
use Nette\DI\Config\Helpers;
use Nette\Utils\Validators;

class PayPalExtension extends CompilerExtension {

/**
* @var array
*/
public $defaults = array(
'currency' => 'CZK',
'gaTrackingEnabled' => true
);

public function loadConfiguration() {
$builder = $this->getContainerBuilder();
$config = Helpers::merge($this->getConfig(), $this->defaults);

Validators::assertField($config, 'clientId');
Validators::assertField($config, 'secret');
Validators::assertField($config, 'sdkConfig', 'array');

$builder->addFactoryDefinition($this->prefix('simplePaymentOperationFactory'))
->setImplement('MetisFW\PayPal\Payment\SimplePaymentOperationFactory');

$builder->addFactoryDefinition($this->prefix('plainPaymentOperationFactory'))
->setImplement('MetisFW\PayPal\Payment\PlainPaymentOperationFactory');

$builder->addDefinition($this->prefix('credentials'))
->setType('PayPal\Auth\OAuthTokenCredential')
->setArguments(array($config['clientId'], $config['secret']));

$builder->addDefinition($this->prefix('apiContext'))
->setType('PayPal\Rest\ApiContext')
->setArguments(array($this->prefix('@credentials')));

$paypal = $builder->addDefinition($this->prefix('PayPal'))
->setType('MetisFW\PayPal\PayPalContext')
->setArguments(array($this->prefix('@apiContext')))
->addSetup('setConfig', array($config['sdkConfig']))
->addSetup('setCurrency', array($config['currency']))
->addSetup('setGaTrackingEnabled', array($config['gaTrackingEnabled']));

if (isset($config['experienceProfileId'])) {
$paypal->addSetup('setExperienceProfileId', array($config['experienceProfileId']));
}
}

public static function register(Configurator $configurator) {
$configurator->onCompile[] = function ($config, Compiler $compiler) {
$compiler->addExtension('payPal', new PayPalExtension());
};
}
use Nette\Schema\Expect;
use Nette\Schema\Schema;

class PayPalExtension extends CompilerExtension
{

public function loadConfiguration()
{
$builder = $this->getContainerBuilder();
$config = (array) $this->getConfig();

$builder->addFactoryDefinition($this->prefix('simplePaymentOperationFactory'))
->setImplement('MetisFW\PayPal\Payment\SimplePaymentOperationFactory');

$builder->addFactoryDefinition($this->prefix('plainPaymentOperationFactory'))
->setImplement('MetisFW\PayPal\Payment\PlainPaymentOperationFactory');

$builder->addDefinition($this->prefix('credentials'))
->setType('PayPal\Auth\OAuthTokenCredential')
->setArguments([$config['clientId'], $config['secret']]);

$builder->addDefinition($this->prefix('apiContext'))
->setType('PayPal\Rest\ApiContext')
->setArguments([$this->prefix('@credentials')]);

$paypal = $builder->addDefinition($this->prefix('PayPal'))
->setType('MetisFW\PayPal\PayPalContext')
->setArguments([$this->prefix('@apiContext')])
->addSetup('setConfig', [(array) $config['sdkConfig']])
->addSetup('setCurrency', [$config['currency']])
->addSetup('setGaTrackingEnabled', [$config['gaTrackingEnabled']]);

if ($config['experienceProfileId'] !== null) {
$paypal->addSetup('setExperienceProfileId', [$config['experienceProfileId']]);
}
}

public static function register(Configurator $configurator)
{
$configurator->onCompile[] = function ($config, Compiler $compiler) {
$compiler->addExtension('payPal', new PayPalExtension());
};
}

public function getConfigSchema(): Schema
{
return Expect::structure([
'currency' => Expect::string('CZK'),
'gaTrackingEnabled' => Expect::bool(true),
'experienceProfileId' => Expect::string(),
'clientId' => Expect::string()->required(),
'secret' => Expect::string()->required(),
'sdkConfig' => Expect::structure([
'mode' => Expect::anyOf('sandbox', 'live')->required(),
'log.LogEnabled' => Expect::bool(),
'log.FileName' => Expect::string(),
'log.LogLevel' => Expect::anyOf('emergency', 'alert', 'critical', 'error', 'warning', 'notice', 'info', 'debug'),
'cache.enabled' => Expect::bool(true),
'cache.FileName' => Expect::string(),
]),
]);
}

}
29 changes: 16 additions & 13 deletions src/MetisFW/PayPal/Helpers/GaTracking.php
Original file line number Diff line number Diff line change
@@ -1,26 +1,29 @@
<?php
<?php declare(strict_types=1);

namespace MetisFW\PayPal\Helpers;

use Nette\Http\Url;
use PayPal\Api\Payment;

class GaTracking {
class GaTracking
{

private function __construct() {
// nothing
}
private function __construct()
{
// nothing
}

public static function addTrackingParameters(Payment $payment) {
$redirectUrls = $payment->getRedirectUrls();
public static function addTrackingParameters(Payment $payment): Payment
{
$redirectUrls = $payment->getRedirectUrls();

$url = new Url($redirectUrls->getReturnUrl());
$url->setQueryParameter('utm_nooverride', 1);
$url = new Url($redirectUrls->getReturnUrl());
$url->setQueryParameter('utm_nooverride', 1);

$redirectUrls->setReturnUrl($url->getAbsoluteUrl());
$payment->setRedirectUrls($redirectUrls);
$redirectUrls->setReturnUrl($url->getAbsoluteUrl());
$payment->setRedirectUrls($redirectUrls);

return $payment;
}
return $payment;
}

}
138 changes: 60 additions & 78 deletions src/MetisFW/PayPal/PayPalContext.php
Original file line number Diff line number Diff line change
@@ -1,85 +1,67 @@
<?php
<?php declare(strict_types=1);

namespace MetisFW\PayPal;

use PayPal\Rest\ApiContext;

class PayPalContext {

/** @var ApiContext */
private $apiContext;

/** @var string */
private $currency;

/** @var bool */
private $gaTrackingEnabled;

/** @var string */
private $experienceProfileId;

/**
* @param string $clientId
* @param string $secret
*/
public function __construct(ApiContext $apiContext) {
$this->apiContext = $apiContext;
}

/**
* @param array $config
*/
public function setConfig(array $config) {
$this->apiContext->setConfig($config);
}

/**
* @param string $currency
*/
public function setCurrency($currency) {
$this->currency = $currency;
}

/**
* @return string
*/
public function getCurrency() {
return $this->currency;
}

/**
* @return ApiContext
*/
public function getApiContext() {
return $this->apiContext;
}

/**
* @param bool $value
*/
public function setGaTrackingEnabled($value) {
$this->gaTrackingEnabled = $value;
}

/**
* @return bool
*/
public function isGaTrackingEnabled() {
return $this->gaTrackingEnabled;
}

/**
* @return string
*/
public function getExperienceProfileId() {
return $this->experienceProfileId;
}

/**
* @param string $experienceProfileId
*/
public function setExperienceProfileId($experienceProfileId) {
$this->experienceProfileId = $experienceProfileId;
}
class PayPalContext
{

/** @var ApiContext */
private $apiContext;

/** @var string */
private $currency;

/** @var bool */
private $gaTrackingEnabled = false;

/** @var string|null */
private $experienceProfileId;

public function __construct(ApiContext $apiContext)
{
$this->apiContext = $apiContext;
}

public function setConfig(array $config): void
{
$this->apiContext->setConfig($config);
}

public function setCurrency(string $currency): void
{
$this->currency = $currency;
}

public function getCurrency(): string
{
return $this->currency;
}

public function getApiContext(): ApiContext
{
return $this->apiContext;
}

public function setGaTrackingEnabled(bool $value): void
{
$this->gaTrackingEnabled = $value;
}

public function isGaTrackingEnabled(): bool
{
return $this->gaTrackingEnabled;
}

public function getExperienceProfileId(): ?string
{
return $this->experienceProfileId;
}

public function setExperienceProfileId(string $experienceProfileId): void
{
$this->experienceProfileId = $experienceProfileId;
}

}
7 changes: 4 additions & 3 deletions src/MetisFW/PayPal/PayPalException.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<?php
<?php declare(strict_types=1);

namespace MetisFW\PayPal;

class PayPalException extends \RuntimeException {
class PayPalException extends \RuntimeException
{

}
}
Loading

0 comments on commit 5ef801f

Please sign in to comment.