Skip to content

Commit

Permalink
Applying the same fixes from Magento 2 module's 2.1.3 version.
Browse files Browse the repository at this point in the history
  • Loading branch information
tiagosampaio committed Mar 10, 2020
1 parent 1562be3 commit e57df93
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 41 deletions.
89 changes: 62 additions & 27 deletions app/code/community/Frenet/Shipping/Helper/Objects.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@
*/
class Frenet_Shipping_Helper_Objects
{
/**
* @var string
*/
private $defaultClassGroup = 'frenet_shipping';

/**
* @return Mage_Core_Model_Cache
*/
Expand All @@ -18,215 +23,223 @@ public function cache()
*/
public function cacheManager()
{
return $this->getInstance('frenet_shipping/cache_manager');
return $this->getInstance('cache_manager');
}

/**
* @return Frenet_Shipping_Model_SerializerInterface
*/
public function serializer()
{
return $this->getInstance('frenet_shipping/serializer');
return $this->getInstance('serializer');
}

/**
* @return Frenet_Shipping_Model_CalculatorInterface
*/
public function calculator()
{
return $this->getInstance('frenet_shipping/calculator');
return $this->getInstance('calculator');
}

/**
* @return Frenet_Shipping_Model_Config
*/
public function config()
{
return $this->getInstance('frenet_shipping/config');
return $this->getInstance('config');
}

/**
* @return Frenet_Shipping_Model_Quote_Multi_Quote_Validator
*/
public function quoteMultiQuoteValidator()
{
return $this->getInstance('frenet_shipping/quote_multi_quote_validator');
return $this->getInstance('quote_multi_quote_validator');
}

/**
* @return Frenet_Shipping_Model_Quote_Item_Validator
*/
public function quoteItemValidator()
{
return $this->getInstance('frenet_shipping/quote_item_validator');
return $this->getInstance('quote_item_validator');
}

/**
* @return Frenet_Shipping_Model_Quote_Item_Quantity_CalculatorInterface
*/
public function quoteItemQtyCalculator()
{
return $this->getInstance('frenet_shipping/quote_item_quantity_calculator');
return $this->getInstance('quote_item_quantity_calculator');
}

/**
* @return Frenet_Shipping_Model_Quote_Item_Price_Calculator
*/
public function quoteItemPriceCalculator()
{
return $this->getInstance('frenet_shipping/quote_item_price_calculator');
return $this->getInstance('quote_item_price_calculator');
}

/**
* @return Frenet_Shipping_Model_Catalog_Product_Attributes_Mapping
*/
public function productAttributesMapping()
{
return $this->getInstance('frenet_shipping/catalog_product_attributes_mapping');
return $this->getInstance('catalog_product_attributes_mapping');
}

/**
* @return Frenet_Shipping_Model_Catalog_Product_Dimensions_Extractor
*/
public function productDimensionsExtractor()
{
return $this->getInstance('frenet_shipping/catalog_product_dimensions_extractor');
return $this->getInstance('catalog_product_dimensions_extractor');
}

/**
* @return Frenet_Shipping_Model_Catalog_Product_Category_Extractor
*/
public function productCategoryExtractor()
{
return $this->getInstance('frenet_shipping/catalog_product_category_extractor');
return $this->getInstance('catalog_product_category_extractor');
}

/**
* @return Frenet_Shipping_Model_Weight_Converter
*/
public function weightConverter()
{
return $this->getInstance('frenet_shipping/weight_converter');
return $this->getInstance('weight_converter');
}

/**
* @return Frenet_Shipping_Model_Service_Api
*/
public function apiService()
{
return $this->getInstance('frenet_shipping/service_api');
return $this->getInstance('service_api');
}

/**
* @return Frenet_Shipping_Model_Tracking
*/
public function trackingService()
{
return $this->getInstance('frenet_shipping/tracking');
return $this->getInstance('tracking');
}

/**
* @return Frenet_Shipping_Model_Service_Finder|Mage_Core_Model_Abstract
*/
public function serviceFinder()
{
return $this->getInstance('frenet_shipping/service_finder');
return $this->getInstance('service_finder');
}

/**
* @return Frenet_Shipping_Model_Packages_Package
*/
public function package()
{
return $this->getInstance('frenet_shipping/packages_package');
return $this->getInstance('packages_package');
}

/**
* @return Frenet_Shipping_Model_Packages_Package_Factory
*/
public function packageFactory()
{
return $this->getInstance('frenet_shipping/packages_package_factory');
return $this->getInstance('packages_package_factory');
}

/**
* @return Frenet_Shipping_Model_Packages_Package_Manager
*/
public function packageManager()
{
return $this->getInstance('frenet_shipping/packages_package_manager');
return $this->getInstance('packages_package_manager');
}

/**
* @return Frenet_Shipping_Model_Packages_Package_Calculator
*/
public function packageCalculator()
{
return $this->getInstance('frenet_shipping/packages_package_calculator');
return $this->getInstance('packages_package_calculator');
}

/**
* @return Frenet_Shipping_Model_Packages_Package_Matching
*/
public function packageMatching()
{
return $this->getInstance('frenet_shipping/packages_package_matching');
return $this->getInstance('packages_package_matching');
}

/**
* @return Frenet_Shipping_Model_Packages_Package_Limit
*/
public function packageLimit()
{
return $this->getInstance('frenet_shipping/packages_package_limit');
return $this->getInstance('packages_package_limit');
}

/**
* @return Frenet_Shipping_Model_Packages_Package_Item
*/
public function packageItem()
{
return $this->getInstance('frenet_shipping/packages_package_item');
return $this->getInstance('packages_package_item');
}

/**
* @return Frenet_Shipping_Model_Packages_Package_Item_Factory
*/
public function packageItemFactory()
{
return $this->getInstance('frenet_shipping/packages_package_item_factory');
return $this->getInstance('packages_package_item_factory');
}

/**
* @return Frenet_Shipping_Model_Store_Management
*/
public function storeManagement()
{
return $this->getInstance('frenet_shipping/store_management');
return $this->getInstance('store_management');
}

/**
* @return Frenet_Shipping_Model_Factory_Product_Resource
*/
public function productResourceFactory()
{
return $this->getInstance('frenet_shipping/factory_product_resource');
return $this->getInstance('factory_product_resource');
}

/**
* @return Frenet_Shipping_Model_Formatters_Postcode_Normalizer
*/
public function postcodeNormalizer()
{
return $this->getInstance('frenet_shipping/formatters_postcode_normalizer');
return $this->getInstance('formatters_postcode_normalizer');
}

/**
* @return Frenet_Shipping_Model_Validator_Postcode
*/
public function postcodeValidator()
{
return $this->getInstance('validator_postcode');
}

/**
* @return Frenet_Shipping_Model_Delivery_Time_Calculator
*/
public function deliveryTimeCalculator()
{
return $this->getInstance('frenet_shipping/delivery_time_calculator');
return $this->getInstance('delivery_time_calculator');
}

/**
Expand All @@ -238,10 +251,32 @@ public function deliveryTimeCalculator()
*/
private function getInstance($modelClass, array $arguments = array(), $singleton = true)
{
if ($this->canAppendDefaultClassGroup($modelClass)) {
$modelClass = $this->defaultClassGroup . '/' . $modelClass;
}

if (true === $singleton) {
return Mage::getSingleton($modelClass, $arguments);
}

return Mage::getModel($modelClass, $arguments);
}

/**
* @param $classGroup
*
* @return bool
*/
private function canAppendDefaultClassGroup($classGroup)
{
if (strpos($classGroup, '/')) {
return false;
}

if (strpos($classGroup, $this->defaultClassGroup)) {
return false;
}

return true;
}
}
25 changes: 13 additions & 12 deletions app/code/community/Frenet/Shipping/Model/Carrier/Frenet.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ class Frenet_Shipping_Model_Carrier_Frenet extends Mage_Shipping_Model_Carrier_A
*/
private $config;

/**
* @var Frenet_Shipping_Model_Validator_Postcode
*/
private $postcodeValidator;

public function __construct()
{
parent::__construct();
Expand All @@ -87,6 +92,7 @@ public function __construct()
$this->config = $this->objects()->config();
$this->deliveryTimeCalculator = $this->objects()->deliveryTimeCalculator();
$this->postcodeNormalizer = $this->objects()->postcodeNormalizer();
$this->postcodeValidator = $this->objects()->postcodeValidator();
}

/**
Expand Down Expand Up @@ -162,19 +168,14 @@ public function proccessAdditionalValidation(Mage_Shipping_Model_Rate_Request $r
*/
public function processAdditionalValidation(Mage_Shipping_Model_Rate_Request $request)
{
/** Validate request items data */
if (empty($request->getAllItems())) {
$this->errors[] = Mage::helper('frenet_shipping')->__('There is no items in this order');
}

/** Validate destination postcode */
if (!$request->getDestPostcode()) {
$this->errors[] = Mage::helper('frenet_shipping')->__('Please inform the destination postcode');
if (!$this->postcodeValidator->validate($request->getDestPostcode())) {
$this->errors[] = Mage::helper('frenet_shipping')->__('Please inform a valid postcode');
}

/** Validate destination postcode */
if (!((int) $this->postcodeNormalizer->format($request->getDestPostcode()))) {
$this->errors[] = Mage::helper('frenet_shipping')->__('Please inform a valid postcode');
/** Validate request items data */
if (empty($request->getAllItems())) {
$this->errors[] = Mage::helper('frenet_shipping')->__('There is no items in this order');
}

if (!empty($this->errors)) {
Expand Down Expand Up @@ -357,9 +358,9 @@ private function prepareMethod($method, $code, $methodTitle, $price, $cost)
$methodInstance = Mage::getModel('shipping/rate_result_method');
$methodInstance->setCarrier($this->_code)
->setCarrierTitle($this->config->getCarrierConfig('title'))
->setMethod($method)
->setMethod($code)
->setMethodTitle($methodTitle)
->setMethodDescription($code)
->setMethodDescription($method)
->setPrice($price)
->setCost($cost);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class Frenet_Shipping_Model_Formatters_Postcode_Normalizer
*
* @return string
*/
public function format($postcode)
public function format($postcode = null)
{
$postcode = preg_replace('/[^0-9]/', null, $postcode);
$postcode = str_pad($postcode, 8, '0', STR_PAD_LEFT);
Expand Down
Loading

0 comments on commit e57df93

Please sign in to comment.