ContentPreviewObserver   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 30
rs 10
c 0
b 0
f 0
wmc 4
lcom 1
cbo 1

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A saving() 0 10 3
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\Article;
11
12
use App\Models\Article;
13
use Illuminate\Support\Str;
14
use Service\ContentRenderer\ContentRendererInterface;
15
16
/**
17
 * Class ContentPreviewObserver.
18
 */
19
class ContentPreviewObserver
20
{
21
    /**
22
     * @var ContentRendererInterface
23
     */
24
    private $renderer;
25
26
    /**
27
     * ContentObserver constructor.
28
     * @param ContentRendererInterface $renderer
29
     */
30
    public function __construct(ContentRendererInterface $renderer)
31
    {
32
        $this->renderer = $renderer;
33
    }
34
35
    /**
36
     * @param Article $article
37
     */
38
    public function saving(Article $article): void
39
    {
40
        if ($article->content_source) {
41
            if (! $article->preview_source) {
42
                $article->preview_source = Str::words($article->content_source, 100, '…');
43
            }
44
45
            $article->preview_rendered = $this->renderer->render($article->preview_source);
46
        }
47
    }
48
}
49