Passed
Push — master ( a53647...875319 )
by Greg
05:28
created

RobotsTxt::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
3
/**
4
 * webtrees: online genealogy
5
 * Copyright (C) 2019 webtrees development team
6
 * This program is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation, either version 3 of the License, or
9
 * (at your option) any later version.
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
 * GNU General Public License for more details.
14
 * You should have received a copy of the GNU General Public License
15
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16
 */
17
18
declare(strict_types=1);
19
20
namespace Fisharebest\Webtrees\Http\RequestHandlers;
21
22
use Fisharebest\Webtrees\Module\SiteMapModule;
23
use Fisharebest\Webtrees\Services\ModuleService;
24
use Psr\Http\Message\ResponseInterface;
25
use Psr\Http\Message\ServerRequestInterface;
26
use Psr\Http\Server\RequestHandlerInterface;
27
28
use function response;
29
30
use const PHP_URL_PATH;
31
32
/**
33
 * Generate a robots exclusion file.
34
 *
35
 * @link https://robotstxt.org
36
 */
37
class RobotsTxt implements RequestHandlerInterface
38
{
39
    private const DISALLOWED_PATHS = [
40
        'admin',
41
        'manager',
42
        'moderator',
43
        'editor',
44
        'account',
45
    ];
46
47
    /** @var ModuleService */
48
    private $module_service;
49
50
    /**
51
     * @param ModuleService $module_service
52
     */
53
    public function __construct(ModuleService $module_service)
54
    {
55
        $this->module_service = $module_service;
56
    }
57
58
    /**
59
     * @param ServerRequestInterface $request
60
     *
61
     * @return ResponseInterface
62
     */
63
    public function handle(ServerRequestInterface $request): ResponseInterface
64
    {
65
        $base_url = $request->getAttribute('base_url');
66
67
        $data = [
68
            'base_url'         => $base_url,
69
            'base_path'        => parse_url($base_url, PHP_URL_PATH) ?? '',
70
            'disallowed_paths' => self::DISALLOWED_PATHS,
71
            'sitemap_url'      => '',
72
        ];
73
74
        $sitemap_module = $this->module_service->findByInterface(SiteMapModule::class)->first();
75
76
        if ($sitemap_module instanceof SiteMapModule) {
77
            $data['sitemap_url'] = route('module', [
78
                'module' => $sitemap_module->name(),
79
                'action' => 'Index',
80
            ]);
81
        }
82
83
        return response(view('robots-txt', $data))
84
            ->withHeader('Content-type', 'text/plain');
85
    }
86
}
87