TagRenderer   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 30
dl 0
loc 79
ccs 28
cts 28
cp 1
rs 10
c 0
b 0
f 0
wmc 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A renderJsTags() 0 16 3
A renderCssTags() 0 19 3
A __construct() 0 8 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SixtyEightPublishers\WebpackEncoreBundle\Latte;
6
7
use Nette;
8
use Symfony;
9
use SixtyEightPublishers;
10
11 1
final class TagRenderer
12
{
13 1
	use Nette\SmartObject;
14
15
	/** @var \SixtyEightPublishers\WebpackEncoreBundle\EntryPoint\IEntryPointLookupProvider  */
16
	private $entryPointLookupProvider;
17
18
	/** @var \Symfony\Component\Asset\Packages  */
19
	private $packages;
20
21
	/** @var array  */
22
	private $defaultAttributes;
23
24
	/**
25
	 * @param \SixtyEightPublishers\WebpackEncoreBundle\EntryPoint\IEntryPointLookupProvider $entryPointLookupProvider
26
	 * @param \Symfony\Component\Asset\Packages                                              $packages
27
	 * @param array                                                                          $defaultAttributes
28
	 */
29
	public function __construct(
30
		SixtyEightPublishers\WebpackEncoreBundle\EntryPoint\IEntryPointLookupProvider $entryPointLookupProvider,
31
		Symfony\Component\Asset\Packages $packages,
32
		array $defaultAttributes = []
33
	) {
34 1
		$this->entryPointLookupProvider = $entryPointLookupProvider;
35 1
		$this->packages = $packages;
36 1
		$this->defaultAttributes = $defaultAttributes;
37 1
	}
38
39
	/**
40
	 * @param string      $entryName
41
	 * @param string|NULL $packageName
42
	 * @param string|NULL $buildName
43
	 *
44
	 * @return string
45
	 */
46
	public function renderJsTags(string $entryName, ?string $packageName = NULL, ?string $buildName = NULL): string
47
	{
48 1
		$entryPointLookup = $this->entryPointLookupProvider->getEntryPointLookup($buildName);
49 1
		$integrityHashes = ($entryPointLookup instanceof SixtyEightPublishers\WebpackEncoreBundle\EntryPoint\IIntegrityDataProvider) ? $entryPointLookup->getIntegrityData() : [];
50 1
		$htmlTag = Nette\Utils\Html::el('script')->addAttributes($this->defaultAttributes);
51
52 1
		foreach (($tags = $entryPointLookup->getJsFiles($entryName)) as $i => $file) {
53 1
			$tags[$i] = (clone $htmlTag)
54 1
				->addAttributes([
55 1
					'src' => $this->packages->getUrl($file, $packageName),
56 1
					'integrity' => $integrityHashes[$file] ?? NULL,
57
				])
58 1
				->render();
59
		}
60
61 1
		return implode("\n", $tags);
62
	}
63
64
	/**
65
	 * @param string      $entryName
66
	 * @param string|NULL $packageName
67
	 * @param string|NULL $buildName
68
	 *
69
	 * @return string
70
	 */
71
	public function renderCssTags(string $entryName, ?string $packageName = NULL, ?string $buildName = NULL): string
72
	{
73 1
		$entryPointLookup = $this->entryPointLookupProvider->getEntryPointLookup($buildName);
74 1
		$integrityHashes = ($entryPointLookup instanceof SixtyEightPublishers\WebpackEncoreBundle\EntryPoint\IIntegrityDataProvider) ? $entryPointLookup->getIntegrityData() : [];
75
76 1
		$htmlTag = Nette\Utils\Html::el('link')
77 1
			->addAttributes($this->defaultAttributes)
78 1
			->setAttribute('rel', 'stylesheet');
79
80 1
		foreach (($tags = $entryPointLookup->getCssFiles($entryName)) as $i => $file) {
81 1
			$tags[$i] = (clone $htmlTag)
82 1
				->addAttributes([
83 1
					'href' => $this->packages->getUrl($file, $packageName),
84 1
					'integrity' => $integrityHashes[$file] ?? NULL,
85
				])
86 1
				->render();
87
		}
88
89 1
		return implode("\n", $tags);
90
	}
91
}
92