Passed
Branch 4.9 (cb955a)
by Mikhail
01:30
created

ReadmeGenerator::getPath()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 7
dl 0
loc 13
c 0
b 0
f 0
rs 10
cc 2
nc 2
nop 0
1
<?php
2
3
namespace generators\Readme;
4
5
use Config;
6
use mediators\AbstractMediator;
7
8
/**
9
  * @property string $pathTemplate
10
  * @property string $content
11
  * @property Config $config
12
  */
13
final class ReadmeGenerator extends \generators\AbstractGenerator
14
{
15
    // readonly
16
    private $pathTemplate = '/app/addons/${addon}/README.md';
17
    private $templatePath = ROOT_DIR . '/resources/README.md';
18
    private $content = '';
19
    private $config;
20
    private $mediator;
21
22
    function __construct(Config $config)
23
    {
24
        $this->config = $config;
25
    }
26
27
    /**
28
     * @inheritdoc
29
     */
30
    public function getTemplateFilename(): string
31
    {
32
        return get_absolute_path($this->templatePath);
33
    }
34
35
    public function setMediator(AbstractMediator $mediator): void
36
    {
37
        $this->mediator = $mediator;
38
    }
39
40
    /**
41
     * @inheritdoc
42
     */
43
    public function getPath(): string
44
    {
45
        $addon_id = $this->config->getOr('addon', 'addon.id');
46
47
        if (!$addon_id) {
48
            throw new \InvalidArgumentException('Addon id (name) not specified');
49
        }
50
51
        $path = $this->config->get('path')
52
            . $this->config->get('filesystem.output_path_relative')
53
            . str_replace('${addon}', $addon_id, $this->pathTemplate);
54
55
        return get_absolute_path($path);
56
    }
57
58
    /**
59
     * @inheritdoc
60
     *
61
     * @return LanguageGenerator
0 ignored issues
show
Bug introduced by
The type generators\Readme\LanguageGenerator was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
62
     */
63
    public function setContent(string $content)
64
    {
65
        $this->content = $content;
66
67
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type generators\Readme\ReadmeGenerator which is incompatible with the documented return type generators\Readme\LanguageGenerator.
Loading history...
68
    }
69
70
    /**
71
     * Replace placeholders by real data
72
     * @param string $content
73
     * 
74
     * @return string
75
     */
76
    public static function substituteData(string $content, array $data): string
77
    {
78
        return str_replace(
79
            [
80
                '${addon.id}',
81
                '${developer.company}',
82
                '${developer.name}',
83
            ],
84
            $data,
85
            $content
86
        );
87
    }
88
89
    /**
90
     * @inheritdoc
91
     */
92
    public function toString(): string
93
    {
94
        return self::substituteData($this->content, [
95
            parse_to_readable($this->config->get('addon.id')),
96
            $this->config->get('developer.company'),
97
            $this->config->get('developer.name')
98
        ]);
99
    }
100
}
101