ReplaceContent   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 15
c 1
b 0
f 0
dl 0
loc 34
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A add() 0 5 1
A mutate() 0 14 3
A __construct() 0 3 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
/**
13
 * Class ReplaceBlockContent.
14
 *
15
 * @author Ivan Barlog <[email protected]>
16
 */
17
class ReplaceContent implements MutatorInterface, RenderableInterface
18
{
19
    use RenderableTrait;
20
21
    /** @var RenderBlock */
22
    private $renderBlock;
23
    private $data = [];
24
25
    public function __construct(RenderBlock $renderBlock)
26
    {
27
        $this->renderBlock = $renderBlock;
28
    }
29
30
    public function mutate(Handler $ajax): Handler
31
    {
32
        foreach ($this->data as $item) {
33
            $block = $item['block'];
34
            try {
35
                $html = $this->renderBlock->render($block, $this->view, $this->parameters);
36
            } catch (AjaxcomException $exception) {
37
                continue;
38
            }
39
40
            $ajax->container($item['selector'])->html($html);
41
        }
42
43
        return $ajax;
44
    }
45
46
    public function add(string $selector, string $blockId): self
47
    {
48
        $this->data[] = ['selector' => $selector, 'block' => new Block($blockId)];
49
50
        return $this;
51
    }
52
}
53