Skip to content

Commit

Permalink
Use css.twig file instead of txt file
Browse files Browse the repository at this point in the history
  • Loading branch information
Roshyo committed Mar 9, 2022
1 parent 45a3cf8 commit a7fe2f6
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 29 deletions.
22 changes: 5 additions & 17 deletions src/DependencyInjection/SetonoSyliusGiftCardExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,11 @@ public function load(array $configs, ContainerBuilder $container): void

$container->setParameter('setono_sylius_gift_card.code_length', $config['code_length']);

// Load default CSS file path
if ($container->hasParameter('kernel.project_dir')) {
/**
* @psalm-suppress UndefinedDocblockClass
*
* @var string $kernelProjectDir
*/
$kernelProjectDir = $container->getParameter('kernel.project_dir');
$fileLocator = new FileLocator([
$kernelProjectDir . '/private',
__DIR__ . '/../Resources/private',
]);
$container->setParameter(
'setono_sylius_gift_card.default_css_file_path',
$fileLocator->locate('default-gift-card-configuration-css.txt')
);
}
// Load default CSS file
$container->setParameter(
'setono_sylius_gift_card.default_css_file',
'@SetonoSyliusGiftCardPlugin/Shop/GiftCard/defaultGiftCardConfiguration.css.twig'
);

$this->registerResources('setono_sylius_gift_card', $config['driver'], $config['resources'], $container);

Expand Down
20 changes: 9 additions & 11 deletions src/Provider/DefaultPdfCssProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,22 @@

namespace Setono\SyliusGiftCardPlugin\Provider;

use Twig\Environment;

final class DefaultPdfCssProvider implements DefaultPdfCssProviderInterface
{
private string $defaultCssFilePath;
private string $defaultCssFile;

private Environment $twig;

public function __construct(string $defaultCssFilePath)
public function __construct(string $defaultCssFile, Environment $twig)
{
$this->defaultCssFilePath = $defaultCssFilePath;
$this->defaultCssFile = $defaultCssFile;
$this->twig = $twig;
}

public function getDefaultCss(): string
{
// TODO: When dropping Symfony 4.4, switch to Symfony File class
$content = file_get_contents($this->defaultCssFilePath);

if (false === $content) {
return '';
}

return $content;
return $this->twig->render($this->defaultCssFile);
}
}
3 changes: 2 additions & 1 deletion src/Resources/config/services/provider.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@

<service id="setono_sylius_gift_card.provider.default_pdf_css"
class="Setono\SyliusGiftCardPlugin\Provider\DefaultPdfCssProvider">
<argument>%setono_sylius_gift_card.default_css_file_path%</argument>
<argument>%setono_sylius_gift_card.default_css_file%</argument>
<argument type="service" id="twig"/>
</service>
<service id="Setono\SyliusGiftCardPlugin\Provider\DefaultPdfCssProviderInterface"
alias="setono_sylius_gift_card.provider.default_pdf_css"/>
Expand Down
30 changes: 30 additions & 0 deletions tests/Unit/Provider/DefaultPdfCssProviderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

namespace Tests\Setono\SyliusGiftCardPlugin\Unit\Provider;

use PHPUnit\Framework\TestCase;
use Prophecy\PhpUnit\ProphecyTrait;
use Setono\SyliusGiftCardPlugin\Provider\DefaultPdfCssProvider;
use Twig\Environment;

final class DefaultPdfCssProviderTest extends TestCase
{
use ProphecyTrait;

/**
* @test
*/
public function it_provides_default_css_from_template(): void
{
$css = 'body{font-size: 2rem;}';
$cssTemplateFile = '@Super/template.css.twig';
$twig = $this->prophesize(Environment::class);
$twig->render($cssTemplateFile)->willReturn($css);

$defaultPdfCssProvider = new DefaultPdfCssProvider($cssTemplateFile, $twig->reveal());

$this->assertEquals($css, $defaultPdfCssProvider->getDefaultCss());
}
}

0 comments on commit a7fe2f6

Please sign in to comment.