Passed
Push — master ( 43311e...19eae0 )
by Florian
04:43
created

EncoreEmailTwigExtension::getFunctions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
/*
4
 * This file is part of the TheAlternativeZurich/events project.
5
 *
6
 * (c) Florian Moser <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace App\Extension;
13
14
use Psr\Container\ContainerInterface;
15
use Symfony\Contracts\Service\ServiceSubscriberInterface;
16
use Symfony\WebpackEncoreBundle\Asset\EntrypointLookupInterface;
17
use Twig\Extension\AbstractExtension;
18
use Twig\TwigFunction;
19
20
class EncoreEmailTwigExtension extends AbstractExtension implements ServiceSubscriberInterface
21
{
22
    private $container;
23
    private $publicDir;
24
25
    public function __construct(ContainerInterface $container, string $publicDir)
26
    {
27
        $this->container = $container;
28
        $this->publicDir = $publicDir;
29
    }
30
31
    public function getFunctions(): array
32
    {
33
        return [
34
            new TwigFunction('encore_entry_css_source', [$this, 'getEncoreEntryCssSource']),
35
        ];
36
    }
37
38
    public function getEncoreEntryCssSource(string $entryName): string
39
    {
40
        $files = $this->container
41
            ->get(EntrypointLookupInterface::class)
42
            ->getCssFiles($entryName);
43
44
        $source = '';
45
        foreach ($files as $file) {
46
            $source .= file_get_contents($this->publicDir.'/'.$file);
47
        }
48
49
        return $source;
50
    }
51
52
    public static function getSubscribedServices()
53
    {
54
        return [
55
            EntrypointLookupInterface::class,
56
        ];
57
    }
58
}
59