ContentObserver::getRenderer()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * This file is part of laravel.su package.
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
declare(strict_types=1);
9
10
namespace App\Models\DocsPage;
11
12
use App\Models\DocsPage;
13
use Service\ContentRenderer\Anchors\Parser;
14
use Service\ContentRenderer\RenderersRepository;
15
use Service\ContentRenderer\Anchors\ProcessedBody;
16
use Service\ContentRenderer\ContentRendererInterface;
17
18
/**
19
 * Class ContentObserver.
20
 */
21
class ContentObserver
22
{
23
    /**
24
     * @var Parser
25
     */
26
    private $parser;
27
28
    /**
29
     * @var RenderersRepository
30
     */
31
    private $repository;
32
33
    /**
34
     * ContentObserver constructor.
35
     * @param Parser              $parser
36
     * @param RenderersRepository $repository
37
     */
38
    public function __construct(Parser $parser, RenderersRepository $repository)
39
    {
40
        $this->parser = $parser;
41
        $this->repository = $repository;
42
    }
43
44
    /**
45
     * @param  DocsPage                  $page
46
     * @throws \InvalidArgumentException
47
     */
48
    public function saving(DocsPage $page): void
49
    {
50
        $body = $this->render($this->getRenderer($page), (string) $page->content_source);
51
52
        // Update data
53
        $page->nav = $body->getLinks();
54
        $page->content_rendered = (string) $body->getContent();
55
    }
56
57
    /**
58
     * @param  DocsPage                  $page
59
     * @return ContentRendererInterface
60
     * @throws \InvalidArgumentException
61
     */
62
    private function getRenderer(DocsPage $page): ContentRendererInterface
63
    {
64
        return $this->repository->getRenderer($page->docs->renderer);
65
    }
66
67
    /**
68
     * @param  ContentRendererInterface $renderer
69
     * @param  string                   $body
70
     * @return ProcessedBody
71
     */
72
    private function render(ContentRendererInterface $renderer, string $body): ProcessedBody
73
    {
74
        // Parse markdown
75
        $rendered = $renderer->render($body);
76
77
        // Parse content headers
78
        return $this->parser->parse($rendered);
79
    }
80
}
81