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

PrependBlocks   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 5
eloc 14
dl 0
loc 34
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A add() 0 5 1
A mutate() 0 12 3
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