Test Failed
Push — feature-laravel-5.4 ( 2b4b90...5bfdc4 )
by Kirill
03:52
created

ContentObserver   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
dl 0
loc 49
rs 10
c 0
b 0
f 0
wmc 3
lcom 1
cbo 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A saving() 0 8 1
A render() 0 8 1
1
<?php
2
3
/**
4
 * This file is part of laravel.su package.
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
declare(strict_types=1);
10
11
namespace App\Models\Docs;
12
13
use App\Models\Docs;
14
use App\Services\ContentRenderer\Anchors\Parser;
15
use App\Services\ContentRenderer\Anchors\ProcessedBody;
16
use App\Services\ContentRenderer\ContentRenderInterface;
17
18
/**
19
 * Class ContentObserver.
20
 */
21
class ContentObserver
22
{
23
    /**
24
     * @var ContentRenderInterface
25
     */
26
    private $renderer;
27
28
    /**
29
     * @var Parser
30
     */
31
    private $parser;
32
33
    /**
34
     * ContentObserver constructor.
35
     *
36
     * @param Parser                 $parser
37
     * @param ContentRenderInterface $renderer
38
     */
39
    public function __construct(Parser $parser, ContentRenderInterface $renderer)
40
    {
41
        $this->renderer = $renderer;
42
        $this->parser = $parser;
43
    }
44
45
    /**
46
     * @param Docs $docs
47
     */
48
    public function saving(Docs $docs): void
49
    {
50
        $body = $this->render((string) $docs->content_source);
0 ignored issues
show
Documentation introduced by
The property content_source does not exist on object<App\Models\Docs>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
51
52
        // Update data
53
        $docs->nav = $body->getLinks();
0 ignored issues
show
Documentation introduced by
The property nav does not exist on object<App\Models\Docs>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
54
        $docs->content_rendered = (string) $body->getContent();
0 ignored issues
show
Documentation introduced by
The property content_rendered does not exist on object<App\Models\Docs>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
55
    }
56
57
    /**
58
     * @param string $body
59
     * @return ProcessedBody
60
     */
61
    private function render(string $body): ProcessedBody
62
    {
63
        // Parse markdown
64
        $rendered = $this->renderer->render($body);
65
66
        // Parse content headers
67
        return $this->parser->parse($rendered);
68
    }
69
}
70