Completed
Push — master ( 3fdc21...2574b5 )
by ReliQ
01:51
created

ConfigProvider::getProductIndexRouteName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace ReliqArts\Docweaver\Services;
6
7
use Illuminate\Contracts\Config\Repository as Config;
8
use ReliqArts\Docweaver\Contracts\ConfigProvider as ConfigProviderContract;
9
use ReliqArts\Docweaver\Models\TemplateConfig;
10
11
final class ConfigProvider implements ConfigProviderContract
12
{
13
    private const NAMESPACE = 'docweaver';
14
    private const KEY_CACHE_KEY = 'cache.key';
15
    private const KEY_DEBUG = 'debug';
16
    private const KEY_TABLE_OF_CONTENTS_PAGE_NAME = 'doc.index';
17
    private const KEY_VERSIONS_ALLOW_WORDED_DEFAULT = 'versions.allow_worded_default';
18
    private const KEY_VIEW_MASTER_TEMPLATE = 'view.master_template';
19
    private const KEY_ROUTE = 'route';
20
    private const KEY_STORAGE_DIRECTORY = 'storage.dir';
21
    private const KEY_ROUTE_BINDINGS = 'route.bindings';
22
    private const KEY_ROUTE_NAME_INDEX = 'route.names.index';
23
    private const KEY_ROUTE_NAME_PRODUCT_INDEX = 'route.names.product_index';
24
    private const KEY_ROUTE_NAME_PRODUCT_PAGE = 'route.names.product_page';
25
    private const KEY_ROUTE_PREFIX = 'route.prefix';
26
    private const KEY_VIEW_INDEX_TITLE = 'view.docs_title';
27
    private const KEY_VIEW_MASTER_SECTION = 'view.master_section';
28
    private const KEY_VIEW_STYLE_STACK = 'view.style_stack';
29
    private const KEY_VIEW_SCRIPT_STACK = 'view.script_stack';
30
    private const KEY_VIEW_INDEX_INTRO = 'view.docs_intro';
31
    private const KEY_VIEW_ACCENTS_PRODUCT_LINE = 'view.accents.product_line';
32
    private const KEY_VIEW_ACCENTS_FOOTNOTES = 'view.accents.footnotes';
33
    private const DEFAULT_INDEX_ROUTE_NAME = 'docs';
34
    private const DEFAULT_PRODUCT_INDEX_ROUTE_NAME = 'docs.index';
35
    private const DEFAULT_PRODUCT_PAGE_ROUTE_NAME = 'docs.show';
36
    private const DEFAULT_ROUTE_PREFIX = 'docs';
37
    private const DEFAULT_CACHE_KEY = 'docweaver.docs';
38
    private const DEFAULT_TABLE_OF_CONTENTS_PAGE_NAME = 'documentation';
39
    private const DEFAULT_INDEX_TITLE = 'Documentation';
40
41
    /**
42
     * @var Config
43
     */
44
    private $config;
45
46
    /**
47
     * ConfigProvider constructor.
48
     *
49
     * @param Config $config
50
     */
51
    public function __construct(Config $config)
52
    {
53
        $this->config = $config;
54
    }
55
56
    /**
57
     * Get directory path to where documentation are stored.
58
     *
59
     * @param bool $absolute whether to return full
60
     *
61
     * @return string
62
     */
63
    public function getDocumentationDirectory($absolute = false): string
64
    {
65
        $path = $this->get(self::KEY_STORAGE_DIRECTORY);
66
67
        if ($absolute) {
68
            $path = base_path($path);
69
        }
70
71
        return $path;
72
    }
73
74
    /**
75
     * Get route config.
76
     *
77
     * @return array
78
     */
79
    public function getRouteConfig(): array
80
    {
81
        return $this->get(self::KEY_ROUTE, []);
82
    }
83
84
    /**
85
     * Get route prefix for docs.
86
     *
87
     * @return string
88
     */
89
    public function getRoutePrefix(): string
90
    {
91
        return $this->get(self::KEY_ROUTE_PREFIX, self::DEFAULT_ROUTE_PREFIX);
92
    }
93
94
    /**
95
     * Get bindings for routes.
96
     *
97
     * @param array $bindings
98
     *
99
     * @return array
100
     */
101
    public function getRouteGroupBindings(array $bindings = []): array
102
    {
103
        $defaults = ['prefix' => $this->getRoutePrefix()];
104
        $bindings = array_merge($this->get(self::KEY_ROUTE_BINDINGS, []), $bindings);
105
106
        return array_merge($defaults, $bindings);
107
    }
108
109
    public function isDebug(): bool
110
    {
111
        return $this->get(self::KEY_DEBUG, false);
112
    }
113
114
    /**
115
     * @return bool
116
     */
117
    public function isWordedDefaultVersionAllowed(): bool
118
    {
119
        return $this->get(self::KEY_VERSIONS_ALLOW_WORDED_DEFAULT, false);
120
    }
121
122
    /**
123
     * @return string
124
     */
125
    public function getIndexRouteName(): string
126
    {
127
        return $this->get(self::KEY_ROUTE_NAME_INDEX, self::DEFAULT_INDEX_ROUTE_NAME);
128
    }
129
130
    /**
131
     * @return string
132
     */
133
    public function getProductIndexRouteName(): string
134
    {
135
        return $this->get(self::KEY_ROUTE_NAME_PRODUCT_INDEX, self::DEFAULT_PRODUCT_INDEX_ROUTE_NAME);
136
    }
137
138
    /**
139
     * @return string
140
     */
141
    public function getProductPageRouteName(): string
142
    {
143
        return $this->get(self::KEY_ROUTE_NAME_PRODUCT_PAGE, self::DEFAULT_PRODUCT_PAGE_ROUTE_NAME);
144
    }
145
146
    /**
147
     * @return string
148
     */
149
    public function getCacheKey(): string
150
    {
151
        return $this->get(self::KEY_CACHE_KEY, self::DEFAULT_CACHE_KEY);
152
    }
153
154
    /**
155
     * Page used as content index (or Table of Contents) for product documentation.
156
     *
157
     * @return string
158
     */
159
    public function getContentIndexPageName(): string
160
    {
161
        return $this->get(self::KEY_TABLE_OF_CONTENTS_PAGE_NAME, self::DEFAULT_TABLE_OF_CONTENTS_PAGE_NAME);
162
    }
163
164
    /**
165
     * @return TemplateConfig
166
     */
167
    public function getTemplateConfig(): TemplateConfig
168
    {
169
        $masterTemplate = $this->get(self::KEY_VIEW_MASTER_TEMPLATE);
170
        $masterSection = $this->get(self::KEY_VIEW_MASTER_SECTION);
171
        $styleStack = $this->get(self::KEY_VIEW_STYLE_STACK, '');
172
        $scriptStack = $this->get(self::KEY_VIEW_SCRIPT_STACK, '');
173
        $indexTitle = $this->get(self::KEY_VIEW_INDEX_TITLE, self::DEFAULT_INDEX_TITLE);
174
        $indexIntro = $this->get(self::KEY_VIEW_INDEX_INTRO, '');
175
        $showProductLine = $this->get(self::KEY_VIEW_ACCENTS_PRODUCT_LINE, true);
176
        $showFootnotes = $this->get(self::KEY_VIEW_ACCENTS_FOOTNOTES, true);
177
178
        return new TemplateConfig(
179
            $masterTemplate,
180
            $masterSection,
181
            $styleStack,
182
            $scriptStack,
183
            $indexTitle,
184
            $indexIntro,
185
            $showProductLine,
186
            $showFootnotes
187
        );
188
    }
189
190
    /**
191
     * @param null|string $key
192
     * @param mixed       $default
193
     *
194
     * @return mixed
195
     */
196
    private function get(?string $key, $default = null)
197
    {
198
        if (empty($key)) {
199
            return $this->config->get(self::NAMESPACE, []);
200
        }
201
202
        return $this->config->get(
203
            sprintf('%s.%s', self::NAMESPACE, $key),
204
            $default
205
        );
206
    }
207
}
208