Skip to content

Commit

Permalink
Merge pull request #55 from shopware5/pt-13155/add-instance-id
Browse files Browse the repository at this point in the history
PT-13155 - Add instance id
  • Loading branch information
mitelg committed Jun 14, 2024
2 parents 73e5dbb + 2481a8d commit 8500aaa
Show file tree
Hide file tree
Showing 31 changed files with 18,745 additions and 18,484 deletions.
4 changes: 3 additions & 1 deletion Components/TransactionReport/TransactionReport.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,11 @@ public function reportOrder($orderId)

/**
* @param string $shopwareVersion
* @param string $instanceId
*
* @return void
*/
public function report($shopwareVersion, Client $client)
public function report($shopwareVersion, $instanceId, Client $client)
{
$reportResult = $this->getReportResult($this->getReportedOrderIds());
$currencies = $reportResult->getCurrencies();
Expand All @@ -60,6 +61,7 @@ public function report($shopwareVersion, Client $client)
'identifier' => self::API_IDENTIFIER,
'reportDate' => (new DateTime())->format('Y-m-d\\TH:i:sP'),
'shopwareVersion' => $shopwareVersion,
'instanceId' => $instanceId,
'currency' => $currency,
'reportDataKeys' => ['turnover' => $reportResult->getTurnover($currency)],
];
Expand Down
7 changes: 7 additions & 0 deletions Setup/Assets/tables.sql
Original file line number Diff line number Diff line change
Expand Up @@ -150,3 +150,10 @@ CREATE TABLE IF NOT EXISTS `swag_payment_paypal_unified_transaction_report`
) ENGINE = InnoDB
DEFAULT CHARSET = utf8
COLLATE = utf8_unicode_ci;

CREATE TABLE IF NOT EXISTS `swag_payment_paypal_unified_instance`
(
`instance_id` VARCHAR(36) NOT NULL
) ENGINE = InnoDB
DEFAULT CHARSET = utf8
COLLATE = utf8_unicode_ci;
8 changes: 8 additions & 0 deletions Setup/Installer.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
namespace SwagPaymentPayPalUnified\Setup;

use Doctrine\DBAL\Connection;
use Exception;
use Shopware\Bundle\AttributeBundle\Service\CrudService;
use Shopware\Bundle\AttributeBundle\Service\CrudServiceInterface;
use Shopware\Bundle\AttributeBundle\Service\TypeMapping;
Expand Down Expand Up @@ -108,6 +109,13 @@ public function install()
true
);

try {
// call the instance id service to create the instance id
(new InstanceIdService($this->connection))->getInstanceId();
} catch (Exception $exception) {
// no need to handle this exception
}

return true;
}

Expand Down
78 changes: 78 additions & 0 deletions Setup/InstanceIdService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php
/**
* (c) shopware AG <info@shopware.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace SwagPaymentPayPalUnified\Setup;

use Doctrine\DBAL\Connection;
use RuntimeException;
use SwagPaymentPayPalUnified\Components\Uuid;

final class InstanceIdService
{
/**
* @var Connection
*/
private $connection;

public function __construct(Connection $connection)
{
$this->connection = $connection;
}

/**
* @return string
*/
public function getInstanceId()
{
$instanceId = $this->get();

if ($instanceId === null) {
$instanceId = $this->create();
}

return $instanceId;
}

/**
* @return string|null
*/
private function get()
{
$result = $this->connection->createQueryBuilder()
->select('instance_id')
->from('swag_payment_paypal_unified_instance')
->execute()
->fetchColumn();

if (!$result) {
return null;
}

return $result;
}

/**
* @return string
*/
private function create()
{
$instanceId = Uuid::generateUuid();

$this->connection->createQueryBuilder()
->insert('swag_payment_paypal_unified_instance')
->values(['instance_id' => ':instanceId'])
->setParameter('instanceId', $instanceId)
->execute();

if ($instanceId !== $this->get()) {
throw new RuntimeException('Could not create instance id');
}

return $instanceId;
}
}
7 changes: 7 additions & 0 deletions Setup/Updater.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
use SwagPaymentPayPalUnified\Setup\Versions\UpdateTo610;
use SwagPaymentPayPalUnified\Setup\Versions\UpdateTo616;
use SwagPaymentPayPalUnified\Setup\Versions\UpdateTo617;
use SwagPaymentPayPalUnified\Setup\Versions\UpdateTo618;

class Updater
{
Expand Down Expand Up @@ -256,6 +257,12 @@ public function update($oldVersion)
$this->connection
))->update();
}

if (\version_compare($oldVersion, '6.1.8', '<')) {
(new UpdateTo618(
$this->connection
))->update();
}
}

/**
Expand Down
56 changes: 56 additions & 0 deletions Setup/Versions/UpdateTo618.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php
/**
* (c) shopware AG <info@shopware.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace SwagPaymentPayPalUnified\Setup\Versions;

use Doctrine\DBAL\Connection;
use Exception;
use SwagPaymentPayPalUnified\Setup\InstanceIdService;

class UpdateTo618
{
/**
* @var Connection
*/
private $connection;

public function __construct(Connection $connection)
{
$this->connection = $connection;
}

/**
* @return void
*/
public function update()
{
$this->createInstanceTable();

try {
(new InstanceIdService($this->connection))->getInstanceId();
} catch (Exception $e) {
// no need to handle this exception
}
}

/**
* @return void
*/
private function createInstanceTable()
{
$this->connection->executeQuery(
'
CREATE TABLE IF NOT EXISTS `swag_payment_paypal_unified_instance`
(
`instance_id` VARCHAR(36) NOT NULL
) ENGINE = InnoDB
DEFAULT CHARSET = utf8
COLLATE = utf8_unicode_ci;'
);
}
}
9 changes: 9 additions & 0 deletions Subscriber/TransactionReportSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@

use Doctrine\DBAL\Connection;
use Enlight\Event\SubscriberInterface;
use Exception;
use GuzzleHttp\Client;
use Shopware;
use SwagPaymentPayPalUnified\Components\TransactionReport\TransactionReport;
use SwagPaymentPayPalUnified\Setup\InstanceIdService;
use Symfony\Component\DependencyInjection\ContainerInterface;

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

try {
$instanceId = (new InstanceIdService($this->connection))->getInstanceId();
} catch (Exception $exception) {
$instanceId = '';
}

(new TransactionReport($this->connection))->report(
$shopwareVersion,
$instanceId,
new Client(['base_uri' => TransactionReport::POST_URL])
);
}
Expand Down
Loading

0 comments on commit 8500aaa

Please sign in to comment.