Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions app/code/MindArc/FeedGenerator/Api/FixerioInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace MindArc\FeedGenerator\Api;

/**
* Interface FixerioInterface
* @api
*/
interface FixerioInterface
{
/**
* @throws \Magento\Framework\Exception\NoSuchEntityException
* @return mixed
*/
public function connectService();
}
52 changes: 52 additions & 0 deletions app/code/MindArc/FeedGenerator/Block/Adminhtml/Config/View.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

namespace MindArc\FeedGenerator\Block\Adminhtml\Config;

use Magento\Backend\Block\Template\Context;

class View
extends \Magento\Config\Block\System\Config\Form\Field
{

const BUTTON_TEMPLATE = 'system/config/view.phtml';

public function __construct(Context $context, $data = array())
{
parent::__construct($context, $data);
}

protected function _prepareLayout()
{
parent::_prepareLayout();
if (!$this->getTemplate()) {
$this->setTemplate(static::BUTTON_TEMPLATE);
}

return $this;
}

public function render(\Magento\Framework\Data\Form\Element\AbstractElement $element)
{
$element->unsScope()->unsCanUseWebsiteValue()->unsCanUseDefaultValue();

return parent::render($element);
}

protected function _getElementHtml(\Magento\Framework\Data\Form\Element\AbstractElement $element)
{
$this->addData(
[
'url' => $this->getUrl(),
'html_id' => $element->getHtmlId(),
]
);

return $this->_toHtml();
}

public function getUrl($route = '', $params = [])
{
return parent::getUrl('generate/feed/view');
}

}
49 changes: 49 additions & 0 deletions app/code/MindArc/FeedGenerator/Controller/Adminhtml/Feed/View.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace MindArc\FeedGenerator\Controller\Adminhtml\Feed;

class View
extends \Magento\Backend\App\Action
{
const XML_TMP_PATH = 'Feeds/';

protected $feed;

protected $messageManager;

protected $feedHelper;

public function __construct(
\MindArc\FeedGenerator\Helper\Data $feedHelper,
\Magento\Framework\Filesystem $filesystem,
\MindArc\FeedGenerator\Model\Feed $feed,
\Magento\Backend\App\Action\Context $context
)
{
$this->feedHelper = $feedHelper;
$this->feed = $feed;
$this->messageManager = $context->getMessageManager();
parent::__construct($context);
}

public function execute()
{
$feedGenerated = $this->feed->generateFeed();

$this->showFeedContents($feedGenerated);

if ($feedGenerated) {
$this->messageManager->addNoticeMessage('Feed has been generated successful');
} else {
$this->messageManager->addErrorMessage('Error to generated the feed');
}
}

protected function showFeedContents($feed)
{
header("Content-Type: application/xml; charset=utf-8");
echo $feed;
}


}
45 changes: 45 additions & 0 deletions app/code/MindArc/FeedGenerator/Cron/Generator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace MindArc\FeedGenerator\Cron;

use Magento\Framework\App\Filesystem\DirectoryList;

class Generator
{
protected $feed;
protected $feedHelper;
protected $mediaDirectory;
protected $logger;

public function __construct(

\MindArc\FeedGenerator\Model\Feed $feed,
\MindArc\FeedGenerator\Helper\Data $feedHelper,
\Magento\Framework\Filesystem $filesystem,
\Psr\Log\LoggerInterface $logger
)
{
$this->feed = $feed;
$this->feedHelper = $feedHelper;
$this->logger = $logger;
$this->mediaDirectory = $filesystem->getDirectoryWrite(DirectoryList::MEDIA);
}

public function execute()
{
$this->logger->info('Feed generation started');

$feedGenerated = $this->feed->generateFeed();

$filename = $this->feedHelper->getFileName('feed');
$path = $this->feedHelper->getFeedPath();

$result = $this->mediaDirectory->writeFile($path . $filename, $feedGenerated);

if (!$result) {
$this->logger->error('The feed has not been generated');
} else {
$this->logger->info('Feed generation finished');
}
}
}
75 changes: 75 additions & 0 deletions app/code/MindArc/FeedGenerator/Helper/Data.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

namespace MindArc\FeedGenerator\Helper;

class Data
extends \Magento\Framework\App\Helper\AbstractHelper
{
const XML_TMP_PATH = 'Feeds/';

protected $storeManager;
protected $dateTime;

public function __construct(
\Magento\Framework\Stdlib\DateTime\DateTime $dateTime,
\Magento\Framework\App\Helper\Context $context,
\Magento\Store\Model\StoreManagerInterface $storeManager
)
{
parent::__construct($context);
$this->dateTime = $dateTime;
$this->storeManager = $storeManager;
}

public function getImageLink($product)
{
$urlImage = $this->storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA) . 'catalog/product' .
$product->getImage();

return $urlImage;
}

public function getWebLink()
{
return $this->storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_WEB);
}

public function getPrice($product)
{
return $product->getPriceInfo()->getPrice('final_price')->getAmount()->getValue();
}

/*
* This is could be a custom attribute product. Magento has not condition attribute for products
* so you can write your custom code here
*/
public function getProductAttribute($product, $attributeName = 'condition')
{
if ($attributeName == 'condition') {
return 'new';
} else {
$attributeValue = $product->getData($attributeName);
if (!empty($attributeValue)) {
return $attributeValue;
}
}

return '';

}

public function getFileName($filename, $format = 'xml')
{
$filename = $filename . '_';
$filename .= $this->dateTime->gmtDate('d-m-Y_h:m:i');
$filename .= '.' . $format;

return $filename;
}

public function getFeedPath()
{
return self::XML_TMP_PATH;
}

}
39 changes: 39 additions & 0 deletions app/code/MindArc/FeedGenerator/Helper/Fixerio.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace MindArc\FeedGenerator\Helper;

class Fixerio
extends \Magento\Framework\App\Helper\AbstractHelper
{
const XML_TMP_PATH = 'Feeds/';

protected $api;
protected $response;

public function __construct(
\MindArc\FeedGenerator\Model\Fixerio $api,
\Magento\Framework\App\Helper\Context $context
)
{
parent::__construct($context);
$this->api = $api;
$this->response = [];
}

public function convert($base, $currency, $amount)
{
if (!count($this->response)) {//it avoids always consult the services
$this->response = $this->api->connectService();
}
$rates = get_object_vars($this->response->rates);
$rate = isset($rates[$currency]) ? $rates[$currency] : 1;

if ($base == $this->response->base) {
$convertedAmount = $amount;
} else {
$convertedAmount = $amount * $rate;
}

return $convertedAmount;
}
}
Loading