Skip to content

Commit 0155bbb

Browse files
mstrzyzewskiMaksymilian Strzyżewski
andauthored
Check if Allegro account is connected before deleting offer mapping (#96)
Co-authored-by: Maksymilian Strzyżewski <m.strzyzewski@macopedia.pl>
1 parent 1f58085 commit 0155bbb

File tree

8 files changed

+20
-25
lines changed

8 files changed

+20
-25
lines changed

Block/Adminhtml/System/Authenticate.php

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,13 @@
77
use Macopedia\Allegro\Model\Api\TokenProvider;
88
use Magento\Backend\Block\Template\Context;
99
use Magento\Config\Block\System\Config\Form\Field;
10-
use Magento\Framework\App\Config\ScopeConfigInterface;
1110
use Magento\Framework\Data\Form\Element\AbstractElement;
12-
use Magento\Framework\Exception\LocalizedException;
1311

1412
/**
1513
* Class responsible for authentication with Allegro API
1614
*/
1715
class Authenticate extends Field
1816
{
19-
/** @var ScopeConfigInterface */
20-
private $scopeConfig;
21-
2217
/** @var TokenProvider */
2318
private $tokenProvider;
2419

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

2823
/**
2924
* @param Context $context
30-
* @param ScopeConfigInterface $scopeConfig
3125
* @param TokenProvider $tokenProvider
3226
* @param Auth $auth
3327
* @param array $data
3428
*/
3529
public function __construct(
3630
Context $context,
37-
ScopeConfigInterface $scopeConfig,
3831
TokenProvider $tokenProvider,
3932
Auth $auth,
4033
array $data = []
4134
) {
42-
$this->scopeConfig = $scopeConfig;
4335
$this->tokenProvider = $tokenProvider;
4436
$this->auth = $auth;
4537
parent::__construct($context, $data);

Console/Command/CleanOffersMapping.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
namespace Macopedia\Allegro\Console\Command;
66

7-
use Macopedia\Allegro\Model\Api\ClientException;
87
use Macopedia\Allegro\Model\OffersMapping;
98
use Magento\Framework\App\Area;
109
use Magento\Framework\App\State;
@@ -52,7 +51,7 @@ protected function configure()
5251
/**
5352
* @param InputInterface $input
5453
* @param OutputInterface $output
55-
* @return int|void
54+
* @return void
5655
* @throws LocalizedException
5756
*/
5857
protected function execute(InputInterface $input, OutputInterface $output)
@@ -65,7 +64,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
6564

6665
try {
6766
$this->offersMapping->clean();
68-
} catch (ClientException $e) {
67+
} catch (\Exception $e) {
6968
$output->writeln('Error occurred while trying to clean old offers mapping');
7069
$output->writeln($e->getMessage());
7170
}

Cron/CleanOffersMapping.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
namespace Macopedia\Allegro\Cron;
66

77
use Macopedia\Allegro\Logger\Logger;
8-
use Macopedia\Allegro\Model\Api\ClientException;
98
use Magento\Framework\App\Config\ScopeConfigInterface;
109
use Macopedia\Allegro\Model\OffersMapping;
1110

@@ -43,7 +42,7 @@ public function execute()
4342
$this->logger->addInfo("Cronjob clean offers mapping is executed.");
4443
try {
4544
$this->offersMapping->clean();
46-
} catch (ClientException $e) {
45+
} catch (\Exception $e) {
4746
$this->logger->error('Error while trying to clean old offers mapping: ' . $e->getMessage());
4847
}
4948
}

Model/Api/Credentials.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Macopedia\Allegro\Model\Api\Auth\Data\TokenSerializer;
77
use Magento\Framework\App\Config\ScopeConfigInterface;
88
use Magento\Framework\App\Config\Storage\WriterInterface;
9+
use Magento\Framework\Exception\LocalizedException;
910
use Magento\Framework\FlagManager;
1011

1112
/**
@@ -110,10 +111,9 @@ public function getToken()
110111
if (!$tokenString) {
111112
throw new ClientException(__('Allegro account is not connected. Connect to Allegro account and try again'));
112113
}
113-
114114
try {
115115
return $this->tokenSerializer->decode($tokenString);
116-
} catch (TokenSerializer\TokenSerializerException $e) {
116+
} catch (Auth\Data\TokenSerializerException $e) {
117117
throw new ClientException(__('Something went wrong while decoding Allegro Api token'));
118118
}
119119
}

Model/Api/TokenProvider.php

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace Macopedia\Allegro\Model\Api;
44

55
use Macopedia\Allegro\Api\Data\TokenInterface;
6-
use Macopedia\Allegro\Logger\Logger;
76

87
/**
98
* Class to get current access token received from Allegro API
@@ -16,22 +15,16 @@ class TokenProvider
1615
/** @var Credentials */
1716
private $credentials;
1817

19-
/** @var Logger */
20-
private $logger;
21-
2218
/**
2319
* @param Auth $auth
2420
* @param Credentials $credentials
25-
* @param Logger $logger
2621
*/
2722
public function __construct(
2823
Auth $auth,
29-
Credentials $credentials,
30-
Logger $logger
24+
Credentials $credentials
3125
) {
3226
$this->auth = $auth;
3327
$this->credentials = $credentials;
34-
$this->logger = $logger;
3528
}
3629

3730
/**

Model/OffersMapping.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Macopedia\Allegro\Model;
66

77
use Macopedia\Allegro\Logger\Logger;
8+
use Macopedia\Allegro\Model\Api\TokenProvider;
89
use Macopedia\Allegro\Model\ResourceModel\Sale\Offers;
910
use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory;
1011

@@ -22,30 +23,39 @@ class OffersMapping
2223
/** @var CollectionFactory */
2324
protected $productCollection;
2425

26+
/** @var TokenProvider */
27+
protected $tokenProvider;
28+
2529
/**
2630
* OffersMapping constructor.
2731
* @param Offers $offers
2832
* @param Logger $logger
2933
* @param Configuration $configuration
3034
* @param CollectionFactory $productCollection
35+
* @param TokenProvider $tokenProvider
3136
*/
3237
public function __construct(
3338
Offers $offers,
3439
Logger $logger,
3540
Configuration $configuration,
36-
CollectionFactory $productCollection
41+
CollectionFactory $productCollection,
42+
TokenProvider $tokenProvider
3743
) {
3844
$this->offers = $offers;
3945
$this->logger = $logger;
4046
$this->configuration = $configuration;
4147
$this->productCollection = $productCollection;
48+
$this->tokenProvider = $tokenProvider;
4249
}
4350

4451
/**
45-
* @throws Api\ClientException
52+
* @throws \Exception
4653
*/
4754
public function clean()
4855
{
56+
//Check connection with Allegro
57+
$this->tokenProvider->getCurrent();
58+
4959
$collection = $this->productCollection->create();
5060
$collection->addAttributeToSelect('*')
5161
->addStoreFilter($this->configuration->getStoreId())

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,8 @@ Oferta wystawiana jest ze zdjęciami pobranymi z produktu.
181181
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.
182182
![publish_offer](README/publishButton.png)
183183

184+
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).
185+
184186
## DEBUG MODE
185187
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)
186188
![debug_mode](README/allegroDebugMode.png)
13.4 KB
Loading

0 commit comments

Comments
 (0)