EncoreEmailTwigExtension   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 7
eloc 20
c 2
b 0
f 0
dl 0
loc 42
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getSubscribedServices() 0 4 1
A getEncoreEntryCssSource() 0 16 4
A __construct() 0 5 1
A getFunctions() 0 4 1
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
    private $environment;
25
26
    public function __construct(ContainerInterface $container, string $publicDir, string $environment)
27
    {
28
        $this->container = $container;
29
        $this->publicDir = $publicDir;
30
        $this->environment = $environment;
31
    }
32
33
    public function getFunctions(): array
34
    {
35
        return [
36
            new TwigFunction('encore_entry_css_source', [$this, 'getEncoreEntryCssSource']),
37
        ];
38
    }
39
40
    public function getEncoreEntryCssSource(string $entryName): string
41
    {
42
        $files = $this->container
43
            ->get(EntrypointLookupInterface::class)
44
            ->getCssFiles($entryName);
45
46
        $source = '';
47
        foreach ($files as $file) {
48
            if ('dev' === $this->environment && 0 === strpos($file, 'https://localhost:8080/')) {
49
                $file = substr($file, strlen('https://localhost:8080/'));
50
            }
51
52
            $source .= file_get_contents($this->publicDir.'/'.$file);
53
        }
54
55
        return $source;
56
    }
57
58
    public static function getSubscribedServices()
59
    {
60
        return [
61
            EntrypointLookupInterface::class,
62
        ];
63
    }
64
}
65