Test Failed
Push — feature-laravel-5.4 ( 43bafb...e7f3fd )
by Kirill
42:21
created

ContentObserver::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 2
dl 0
loc 5
rs 9.4285
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 App\Services\ContentRenderer\Anchors\Parser;
14
use App\Services\ContentRenderer\RenderersRepository;
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 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);
0 ignored issues
show
Documentation introduced by
The property content_source does not exist on object<App\Models\DocsPage>. 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
        $page->nav = $body->getLinks();
0 ignored issues
show
Documentation introduced by
The property nav does not exist on object<App\Models\DocsPage>. 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
        $page->content_rendered = (string)$body->getContent();
0 ignored issues
show
Documentation introduced by
The property content_rendered does not exist on object<App\Models\DocsPage>. 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 DocsPage $page
59
     * @return ContentRenderInterface
60
     * @throws \InvalidArgumentException
61
     */
62
    private function getRenderer(DocsPage $page): ContentRenderInterface
63
    {
64
        return $this->repository->getRenderer($page->docs->renderer);
0 ignored issues
show
Documentation introduced by
The property docs does not exist on object<App\Models\DocsPage>. 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...
65
    }
66
67
    /**
68
     * @param ContentRenderInterface $renderer
69
     * @param  string                $body
70
     * @return ProcessedBody
71
     */
72
    private function render(ContentRenderInterface $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