Passed
Push — master ( fe64ef...2e5fa1 )
by Ivan
02:01
created

ReplaceJavaScripts   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 26
Duplicated Lines 0 %

Importance

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

2 Methods

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