ReplaceJavaScripts   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 26
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 3
eloc 13
dl 0
loc 26
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A mutate() 0 11 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Everlution\AjaxcomBundle\Mutation;
6
7
use Everlution\Ajaxcom\Handler;
8
use Everlution\AjaxcomBundle\AjaxcomException;
9
use Everlution\AjaxcomBundle\DataObject\Block;
10
use Everlution\AjaxcomBundle\Service\RenderBlock;
11
12
/**
13
 * Class ReplaceJavaScripts.
14
 *
15
 * @author Ivan Barlog <[email protected]>
16
 */
17
class ReplaceJavaScripts implements MutatorInterface, RenderableInterface
18
{
19
    use RenderableTrait;
20
21
    /** @var RenderBlock */
22
    private $renderBlock;
23
    /** @var string */
24
    private $persistentClass;
25
26
    public function __construct(RenderBlock $renderBlock, string $persistentClass)
27
    {
28
        $this->renderBlock = $renderBlock;
29
        $this->persistentClass = $persistentClass;
30
    }
31
32
    public function mutate(Handler $ajax): Handler
33
    {
34
        $ajax->container(sprintf('script:not(.%s):not([nonce])', $this->persistentClass))->remove();
35
36
        try {
37
            $javaScripts = $this->renderBlock->render(new Block('javascripts'), $this->view, $this->parameters);
38
            $ajax->container('script:last-of-type')->insertAfter($javaScripts);
39
        } catch (AjaxcomException $exception) {
40
            // do nothing
41
        } finally {
42
            return $ajax;
43
        }
44
    }
45
}
46