Skip to content

Commit

Permalink
NTR: refactor (#749)
Browse files Browse the repository at this point in the history
* NTR: refactor

* NTR: fix fetch

* NTR: fix column

---------

Co-authored-by: Vitalij Mik <[email protected]>
  • Loading branch information
BlackScorp and Vitalij Mik authored May 16, 2024
1 parent 4d9aefc commit fcc31a5
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 18 deletions.
27 changes: 13 additions & 14 deletions src/Compatibility/DependencyLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;

class DependencyLoader
Expand All @@ -15,24 +16,26 @@ class DependencyLoader
*/
private $container;

/**
* @var VersionCompare
*/
private $versionCompare;


/**
* @param Container $container
*/
public function __construct(Container $container)
public function __construct(ContainerInterface $container, VersionCompare $versionCompare)
{
$this->container = $container;
$this->versionCompare = $versionCompare;
}

/**
* @throws \Exception
*/
public function loadServices(): void
{
/** @var string $version */
$version = $this->container->getParameter('kernel.shopware_version');

$versionCompare = new VersionCompare($version);


/** @var ContainerBuilder $containerBuilder */
Expand All @@ -44,21 +47,22 @@ public function loadServices(): void
# load Flow Builder
$loader->load('compatibility/flowbuilder/all_versions.xml');

if ($versionCompare->gte('6.4.6.0')) {
if ($this->versionCompare->gte('6.4.6.0')) {
$loader->load('compatibility/flowbuilder/6.4.6.0.xml');
}


$composerDevReqsInstalled = file_exists(__DIR__ . '/../../vendor/bin/phpunit');
$composerDevReqsInstalled = file_exists(__DIR__.'/../../vendor/bin/phpunit');

if ($composerDevReqsInstalled) {
$dirFixtures = __DIR__ . '/../../tests/Fixtures';
$dirFixtures = (string)realpath(__DIR__ . '/../../tests/Fixtures/');

if (is_dir($dirFixtures)) {
# we need to tell Shopware to load our custom fixtures
# from our TEST autoload-dev area....
$classLoader = new ClassLoader();
$classLoader->addPsr4("MolliePayments\\Fixtures\\", $dirFixtures, true);

$classLoader->register();

$loader->load('services/fixtures/fixtures.xml');
Expand All @@ -71,11 +75,6 @@ public function loadServices(): void
*/
public function prepareStorefrontBuild(): void
{
/** @var string $version */
$version = $this->container->getParameter('kernel.shopware_version');

$versionCompare = new VersionCompare($version);

$pluginRoot = __DIR__ . '/../..';

$distFileFolder = $pluginRoot . '/src/Resources/app/storefront/dist/storefront/js';
Expand All @@ -84,7 +83,7 @@ public function prepareStorefrontBuild(): void
mkdir($distFileFolder, 0777, true);
}

if ($versionCompare->gte('6.5')) {
if ($this->versionCompare->gte('6.5')) {
$file = $pluginRoot . '/src/Resources/app/storefront/dist/mollie-payments-65.js';
$target = $distFileFolder . '/mollie-payments.js';
} else {
Expand Down
5 changes: 5 additions & 0 deletions src/Migration/Migration1711618833SubscriptionCurrency.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ public function getCreationTimestamp(): int
public function update(Connection $connection): void
{
$utils = new MigrationUtils($connection);

// sometimes there is a problem with migration, so we check if the currency field was removed, if it not there anymore, we dont need to run the migration
if (!$utils->columnExists('mollie_subscription', 'currency')) {
return;
}
// add new columns
$sql = "ALTER TABLE `mollie_subscription`
ADD COLUMN `currency_id` BINARY(16) NULL AFTER `currency`,
Expand Down
9 changes: 7 additions & 2 deletions src/Migration/MigrationUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function __construct(Connection $connection)
*/
public function createColumn(string $table, string $column, string $type, string $default, string $after): void
{
$colQuery = $this->connection->executeQuery("SHOW COLUMNS FROM " . $table . " LIKE '" . $column . "'")->fetch();
$colQuery = $this->columnExists($table, $column);

# only create if not yet existing
if ($colQuery === false) {
Expand All @@ -57,7 +57,7 @@ public function createColumn(string $table, string $column, string $type, string
*/
public function deleteColumn(string $table, string $column): void
{
$colQuery = $this->connection->executeQuery("SHOW COLUMNS FROM " . $table . " LIKE '" . $column . "'")->fetch();
$colQuery = $this->columnExists($table, $column);

# only delete if existing
if ($colQuery !== false) {
Expand All @@ -66,6 +66,11 @@ public function deleteColumn(string $table, string $column): void
}
}

public function columnExists(string $table, string $column): bool
{
return (bool)$this->connection->executeQuery("SHOW COLUMNS FROM " . $table . " LIKE '" . $column . "'")->fetchOne();
}

/**
* @param string $table
* @param string $keyName
Expand Down
8 changes: 6 additions & 2 deletions src/MolliePayments.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Exception;
use Kiener\MolliePayments\Compatibility\DependencyLoader;
use Kiener\MolliePayments\Compatibility\VersionCompare;
use Kiener\MolliePayments\Components\Installer\PluginInstaller;
use Kiener\MolliePayments\Repository\CustomFieldSet\CustomFieldSetRepository;
use Psr\Container\ContainerInterface;
Expand Down Expand Up @@ -36,10 +37,13 @@ public function build(ContainerBuilder $container): void
parent::build($container);

$this->container = $container;

$shopwareVersion = $this->container->getParameter('kernel.shopware_version');
if (!is_string($shopwareVersion)) {
$shopwareVersion= Kernel::SHOPWARE_FALLBACK_VERSION;
}
# load the dependencies that are compatible
# with our current shopware version
$loader = new DependencyLoader($this->container);
$loader = new DependencyLoader($this->container, new VersionCompare($shopwareVersion));
$loader->loadServices();
$loader->prepareStorefrontBuild();
}
Expand Down

0 comments on commit fcc31a5

Please sign in to comment.