Skip to content

Commit cc34319

Browse files
committed
Added a ping check to commands. This avoids a lot of unnecessary calls if the API key is not set or invalid
1 parent 9e7a834 commit cc34319

File tree

6 files changed

+37
-3
lines changed

6 files changed

+37
-3
lines changed

src/Client/Client.php

+5
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,11 @@ public function subscribeEmail(AudienceInterface $audience, string $email): void
157157
);
158158
}
159159

160+
public function ping(): void
161+
{
162+
$this->makeRequest('get', 'ping');
163+
}
164+
160165
private function hasOrder(string $storeId, string $orderId): bool
161166
{
162167
try {

src/Client/ClientInterface.php

+5
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,9 @@ public function updateMember(AudienceInterface $audience, CustomerInterface $cus
4343
* @throws ClientException
4444
*/
4545
public function subscribeEmail(AudienceInterface $audience, string $email): void;
46+
47+
/**
48+
* Will try to ping Mailchimp. If the API key is invalid or something else goes wrong, this will throw an exception
49+
*/
50+
public function ping(): void;
4651
}

src/Command/LoadAudiencesCommand.php

+8-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Setono\SyliusMailchimpPlugin\Command;
66

7+
use Setono\SyliusMailchimpPlugin\Client\Client;
78
use Setono\SyliusMailchimpPlugin\Loader\AudiencesLoaderInterface;
89
use Symfony\Component\Console\Command\Command;
910
use Symfony\Component\Console\Command\LockableTrait;
@@ -17,13 +18,17 @@ final class LoadAudiencesCommand extends Command
1718

1819
protected static $defaultName = 'setono:sylius-mailchimp:load-audiences';
1920

21+
/** @var Client */
22+
private $client;
23+
2024
/** @var AudiencesLoaderInterface */
2125
protected $audiencesLoader;
2226

23-
public function __construct(AudiencesLoaderInterface $audiencesLoader)
27+
public function __construct(Client $client, AudiencesLoaderInterface $audiencesLoader)
2428
{
2529
parent::__construct();
2630

31+
$this->client = $client;
2732
$this->audiencesLoader = $audiencesLoader;
2833
}
2934

@@ -48,6 +53,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int
4853
return 0;
4954
}
5055

56+
$this->client->ping();
57+
5158
$preserve = (bool) $input->getOption('preserve');
5259
$this->audiencesLoader->load($preserve);
5360

src/Command/PushCustomersCommand.php

+8-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Setono\SyliusMailchimpPlugin\Command;
66

7+
use Setono\SyliusMailchimpPlugin\Client\Client;
78
use Setono\SyliusMailchimpPlugin\Message\Command\PushCustomers;
89
use Symfony\Component\Console\Command\Command;
910
use Symfony\Component\Console\Command\LockableTrait;
@@ -17,13 +18,17 @@ final class PushCustomersCommand extends Command
1718

1819
protected static $defaultName = 'setono:sylius-mailchimp:push-customers';
1920

21+
/** @var Client */
22+
private $client;
23+
2024
/** @var MessageBusInterface */
2125
private $commandBus;
2226

23-
public function __construct(MessageBusInterface $commandBus)
27+
public function __construct(Client $client, MessageBusInterface $commandBus)
2428
{
2529
parent::__construct();
2630

31+
$this->client = $client;
2732
$this->commandBus = $commandBus;
2833
}
2934

@@ -41,6 +46,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int
4146
return 0;
4247
}
4348

49+
$this->client->ping();
50+
4451
$this->commandBus->dispatch(new PushCustomers());
4552

4653
$this->release();

src/Command/PushOrdersCommand.php

+8-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Setono\SyliusMailchimpPlugin\Command;
66

7+
use Setono\SyliusMailchimpPlugin\Client\Client;
78
use Setono\SyliusMailchimpPlugin\Message\Command\PushOrders;
89
use Symfony\Component\Console\Command\Command;
910
use Symfony\Component\Console\Command\LockableTrait;
@@ -17,13 +18,17 @@ final class PushOrdersCommand extends Command
1718

1819
protected static $defaultName = 'setono:sylius-mailchimp:push-orders';
1920

21+
/** @var Client */
22+
private $client;
23+
2024
/** @var MessageBusInterface */
2125
private $commandBus;
2226

23-
public function __construct(MessageBusInterface $commandBus)
27+
public function __construct(Client $client, MessageBusInterface $commandBus)
2428
{
2529
parent::__construct();
2630

31+
$this->client = $client;
2732
$this->commandBus = $commandBus;
2833
}
2934

@@ -42,6 +47,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int
4247
return 0;
4348
}
4449

50+
$this->client->ping();
51+
4552
$this->commandBus->dispatch(new PushOrders());
4653

4754
$this->release();

src/Resources/config/services/command.xml

+3
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,23 @@
77

88
<service id="setono_sylius_mailchimp.command.load_audiences"
99
class="Setono\SyliusMailchimpPlugin\Command\LoadAudiencesCommand">
10+
<argument type="service" id="setono_sylius_mailchimp.client"/>
1011
<argument type="service" id="setono_sylius_mailchimp.loader.audiences"/>
1112

1213
<tag name="console.command"/>
1314
</service>
1415

1516
<service id="setono_sylius_mailchimp.command.push_customers"
1617
class="Setono\SyliusMailchimpPlugin\Command\PushCustomersCommand">
18+
<argument type="service" id="setono_sylius_mailchimp.client"/>
1719
<argument type="service" id="setono_sylius_mailchimp.command_bus"/>
1820

1921
<tag name="console.command"/>
2022
</service>
2123

2224
<service id="setono_sylius_mailchimp.command.push_orders"
2325
class="Setono\SyliusMailchimpPlugin\Command\PushOrdersCommand">
26+
<argument type="service" id="setono_sylius_mailchimp.client"/>
2427
<argument type="service" id="setono_sylius_mailchimp.command_bus"/>
2528

2629
<tag name="console.command"/>

0 commit comments

Comments
 (0)