TagRenderer::renderJsTags()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 10
c 1
b 0
f 0
nc 4
nop 3
dl 0
loc 16
ccs 11
cts 11
cp 1
crap 3
rs 9.9332
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
final class TagRenderer
12
{
13
	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 1
	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 1
	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 1
	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