Completed
Pull Request — master (#1987)
by Gabriel
61:08
created

TwigFactory::getCache()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 4.125

Importance

Changes 0
Metric Value
cc 3
eloc 5
nc 3
nop 0
dl 0
loc 8
ccs 2
cts 4
cp 0.5
crap 4.125
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare( strict_types = 1 );
4
5
namespace WMDE\Fundraising\Frontend\Factories;
6
7
use Twig\Cache\CacheInterface;
8
use Twig\Cache\FilesystemCache;
9
use Twig\Cache\NullCache;
10
use Twig\Environment;
11
use Twig\Lexer;
12
use Twig\Loader\FilesystemLoader;
13
use Twig\Loader\LoaderInterface;
14
use Twig\TwigFilter;
15
use WMDE\Fundraising\Frontend\Presentation\FilePrefixer;
16
17
abstract class TwigFactory {
18
19
	private array $config;
20
	private string $cachePath;
21
	private string $locale;
22
	private ?CacheInterface $cache;
23 173
24 173
	public function __construct( array $config, string $cachePath, string $locale ) {
25 173
		$this->config = $config;
26 173
		$this->cachePath = $cachePath;
27 173
		$this->locale = $locale;
28
		$this->cache = null;
29 173
	}
30 173
31 1
	protected function newFilePrefixFilter( FilePrefixer $filePrefixer ): TwigFilter {
32
		return new TwigFilter( 'prefix_file', [ $filePrefixer, 'prefixFile' ] );
33 172
	}
34 172
35
	private function getLoader(): LoaderInterface {
36
		if ( !empty( $this->config['loaders']['filesystem'] ) ) {
37
			return new FilesystemLoader( $this->config['loaders']['filesystem'] );
38
		}
39
		throw new \UnexpectedValueException( 'Invalid Twig loader configuration - missing filesystem' );
40
	}
41
42
	protected function newTwigEnvironment( array $filters, array $functions, array $globals = [] ): Environment {
43 172
		$options = [
44 172
			'strict_variables' => isset( $this->config['strict-variables'] ) && $this->config['strict-variables'] === true,
45 172
			'cache' => $this->getCache()
46 172
		];
47
		$twig = new Environment( $this->getLoader(), $options );
48
49
		foreach ( $globals as $name => $global ) {
50
			$twig->addGlobal( $name, $global );
51
		}
52
53
		foreach ( $functions as $function ) {
54
			$twig->addFunction( $function );
55 172
		}
56 172
57
		foreach ( $filters as $filter ) {
58 172
			$twig->addFilter( $filter );
59 171
		}
60
61 172
		$twig->setLexer( new Lexer( $twig, [
62 172
			'tag_comment' => [ '{#', '#}' ],
63 172
			'tag_block' => [ '{%', '%}' ],
64
			'tag_variable' => [ '{$', '$}' ]
65
		] ) );
66
67 170
		return $twig;
68 170
	}
69 170
70
	public function getCache(): CacheInterface {
71
		if ( empty( $this->config['enable-cache'] ) ) {
72
			return new NullCache();
73
		}
74
		if ( $this->cache === null ) {
75
			$this->cache = new FilesystemCache( $this->cachePath );
76 170
		}
77 170
		return $this->cache;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->cache could return the type null which is incompatible with the type-hinted return Twig\Cache\CacheInterface. Consider adding an additional type-check to rule them out.
Loading history...
78
	}
79
80
}
81