Passed
Push — master ( 3e4ca6...647507 )
by Ivan
05:43
created

PrependBlocks::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 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\DataObject\Block;
10
use Everlution\AjaxcomBundle\Service\RenderBlock;
11
12
class PrependBlocks implements MutatorInterface, RenderableInterface
13
{
14
    use RenderableTrait;
15
16
    /** @var RenderBlock */
17
    private $renderBlock;
18
19
    /** @var Block[] */
20
    private $blocks = [];
21
22
    public function __construct(RenderBlock $renderBlock)
23
    {
24
        $this->renderBlock = $renderBlock;
25
    }
26
27
    public function mutate(Handler $ajaxcom): Handler
28
    {
29
        foreach ($this->blocks as $block) {
30
            try {
31
                $html = $this->renderBlock->render($block, $this->view, $this->parameters);
32
                $ajaxcom->container('#'.$block->getId())->prepend($html);
33
            } catch (AjaxcomException $e) {
34
                continue;
35
            }
36
        }
37
38
        return $ajaxcom;
39
    }
40
41
    public function add(string $selector): self
42
    {
43
        $this->blocks[] = new Block($selector);
44
45
        return $this;
46
    }
47
}
48