Skip to content

Commit

Permalink
Merge pull request #34 from timmess/SYLIUS-274-maintenance-channels
Browse files Browse the repository at this point in the history
Sylius maintenance channels
  • Loading branch information
oallain committed Jan 5, 2024
2 parents ac5681a + b947caa commit 73c4266
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 12 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/analysis.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ jobs:
fail-fast: false
matrix:
php:
- 8.0
- 8.1
- 8.2
symfony:
- '5.4.*'
- '6.2.*'
- '6.3.*'
env:
APP_ENV: test
steps:
Expand Down
9 changes: 3 additions & 6 deletions .github/workflows/sylius.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,20 @@ jobs:
fail-fast: false
matrix:
php:
- 8.0
- 8.1
- 8.2
sylius:
- 1.10.0
- 1.12.0
symfony:
- 5.4
- 6.2
- 6.3
node:
- 14.x
exclude:
-
sylius: 1.10.0
symfony: 6.2
-
php: '8.0'
symfony: 6.2
symfony: 6.3
env:
APP_ENV: test
package-name: synolia/sylius-maintenance-plugin
Expand Down
34 changes: 30 additions & 4 deletions src/Command/EnableMaintenanceCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace Synolia\SyliusMaintenancePlugin\Command;

use Sylius\Component\Channel\Repository\ChannelRepositoryInterface;
use Sylius\Component\Core\Model\ChannelInterface;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
Expand All @@ -13,6 +15,7 @@
use Synolia\SyliusMaintenancePlugin\Exporter\MaintenanceConfigurationExporter;
use Synolia\SyliusMaintenancePlugin\Factory\MaintenanceConfigurationFactory;
use Synolia\SyliusMaintenancePlugin\FileManager\ConfigurationFileManager;
use Synolia\SyliusMaintenancePlugin\Model\MaintenanceConfiguration;

final class EnableMaintenanceCommand extends Command
{
Expand All @@ -23,6 +26,7 @@ public function __construct(
private MaintenanceConfigurationExporter $maintenanceExporter,
private MaintenanceConfigurationFactory $configurationFactory,
private CacheInterface $synoliaMaintenanceCache,
private ChannelRepositoryInterface $channelRepository,
) {
parent::__construct();
}
Expand All @@ -37,18 +41,40 @@ protected function configure(): void
}

protected function execute(InputInterface $input, OutputInterface $output): int
{
$maintenanceConfiguration = $this->getMaintenanceConfiguration($input);
$this->maintenanceExporter->export($maintenanceConfiguration);
$this->synoliaMaintenanceCache->delete(ConfigurationFileManager::MAINTENANCE_CACHE_KEY);
$output->writeln($this->translator->trans('maintenance.ui.message_enabled'));

return 0;
}

private function getChannels(): array
{
$channels = $this->channelRepository->findAll();
$channelToExport = [];

/** @var ChannelInterface $channel */
foreach ($channels as $channel) {
$channelToExport[] = $channel->getCode();
}

return $channelToExport;
}

private function getMaintenanceConfiguration(InputInterface $input): MaintenanceConfiguration
{
$maintenanceConfiguration = $this->configurationFactory->get();
$maintenanceConfiguration->setChannels($this->getChannels());
$maintenanceConfiguration->setEnabled(true);

/** @var array $ipsAddress */
$ipsAddress = $input->getArgument('ips_address');
if ([] !== $ipsAddress) {
$maintenanceConfiguration->setIpAddresses(implode(',', $ipsAddress));
}
$this->maintenanceExporter->export($maintenanceConfiguration);
$this->synoliaMaintenanceCache->delete(ConfigurationFileManager::MAINTENANCE_CACHE_KEY);
$output->writeln($this->translator->trans('maintenance.ui.message_enabled'));

return 0;
return $maintenanceConfiguration;
}
}
53 changes: 53 additions & 0 deletions tests/PHPUnit/MulitChannelMaintenanceTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

declare(strict_types=1);

namespace Tests\Synolia\SyliusMaintenancePlugin\PHPUnit;

use Sylius\Component\Channel\Repository\ChannelRepositoryInterface;
use Sylius\Component\Core\Model\ChannelInterface;
use Sylius\Component\Core\Test\Services\DefaultChannelFactory;
use Symfony\Component\Yaml\Yaml;

final class MulitChannelMaintenanceTest extends AbstractWebTestCase
{
protected function setUp(): void
{
parent::setUp();

/** @var ChannelRepositoryInterface $channelRepository */
$channelRepository = $this->manager->getRepository(ChannelInterface::class);
/** @var DefaultChannelFactory $channelFactory */
$channelFactory = self::$kernel->getContainer()->get('sylius.behat.factory.default_channel');

// set hostname for actuel channel
$channel = $channelRepository->findOneByCode('FASHION_WEB');
$channel->setHostname('fashion.localhost');

// create a new channel for maintenance
$maintenanceChannel = $channelFactory->create('test', 'Test channel')['channel'];
$maintenanceChannel->setHostname('test.localhost');
$this->manager->persist($maintenanceChannel);

$this->manager->flush();
}

public function testMaintenanceIsNotEnabledWhenFileIsNotEnabled(): void
{
\file_put_contents(
$this->file,
Yaml::dump([
'channels' => [
'FASHION_WEB',
'test',
],
'enabled' => true,
]),
);
self::$client->request('GET', 'http://fashion.localhost/en_US/');
$this->assertSiteIsInMaintenance();

self::$client->request('GET', 'http://test.localhost/en_US/');
$this->assertSiteIsInMaintenance();
}
}

0 comments on commit 73c4266

Please sign in to comment.