Skip to content

Commit

Permalink
Check if Allegro account is connected before deleting offer mapping (#96
Browse files Browse the repository at this point in the history
)

Co-authored-by: Maksymilian Strzyżewski <[email protected]>
  • Loading branch information
mstrzyzewski and Maksymilian Strzyżewski authored May 27, 2021
1 parent 1f58085 commit 0155bbb
Show file tree
Hide file tree
Showing 8 changed files with 20 additions and 25 deletions.
8 changes: 0 additions & 8 deletions Block/Adminhtml/System/Authenticate.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,13 @@
use Macopedia\Allegro\Model\Api\TokenProvider;
use Magento\Backend\Block\Template\Context;
use Magento\Config\Block\System\Config\Form\Field;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\Data\Form\Element\AbstractElement;
use Magento\Framework\Exception\LocalizedException;

/**
* Class responsible for authentication with Allegro API
*/
class Authenticate extends Field
{
/** @var ScopeConfigInterface */
private $scopeConfig;

/** @var TokenProvider */
private $tokenProvider;

Expand All @@ -27,19 +22,16 @@ class Authenticate extends Field

/**
* @param Context $context
* @param ScopeConfigInterface $scopeConfig
* @param TokenProvider $tokenProvider
* @param Auth $auth
* @param array $data
*/
public function __construct(
Context $context,
ScopeConfigInterface $scopeConfig,
TokenProvider $tokenProvider,
Auth $auth,
array $data = []
) {
$this->scopeConfig = $scopeConfig;
$this->tokenProvider = $tokenProvider;
$this->auth = $auth;
parent::__construct($context, $data);
Expand Down
5 changes: 2 additions & 3 deletions Console/Command/CleanOffersMapping.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace Macopedia\Allegro\Console\Command;

use Macopedia\Allegro\Model\Api\ClientException;
use Macopedia\Allegro\Model\OffersMapping;
use Magento\Framework\App\Area;
use Magento\Framework\App\State;
Expand Down Expand Up @@ -52,7 +51,7 @@ protected function configure()
/**
* @param InputInterface $input
* @param OutputInterface $output
* @return int|void
* @return void
* @throws LocalizedException
*/
protected function execute(InputInterface $input, OutputInterface $output)
Expand All @@ -65,7 +64,7 @@ protected function execute(InputInterface $input, OutputInterface $output)

try {
$this->offersMapping->clean();
} catch (ClientException $e) {
} catch (\Exception $e) {
$output->writeln('Error occurred while trying to clean old offers mapping');
$output->writeln($e->getMessage());
}
Expand Down
3 changes: 1 addition & 2 deletions Cron/CleanOffersMapping.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace Macopedia\Allegro\Cron;

use Macopedia\Allegro\Logger\Logger;
use Macopedia\Allegro\Model\Api\ClientException;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Macopedia\Allegro\Model\OffersMapping;

Expand Down Expand Up @@ -43,7 +42,7 @@ public function execute()
$this->logger->addInfo("Cronjob clean offers mapping is executed.");
try {
$this->offersMapping->clean();
} catch (ClientException $e) {
} catch (\Exception $e) {
$this->logger->error('Error while trying to clean old offers mapping: ' . $e->getMessage());
}
}
Expand Down
4 changes: 2 additions & 2 deletions Model/Api/Credentials.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Macopedia\Allegro\Model\Api\Auth\Data\TokenSerializer;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\App\Config\Storage\WriterInterface;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\FlagManager;

/**
Expand Down Expand Up @@ -110,10 +111,9 @@ public function getToken()
if (!$tokenString) {
throw new ClientException(__('Allegro account is not connected. Connect to Allegro account and try again'));
}

try {
return $this->tokenSerializer->decode($tokenString);
} catch (TokenSerializer\TokenSerializerException $e) {
} catch (Auth\Data\TokenSerializerException $e) {
throw new ClientException(__('Something went wrong while decoding Allegro Api token'));
}
}
Expand Down
9 changes: 1 addition & 8 deletions Model/Api/TokenProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Macopedia\Allegro\Model\Api;

use Macopedia\Allegro\Api\Data\TokenInterface;
use Macopedia\Allegro\Logger\Logger;

/**
* Class to get current access token received from Allegro API
Expand All @@ -16,22 +15,16 @@ class TokenProvider
/** @var Credentials */
private $credentials;

/** @var Logger */
private $logger;

/**
* @param Auth $auth
* @param Credentials $credentials
* @param Logger $logger
*/
public function __construct(
Auth $auth,
Credentials $credentials,
Logger $logger
Credentials $credentials
) {
$this->auth = $auth;
$this->credentials = $credentials;
$this->logger = $logger;
}

/**
Expand Down
14 changes: 12 additions & 2 deletions Model/OffersMapping.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Macopedia\Allegro\Model;

use Macopedia\Allegro\Logger\Logger;
use Macopedia\Allegro\Model\Api\TokenProvider;
use Macopedia\Allegro\Model\ResourceModel\Sale\Offers;
use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory;

Expand All @@ -22,30 +23,39 @@ class OffersMapping
/** @var CollectionFactory */
protected $productCollection;

/** @var TokenProvider */
protected $tokenProvider;

/**
* OffersMapping constructor.
* @param Offers $offers
* @param Logger $logger
* @param Configuration $configuration
* @param CollectionFactory $productCollection
* @param TokenProvider $tokenProvider
*/
public function __construct(
Offers $offers,
Logger $logger,
Configuration $configuration,
CollectionFactory $productCollection
CollectionFactory $productCollection,
TokenProvider $tokenProvider
) {
$this->offers = $offers;
$this->logger = $logger;
$this->configuration = $configuration;
$this->productCollection = $productCollection;
$this->tokenProvider = $tokenProvider;
}

/**
* @throws Api\ClientException
* @throws \Exception
*/
public function clean()
{
//Check connection with Allegro
$this->tokenProvider->getCurrent();

$collection = $this->productCollection->create();
$collection->addAttributeToSelect('*')
->addStoreFilter($this->configuration->getStoreId())
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,8 @@ Oferta wystawiana jest ze zdjęciami pobranymi z produktu.
Po uzupełnieniu wszystkich pół i kliknięciu "Zapisz" - zostanie wystawiony szkic oferty na Allegro i zostaniemy przekierowani na stronę edycji oferty. Teraz wystarczy kliknąć "Opublikuj", aby oferta stała się aktywna. W każdej chwili możemy edytować ofertę, zakończyć ją, a potem następnie aktywować. Produkt jest już teraz powiązany z ofertą na Allegro.
![publish_offer](README/publishButton.png)

W konfiguracji jest opcja włączenia zadania cron, które będzie usuwać z produktu ID oferty, która już nie istnieje na Allegro (Sklepy->Konfiguracja->Allegro->Konfiguracja->Import zamówień->Cron do czyszczenia starych rezerwacji jest włączony).

## DEBUG MODE
Wtyczka oferuje możliwość logowania wszystkich danych przesyłanych do i z API Allegro. Włączyć ją można na stronie konfiguracji (sklepy->Konfiguracja->Allegro->Konfiguracja->Debug mode)
![debug_mode](README/allegroDebugMode.png)
Expand Down
Binary file modified README/allegroOrderImporterConfiguration.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 0155bbb

Please sign in to comment.