Skip to content

Commit

Permalink
PSR-2 refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
srenon committed Jan 1, 2018
1 parent 5a75045 commit 8c71ef0
Show file tree
Hide file tree
Showing 10 changed files with 48 additions and 73 deletions.
14 changes: 7 additions & 7 deletions Block/Adminhtml/Order/Create/Shipping/Method/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

namespace MagePal\CustomShippingRate\Block\Adminhtml\Order\Create\Shipping\Method;


class Form extends \Magento\Sales\Block\Adminhtml\Order\Create\Shipping\Method\Form
{

Expand All @@ -15,7 +14,8 @@ class Form extends \Magento\Sales\Block\Adminhtml\Order\Create\Shipping\Method\F
*
* @return string
*/
public function getActiveCustomShippingRateMethod(){
public function getActiveCustomShippingRateMethod()
{
$rate = $this->getActiveMethodRate();
return $rate && $rate->getCarrier() == \MagePal\CustomShippingRate\Model\Carrier::CODE ? $rate->getMethod() : '';
}
Expand All @@ -25,20 +25,20 @@ public function getActiveCustomShippingRateMethod(){
*
* @return string
*/
public function getActiveCustomShippingRatePrice(){
public function getActiveCustomShippingRatePrice()
{
$rate = $this->getActiveMethodRate();
return $this->getActiveCustomShippingRateMethod() && $rate->getPrice() ? $rate->getPrice() * 1 : '';
}


/**
* Custom shipping rate
*
* @return string
*/
public function isCustomShippingRateActive(){
public function isCustomShippingRateActive()
{
$rate = $this->getActiveMethodRate();
return $rate && $rate->getCarrier() == \MagePal\CustomShippingRate\Model\Carrier::CODE ? true : false;
}

}
}
14 changes: 5 additions & 9 deletions Block/Adminhtml/System/Config/Form/Composer/Version.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ public function render(\Magento\Framework\Data\Form\Element\AbstractElement $ele
return parent::render($element);
}


/**
* Return element html
*
Expand All @@ -68,13 +67,13 @@ protected function _getElementHtml(\Magento\Framework\Data\Form\Element\Abstract
return 'v' . $this->getVersion();
}


/**
* Get Module version number
*
* @return string
*/
public function getVersion(){
public function getVersion()
{
return $this->getComposerVersion($this->getModuleName());
}

Expand All @@ -101,21 +100,18 @@ public function getComposerVersion($moduleName)
$moduleName
);

try{
try {
$directoryRead = $this->readFactory->create($path);
$composerJsonData = $directoryRead->readFile('composer.json');

if($composerJsonData){
if ($composerJsonData) {
$data = json_decode($composerJsonData);
return !empty($data->version) ? $data->version : __('Unknown');
}

}
catch (\Exception $e){
} catch (\Exception $e) {
//
}

return 'Unknown';

}
}
8 changes: 2 additions & 6 deletions Block/Adminhtml/System/Config/Form/Field/Link.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
class Link extends \Magento\Config\Block\System\Config\Form\Field
{


/**
* @param \Magento\Backend\Block\Template\Context $context
* @param array $data
Expand All @@ -34,7 +33,6 @@ public function render(\Magento\Framework\Data\Form\Element\AbstractElement $ele
return parent::render($element);
}


/**
* Return element html
*
Expand All @@ -44,12 +42,10 @@ public function render(\Magento\Framework\Data\Form\Element\AbstractElement $ele
*/
protected function _getElementHtml(\Magento\Framework\Data\Form\Element\AbstractElement $element)
{
return sprintf('<a href ="%s">%s</a>',
return sprintf(
'<a href ="%s">%s</a>',
$this->_urlBuilder->getUrl('adminhtml/system_config/edit/section/carriers'),
__('Store > Configuration > Shipping Methods > Custom Shipping Rate')
);
}



}
1 change: 1 addition & 0 deletions Block/Adminhtml/System/Config/Form/Field/ShippingList.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
namespace MagePal\CustomShippingRate\Block\Adminhtml\System\Config\Form\Field;

use Magento\Config\Block\System\Config\Form\Field\FieldArray\AbstractFieldArray;

/**
* Class Locations Backend system config array field renderer
*/
Expand Down
5 changes: 2 additions & 3 deletions Block/Adminhtml/System/Config/Form/Module/Version.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ public function render(\Magento\Framework\Data\Form\Element\AbstractElement $ele
return parent::render($element);
}


/**
* Return element html
*
Expand All @@ -54,13 +53,13 @@ protected function _getElementHtml(\Magento\Framework\Data\Form\Element\Abstract
return 'v' . $this->getVersion();
}


/**
* Get Module version number
*
* @return string
*/
public function getVersion(){
public function getVersion()
{
$moduleInfo = $this->_moduleList->getOne($this->getModuleName());
return $moduleInfo['setup_version'];
}
Expand Down
19 changes: 7 additions & 12 deletions Helper/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,22 @@

class Data extends \Magento\Framework\App\Helper\AbstractHelper
{

public function getShippingType(){
public function getShippingType()
{
$arrayValues = [];
$configData = $this->getConfigData('shipping_type');

if (is_string($configData) && !empty($configData) && $configData !== '[]') {
if($this->isJson($configData)){
if ($this->isJson($configData)) {
$arrayValues = json_decode($configData, true);
}
else{
} else {
$arrayValues = array_values(unserialize($configData));
}

}

return (array)$arrayValues;
}


/**
* @return bool
*/
Expand All @@ -40,7 +37,6 @@ public function isEnabled()
return (bool)$this->getConfigData('active');
}


/**
* Retrieve information from carrier configuration
*
Expand Down Expand Up @@ -68,10 +64,9 @@ public function getConfigData($field)
* @param string $string
* @return bool
*/
public function isJson($string) {
public function isJson($string)
{
json_decode($string);
return (json_last_error() == JSON_ERROR_NONE);
}


}
}
22 changes: 7 additions & 15 deletions Model/Carrier.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@

namespace MagePal\CustomShippingRate\Model;


use Magento\Shipping\Model\Carrier\AbstractCarrier;
use Magento\Quote\Model\Quote\Address\RateRequest;

use Magento\Shipping\Model\Carrier\AbstractCarrier;

class Carrier extends AbstractCarrier implements \Magento\Shipping\Model\Carrier\CarrierInterface
{
Expand Down Expand Up @@ -55,7 +53,6 @@ class Carrier extends AbstractCarrier implements \Magento\Shipping\Model\Carrier
*/
protected $_customShippingRateHelper;


/**
* @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
* @param \Magento\Quote\Model\Quote\Address\RateResult\ErrorFactory $rateErrorFactory
Expand Down Expand Up @@ -89,7 +86,6 @@ public function __construct(
$this->_customShippingRateHelper = $customShippingRateHelper;
}


/**
* Collect and get rates
*
Expand All @@ -104,8 +100,7 @@ public function collectRates(RateRequest $request)
return $result;
}


foreach($this->_customShippingRateHelper->getShippingType() as $shippingType){
foreach ($this->_customShippingRateHelper->getShippingType() as $shippingType) {
$rate = $this->_rateMethodFactory->create();
$rate->setCarrier($this->_code);
$rate->setCarrierTitle($this->getConfigData('title'));
Expand All @@ -120,21 +115,18 @@ public function collectRates(RateRequest $request)
return $result;
}

/**
* Get allowed shipping methods
*
* @return array
*/
/**
* Get allowed shipping methods
*
* @return array
*/
public function getAllowedMethods()
{
return [$this->getCarrierCode() => __($this->getConfigData('name'))];
}


public function isTrackingAvailable()
{
return false;
}


}
24 changes: 8 additions & 16 deletions Plugin/Quote/Address/Total/ShippingPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

namespace MagePal\CustomShippingRate\Plugin\Quote\Address\Total;


class ShippingPlugin
{

Expand All @@ -20,7 +19,6 @@ class ShippingPlugin
*/
protected $_quote;


/**
* @param \MagePal\CustomShippingRate\Helper\Data $customShippingRateHelper
*/
Expand All @@ -44,11 +42,10 @@ public function aroundCollect(
\Magento\Quote\Model\Quote $quote,
\Magento\Quote\Api\Data\ShippingAssignmentInterface $shippingAssignment,
\Magento\Quote\Model\Quote\Address\Total $total
){

) {
$returnValue = $proceed($quote, $shippingAssignment, $total);

if(!$this->_customShippingRateHelper->isEnabled()){
if (!$this->_customShippingRateHelper->isEnabled()) {
return $returnValue;
}

Expand All @@ -60,7 +57,7 @@ public function aroundCollect(
if (strpos($method, \MagePal\CustomShippingRate\Model\Carrier::CODE) !== false) {
$customOption = $this->getCustomShippingJsonToArray($method);

if($customOption && strpos($method, $customOption['code']) !== false){
if ($customOption && strpos($method, $customOption['code']) !== false) {
foreach ($address->getAllShippingRates() as $rate) {
if ($rate->getCode() == $customOption['code']) {
$cost = $customOption['rate'];
Expand All @@ -79,7 +76,6 @@ public function aroundCollect(
}
}
}

}

return $returnValue;
Expand All @@ -99,18 +95,16 @@ private function getCustomShippingJsonToArray($json)

$jsonToArray = (array)json_decode($json, true);

if(!$json || count($jsonToArray) != 3){
if (!$json || count($jsonToArray) != 3) {
$json = $this->getQuote()->getCustomShippingRateJson();

if($json){
if ($json) {
$jsonToArray = (array)json_decode($json, true);
}

}


if(is_array($jsonToArray) && count($jsonToArray) == 3){
foreach($jsonToArray as $key => $value){
if (is_array($jsonToArray) && count($jsonToArray) == 3) {
foreach ($jsonToArray as $key => $value) {
$customOption[$key] = $value;
}

Expand All @@ -119,7 +113,6 @@ private function getCustomShippingJsonToArray($json)
}

return false;

}

/**
Expand All @@ -139,5 +132,4 @@ public function getQuote()
{
return $this->_quote;
}

}
}
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"custom shipping"
],
"type": "magento2-module",
"version": "1.3.1",
"version": "1.3.2",
"license": [
"MagePal License"
],
Expand Down
Loading

0 comments on commit 8c71ef0

Please sign in to comment.