Skip to content

Commit 8500aaa

Browse files
authored
Merge pull request #55 from shopware5/pt-13155/add-instance-id
PT-13155 - Add instance id
2 parents 73e5dbb + 2481a8d commit 8500aaa

31 files changed

+18745
-18484
lines changed

Components/TransactionReport/TransactionReport.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,11 @@ public function reportOrder($orderId)
4747

4848
/**
4949
* @param string $shopwareVersion
50+
* @param string $instanceId
5051
*
5152
* @return void
5253
*/
53-
public function report($shopwareVersion, Client $client)
54+
public function report($shopwareVersion, $instanceId, Client $client)
5455
{
5556
$reportResult = $this->getReportResult($this->getReportedOrderIds());
5657
$currencies = $reportResult->getCurrencies();
@@ -60,6 +61,7 @@ public function report($shopwareVersion, Client $client)
6061
'identifier' => self::API_IDENTIFIER,
6162
'reportDate' => (new DateTime())->format('Y-m-d\\TH:i:sP'),
6263
'shopwareVersion' => $shopwareVersion,
64+
'instanceId' => $instanceId,
6365
'currency' => $currency,
6466
'reportDataKeys' => ['turnover' => $reportResult->getTurnover($currency)],
6567
];

Setup/Assets/tables.sql

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,3 +150,10 @@ CREATE TABLE IF NOT EXISTS `swag_payment_paypal_unified_transaction_report`
150150
) ENGINE = InnoDB
151151
DEFAULT CHARSET = utf8
152152
COLLATE = utf8_unicode_ci;
153+
154+
CREATE TABLE IF NOT EXISTS `swag_payment_paypal_unified_instance`
155+
(
156+
`instance_id` VARCHAR(36) NOT NULL
157+
) ENGINE = InnoDB
158+
DEFAULT CHARSET = utf8
159+
COLLATE = utf8_unicode_ci;

Setup/Installer.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
namespace SwagPaymentPayPalUnified\Setup;
1010

1111
use Doctrine\DBAL\Connection;
12+
use Exception;
1213
use Shopware\Bundle\AttributeBundle\Service\CrudService;
1314
use Shopware\Bundle\AttributeBundle\Service\CrudServiceInterface;
1415
use Shopware\Bundle\AttributeBundle\Service\TypeMapping;
@@ -108,6 +109,13 @@ public function install()
108109
true
109110
);
110111

112+
try {
113+
// call the instance id service to create the instance id
114+
(new InstanceIdService($this->connection))->getInstanceId();
115+
} catch (Exception $exception) {
116+
// no need to handle this exception
117+
}
118+
111119
return true;
112120
}
113121

Setup/InstanceIdService.php

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?php
2+
/**
3+
* (c) shopware AG <[email protected]>
4+
*
5+
* For the full copyright and license information, please view the LICENSE
6+
* file that was distributed with this source code.
7+
*/
8+
9+
namespace SwagPaymentPayPalUnified\Setup;
10+
11+
use Doctrine\DBAL\Connection;
12+
use RuntimeException;
13+
use SwagPaymentPayPalUnified\Components\Uuid;
14+
15+
final class InstanceIdService
16+
{
17+
/**
18+
* @var Connection
19+
*/
20+
private $connection;
21+
22+
public function __construct(Connection $connection)
23+
{
24+
$this->connection = $connection;
25+
}
26+
27+
/**
28+
* @return string
29+
*/
30+
public function getInstanceId()
31+
{
32+
$instanceId = $this->get();
33+
34+
if ($instanceId === null) {
35+
$instanceId = $this->create();
36+
}
37+
38+
return $instanceId;
39+
}
40+
41+
/**
42+
* @return string|null
43+
*/
44+
private function get()
45+
{
46+
$result = $this->connection->createQueryBuilder()
47+
->select('instance_id')
48+
->from('swag_payment_paypal_unified_instance')
49+
->execute()
50+
->fetchColumn();
51+
52+
if (!$result) {
53+
return null;
54+
}
55+
56+
return $result;
57+
}
58+
59+
/**
60+
* @return string
61+
*/
62+
private function create()
63+
{
64+
$instanceId = Uuid::generateUuid();
65+
66+
$this->connection->createQueryBuilder()
67+
->insert('swag_payment_paypal_unified_instance')
68+
->values(['instance_id' => ':instanceId'])
69+
->setParameter('instanceId', $instanceId)
70+
->execute();
71+
72+
if ($instanceId !== $this->get()) {
73+
throw new RuntimeException('Could not create instance id');
74+
}
75+
76+
return $instanceId;
77+
}
78+
}

Setup/Updater.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
use SwagPaymentPayPalUnified\Setup\Versions\UpdateTo610;
3333
use SwagPaymentPayPalUnified\Setup\Versions\UpdateTo616;
3434
use SwagPaymentPayPalUnified\Setup\Versions\UpdateTo617;
35+
use SwagPaymentPayPalUnified\Setup\Versions\UpdateTo618;
3536

3637
class Updater
3738
{
@@ -256,6 +257,12 @@ public function update($oldVersion)
256257
$this->connection
257258
))->update();
258259
}
260+
261+
if (\version_compare($oldVersion, '6.1.8', '<')) {
262+
(new UpdateTo618(
263+
$this->connection
264+
))->update();
265+
}
259266
}
260267

261268
/**

Setup/Versions/UpdateTo618.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
/**
3+
* (c) shopware AG <[email protected]>
4+
*
5+
* For the full copyright and license information, please view the LICENSE
6+
* file that was distributed with this source code.
7+
*/
8+
9+
namespace SwagPaymentPayPalUnified\Setup\Versions;
10+
11+
use Doctrine\DBAL\Connection;
12+
use Exception;
13+
use SwagPaymentPayPalUnified\Setup\InstanceIdService;
14+
15+
class UpdateTo618
16+
{
17+
/**
18+
* @var Connection
19+
*/
20+
private $connection;
21+
22+
public function __construct(Connection $connection)
23+
{
24+
$this->connection = $connection;
25+
}
26+
27+
/**
28+
* @return void
29+
*/
30+
public function update()
31+
{
32+
$this->createInstanceTable();
33+
34+
try {
35+
(new InstanceIdService($this->connection))->getInstanceId();
36+
} catch (Exception $e) {
37+
// no need to handle this exception
38+
}
39+
}
40+
41+
/**
42+
* @return void
43+
*/
44+
private function createInstanceTable()
45+
{
46+
$this->connection->executeQuery(
47+
'
48+
CREATE TABLE IF NOT EXISTS `swag_payment_paypal_unified_instance`
49+
(
50+
`instance_id` VARCHAR(36) NOT NULL
51+
) ENGINE = InnoDB
52+
DEFAULT CHARSET = utf8
53+
COLLATE = utf8_unicode_ci;'
54+
);
55+
}
56+
}

Subscriber/TransactionReportSubscriber.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@
1010

1111
use Doctrine\DBAL\Connection;
1212
use Enlight\Event\SubscriberInterface;
13+
use Exception;
1314
use GuzzleHttp\Client;
1415
use Shopware;
1516
use SwagPaymentPayPalUnified\Components\TransactionReport\TransactionReport;
17+
use SwagPaymentPayPalUnified\Setup\InstanceIdService;
1618
use Symfony\Component\DependencyInjection\ContainerInterface;
1719

1820
class TransactionReportSubscriber implements SubscriberInterface
@@ -52,8 +54,15 @@ public function onTransactionReport()
5254
$shopwareVersion = $this->container->getParameter('shopware.release.version');
5355
}
5456

57+
try {
58+
$instanceId = (new InstanceIdService($this->connection))->getInstanceId();
59+
} catch (Exception $exception) {
60+
$instanceId = '';
61+
}
62+
5563
(new TransactionReport($this->connection))->report(
5664
$shopwareVersion,
65+
$instanceId,
5766
new Client(['base_uri' => TransactionReport::POST_URL])
5867
);
5968
}

0 commit comments

Comments
 (0)