Skip to content

Instantly share code, notes, and snippets.

@St0iK
Created October 21, 2016 23:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save St0iK/c11b21e74441c3b49a7b2e2635dcaeb7 to your computer and use it in GitHub Desktop.
Save St0iK/c11b21e74441c3b49a7b2e2635dcaeb7 to your computer and use it in GitHub Desktop.
<?php
namespace Creode\Deliveryday\Model;
use Magento\Checkout\Model\ConfigProviderInterface;
use Magento\Store\Model\ScopeInterface;
class DeliverydayConfigProvider implements ConfigProviderInterface
{
const XPATH_ENABLE_COMMENT_FIELD = 'creode_deliveryday/general/enable_comment_field';
const XPATH_DESCRIPTION = 'creode_deliveryday/general/description';
const XPATH_NEXT_DAYS = 'creode_deliveryday/general/next_days';
const XPATH_DELIVER_ON_SATURDAY = 'creode_deliveryday/general/deliver_on_saturday';
const XPATH_DELIVER_ON_SUNDAY = 'creode_deliveryday/general/deliver_on_sunday';
const XPATH_TIME_FOR_DELIVERY = 'creode_deliveryday/general/time_for_today';
const XPATH_DISABLED_DELIVERY_DATES = 'creode_deliveryday/general/disable_delivery_dates';
/**
* @var \Magento\Store\Model\StoreManagerInterface
*/
protected $storeManager;
/**
* @var \Magento\Framework\App\Config\ScopeConfigInterface
*/
protected $scopeConfig;
protected $_deliverydayFactory;
/**
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
* @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
*/
public function __construct(
\Magento\Store\Model\StoreManagerInterface $storeManager,
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
\Creode\Deliveryday\Model\DeliverydayFactory $deliverydayFactory
) {
$this->storeManager = $storeManager;
$this->scopeConfig = $scopeConfig;
$this->_deliverydayFactory = $deliverydayFactory;
}
/**
* Exposes the Delivery Day information supplied in the backend
* but the users. These values are exposed to window.checkoutConfig in javascript
* and can be used in our uiCompoment code in hs
*/
public function getConfig()
{
$configurationValues = $this->_deliveryDayConfigurationValues();
$deliveryDayPricing = $this->_deliveryDayPricingInformation();
return [
'delivery_day' => [
'config' => [
$configurationValues
],
'pricing' => $deliveryDayPricing
],
];
}
/**
* Returns an array containing all the Configuration Values
* in key/value pairs
* @return Array
*/
private function _deliveryDayConfigurationValues()
{
$configurationValues = [];
$configurationValues['enable_comment_field'] = $this->_getSystemConfigValue(self::XPATH_ENABLE_COMMENT_FIELD);
$configurationValues['description'] = $this->_getSystemConfigValue(self::XPATH_DESCRIPTION);
$configurationValues['next_days'] = $this->_getSystemConfigValue(self::XPATH_NEXT_DAYS);
$configurationValues['deliver_on_saturday'] = $this->_getSystemConfigValue(self::XPATH_DELIVER_ON_SATURDAY);
$configurationValues['deliver_on_sunday'] = $this->_getSystemConfigValue(self::XPATH_DELIVER_ON_SUNDAY);
$configurationValues['time_for_today'] = $this->_getSystemConfigValue(self::XPATH_TIME_FOR_DELIVERY);
$configurationValues['disable_delivery_dates'] =
$this->_getDisabledDaysFormatted($this->_getSystemConfigValue(self::XPATH_DISABLED_DELIVERY_DATES));
return $configurationValues;
}
/**
* Returns the current store id
* @return [type] [description]
*/
private function _currentStoreId()
{
return $this->storeManager->getStore()->getStoreId();
}
/**
* Creates an array with all the pricing information
* for every weekday as defined in the backend
* @return Array with weekdays/prices as key/value
*/
private function _deliveryDayPricingInformation()
{
$deliveryDayCollection = $this->_deliverydayFactory->create()->getCollection();
$deliveryDayPricing = [];
foreach($deliveryDayCollection as $deliveryday){
$deliveryDayPricing[] = $deliveryday->getData();
}
return $deliveryDayPricing;
}
/**
* Creates an array of the dates
* that will be disabled
* for the Delivery Date, day picker
*
* @param $disable_delivery_dates Serialised array containg the
* values entered from the user for the dates that will
* be disabled
* @return Array with dates
*/
private function _getDisabledDaysFormatted($disable_delivery_dates)
{
$disabledDays = [];
$unserialized_disable_delivery_dates = unserialize($disable_delivery_dates);
foreach($unserialized_disable_delivery_dates as $disabledDay){
$disabledDays[] = $disabledDay['text'];
}
return $disabledDays;
}
/**
* Small helper function to the the System store value by key
* Just makes code look nicer in the @_deliveryDayConfigurationValues function
* @param $key
* @param $scope ScopeInterface
* @return the config value for the key asked
*/
private function _getSystemConfigValue($key, $scope = ScopeInterface::SCOPE_STORE)
{
return $this->scopeConfig->getValue($key, $scope, $this->_currentStoreId());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment