Skip to content

Commit

Permalink
cleanup, php 8.2
Browse files Browse the repository at this point in the history
  • Loading branch information
leowebguy committed Mar 24, 2023
1 parent b670624 commit 349d152
Show file tree
Hide file tree
Showing 9 changed files with 124 additions and 119 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

All notable changes to this project will be documented in this file.

## 1.0.7 - 2023.03.24
### Cleanup, PHP 8.2

## 1.0.6 - 2022.08.25
### Parse Env

Expand Down
13 changes: 5 additions & 8 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"convert",
"converter"
],
"version": "1.0.6",
"version": "1.0.7",
"type": "craft-plugin",
"license": "MIT",
"authors": [
Expand All @@ -19,6 +19,7 @@
"minimum-stability": "dev",
"prefer-stable": true,
"require": {
"php": "^7.2.5|^8.0",
"craftcms/cms": "^3.0|^4.0"
},
"require-dev": {
Expand All @@ -34,12 +35,7 @@
"extra": {
"name": "Currency Converter",
"handle": "currency-converter",
"hasCpSection": false,
"hasCpSettings": true,
"class": "leowebguy\\currencyconverter\\CurrencyConverter",
"components": {
"currencyConverterService": "leowebguy\\currencyconverter\\services\\CurrencyConverterService"
}
"class": "leowebguy\\currencyconverter\\Currency"
},
"config": {
"platform": {
Expand All @@ -53,6 +49,7 @@
"scripts": {
"check-cs": "vendor/bin/ecs check src --ansi",
"fix-cs": "vendor/bin/ecs check src --ansi --fix",
"phpstan": "vendor/bin/phpstan analyse src"
"phpstan": "vendor/bin/phpstan analyse src",
"rector": "vendor/bin/rector process src --config vendor/craftcms/rector/sets/craft-cms-40.php"
}
}
41 changes: 29 additions & 12 deletions src/CurrencyConverter.php → src/Currency.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<?php
/**
* Currency Converter plugin for Craft CMS 3.x
* Currency Converter plugin for Craft CMS
*
* @author Leo Leoncio
* @see https://github.com/leowebguy
* @copyright Copyright (c) 2021, leowebguy
* @copyright Copyright (c) 2023, leowebguy
* @license MIT
*/

Expand All @@ -14,20 +14,26 @@
use craft\base\Plugin;
use craft\base\Model;
use craft\web\twig\variables\CraftVariable;
use leowebguy\currencyconverter\models\CurrencyConverterModel;
use leowebguy\currencyconverter\variables\CurrencyConverterVariable;
use leowebguy\currencyconverter\models\CurrencyModel;
use leowebguy\currencyconverter\services\CurrencyService;
use leowebguy\currencyconverter\variables\CurrencyVariable;
use Twig\Error\LoaderError;
use Twig\Error\RuntimeError;
use Twig\Error\SyntaxError;
use yii\base\Event;
use yii\base\Exception;

/**
* Class CurrencyConverter
*/
class CurrencyConverter extends Plugin
class Currency extends Plugin
{
// Properties
// =========================================================================

public static $plugin;

public bool $hasCpSection = false;

public bool $hasCpSettings = true;

// Public Methods
// =========================================================================

Expand All @@ -40,18 +46,20 @@ public function init()
return;
}

// craft var
$this->setComponents([
'currencyService' => CurrencyService::class
]);

Event::on(
CraftVariable::class,
CraftVariable::EVENT_INIT,
function(Event $event) {
/** @var CraftVariable $variable */
$variable = $event->sender;
$variable->set('currency', CurrencyConverterVariable::class);
$variable->set('currency', CurrencyVariable::class);
}
);

// log info
Craft::info(
'Currency Converter plugin loaded',
__METHOD__
Expand All @@ -61,11 +69,20 @@ function(Event $event) {
// Protected Methods
// =========================================================================

/**
* @return Model|null
*/
protected function createSettingsModel(): ?Model
{
return new CurrencyConverterModel();
return new CurrencyModel();
}

/**
* @throws SyntaxError
* @throws Exception
* @throws RuntimeError
* @throws LoaderError
*/
protected function settingsHtml(): ?string
{
return Craft::$app->getView()->renderTemplate('currency-converter/settings', [
Expand Down
22 changes: 0 additions & 22 deletions src/models/CurrencyConverterModel.php

This file was deleted.

19 changes: 19 additions & 0 deletions src/models/CurrencyModel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php
/**
* Currency Converter plugin for Craft CMS
*
* @author Leo Leoncio
* @see https://github.com/leowebguy
* @copyright Copyright (c) 2023, leowebguy
* @license MIT
*/

namespace leowebguy\currencyconverter\models;

use craft\base\Model;

class CurrencyModel extends Model
{
public string $accessKey = '$CC_KEY';
public int $cacheTime = 6;
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<?php
/**
* Currency Converter plugin for Craft CMS 3.x
* Currency Converter plugin for Craft CMS
*
* @author Leo Leoncio
* @see https://github.com/leowebguy
* @copyright Copyright (c) 2021, leowebguy
* @copyright Copyright (c) 2023, leowebguy
* @license MIT
*/

Expand All @@ -14,28 +14,23 @@
use craft\base\Component;
use craft\helpers\App;
use craft\helpers\FileHelper;
use GuzzleHttp\Exception\GuzzleException;
use yii\base\Exception;

/**
* CurrencyConverterService.
*/
class CurrencyConverterService extends Component
class CurrencyService extends Component
{
/**
* getConversion.
*
* @param string $from
* @param string $to
* @param float $amount
*
* @param int $amount
* @return mixed
* @throws \yii\base\Exception
* @throws GuzzleException
* @throws Exception
*/
public function getConversion($from = 'EUR', $to = 'USD', $amount = 1)
public function getConversion(string $from = 'EUR', string $to = 'USD', int $amount = 1): mixed
{
$key = $from . '_' . $to;

$path = Craft::$app->path->getStoragePath() . '/currency/';
$cache = $path . $key;
$cache = $path . $from . '_' . $to;

if (!is_dir($path)) {
FileHelper::createDirectory($path);
Expand All @@ -62,19 +57,21 @@ public function getConversion($from = 'EUR', $to = 'USD', $amount = 1)
],
]);
} catch (\Exception $e) {
Craft::error('RapidAPI Guzzle Client Error', __METHOD__);
return 'RapidAPI Error';
Craft::error('RapidAPI Error | ' . $e->getMessage(), __METHOD__);
Craft::$app->getErrorHandler()->logException($e);

return 'RapidAPI Error #1';
}

$r = @json_decode(trim($response->getBody()->getContents()));

if ('success' == $r->status) {
if ($r->status == 'success') {
$rate = $r->rates->$to->rate;
@file_put_contents($cache, $rate);

return $amount * $rate;
}

return false;
return 'RapidAPI Error #2';
}
}
50 changes: 25 additions & 25 deletions src/templates/settings.html
Original file line number Diff line number Diff line change
@@ -1,35 +1,35 @@
{% import '_includes/forms' as forms %}

{{ forms.autosuggestField({
first: true,
name: 'accessKey',
label: "Access Key",
instructions: "Enter your access key from https://rapidapi.com/natkapral/api/currency-converter5 in this field"|t,
required: true,
suggestEnvVars: true,
value: settings.accessKey,
size: 64
first: true,
name: 'accessKey',
label: "Access Key",
instructions: "Enter your access key from https://rapidapi.com/natkapral/api/currency-converter5 in this field"|t,
required: true,
suggestEnvVars: true,
value: settings.accessKey,
size: 64
}) }}

{{ forms.selectField({
first: true,
name: "cacheTime",
label: "Cache Time",
instructions: "Select how many hours the plugin should cache the currency rate"|t,
options: {
1: "1 hour",
3: "3 hours",
6: "6 hours",
12: "12 hours",
24: "24 hours"
},
value: settings.cacheTime
first: true,
name: "cacheTime",
label: "Cache Time",
instructions: "Select how many hours the plugin should cache the currency rate"|t,
options: {
1: "1 hour",
3: "3 hours",
6: "6 hours",
12: "12 hours",
24: "24 hours"
},
value: settings.cacheTime
}) }}

{# {{ forms.lightswitchField({
first: true,
name: 'https',
label: "Use HTTPS",
instructions: "Whether API requests should be made using HTTPS instead of HTTP"|t,
on: settings.https
first: true,
name: 'https',
label: "Use HTTPS",
instructions: "Whether API requests should be made using HTTPS instead of HTTP"|t,
on: settings.https
}) }} #}
33 changes: 0 additions & 33 deletions src/variables/CurrencyConverterVariable.php

This file was deleted.

27 changes: 27 additions & 0 deletions src/variables/CurrencyVariable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php
/**
* Currency Converter plugin for Craft CMS
*
* @author Leo Leoncio
* @see https://github.com/leowebguy
* @copyright Copyright (c) 2023, leowebguy
* @license MIT
*/

namespace leowebguy\currencyconverter\variables;

use leowebguy\currencyconverter\Currency;

class CurrencyVariable
{
/**
* @param string $from
* @param string $to
* @param float|int $amount
* @return mixed
*/
public function conversion(string $from = 'EUR', string $to = 'USD', float|int $amount = 1): mixed
{
return Currency::$plugin->currencyService->getConversion($from, $to, $amount);
}
}

0 comments on commit 349d152

Please sign in to comment.