AppendBlocks::add()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 5
rs 10
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 AppendBlocks 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())->append($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