Passed
Push — master ( 22e73d...57bfa9 )
by Greg
08:48
created

SiteLogsPage::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 4
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\Carbon;
23
use Fisharebest\Webtrees\Http\ViewResponseTrait;
24
use Fisharebest\Webtrees\I18N;
25
use Fisharebest\Webtrees\Services\TreeService;
26
use Fisharebest\Webtrees\Services\UserService;
27
use Fisharebest\Webtrees\Tree;
28
use Fisharebest\Webtrees\User;
29
use Illuminate\Database\Capsule\Manager as DB;
30
use Illuminate\Support\Collection;
31
use Psr\Http\Message\ResponseInterface;
32
use Psr\Http\Message\ServerRequestInterface;
33
use Psr\Http\Server\RequestHandlerInterface;
34
35
use function max;
36
use function min;
37
38
/**
39
 * Show logs.
40
 */
41
class SiteLogsPage implements RequestHandlerInterface
42
{
43
    use ViewResponseTrait;
44
45
    /** @var TreeService */
46
    private $tree_service;
47
48
    /** @var UserService */
49
    private $user_service;
50
51
    /**
52
     * @param TreeService $tree_service
53
     * @param UserService $user_service
54
     */
55
    public function __construct(TreeService $tree_service, UserService $user_service)
56
    {
57
        $this->tree_service = $tree_service;
58
        $this->user_service = $user_service;
59
    }
60
61
    /**
62
     * @param ServerRequestInterface $request
63
     *
64
     * @return ResponseInterface
65
     */
66
    public function handle(ServerRequestInterface $request): ResponseInterface
67
    {
68
        $this->layout = 'layouts/administration';
69
70
        $earliest = DB::table('log')->min('log_time');
71
        $latest   = DB::table('log')->max('log_time');
72
73
        $earliest = $earliest ? Carbon::make($earliest) : Carbon::now();
74
        $latest   = $latest ? Carbon::make($latest) : Carbon::now();
75
76
        $earliest = $earliest->toDateString();
0 ignored issues
show
Bug introduced by
The method toDateString() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

76
        /** @scrutinizer ignore-call */ 
77
        $earliest = $earliest->toDateString();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
77
        $latest   = $latest->toDateString();
0 ignored issues
show
Bug introduced by
The method toDateString() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

77
        /** @scrutinizer ignore-call */ 
78
        $latest   = $latest->toDateString();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
78
79
        $params   = $request->getQueryParams();
80
        $action   = $params['action'] ?? '';
81
        $from     = $params['from'] ?? $earliest;
82
        $to       = $params['to'] ?? $latest;
83
        $type     = $params['type'] ?? '';
84
        $text     = $params['text'] ?? '';
85
        $ip       = $params['ip'] ?? '';
86
        $username = $params['username'] ?? '';
87
        $tree     = $params['tree'] ?? '';
88
89
        $from = max($from, $earliest);
90
        $to   = min(max($from, $to), $latest);
91
92
        $user_options = $this->user_service->all()->mapWithKeys(static function (User $user): array {
93
            return [$user->userName() => $user->userName()];
94
        });
95
        $user_options = (new Collection(['' => '']))->merge($user_options);
96
97
        $tree_options = $this->tree_service->all()->mapWithKeys(static function (Tree $tree): array {
98
            return [$tree->name() => $tree->title()];
99
        });
100
        $tree_options = (new Collection(['' => '']))->merge($tree_options);
101
102
        $title = I18N::translate('Website logs');
103
104
        return $this->viewResponse('admin/site-logs', [
105
            'action'       => $action,
106
            'earliest'     => $earliest,
107
            'from'         => $from,
108
            'tree'         => $tree,
109
            'ip'           => $ip,
110
            'latest'       => $latest,
111
            'tree_options' => $tree_options,
112
            'title'        => $title,
113
            'to'           => $to,
114
            'text'         => $text,
115
            'type'         => $type,
116
            'username'     => $username,
117
            'user_options' => $user_options,
118
        ]);
119
    }
120
}
121