Issues (1783)

src/helpers/FileLog.php (34 issues)

1
<?php
2
/**
3
 * Retour plugin for Craft CMS
4
 *
5
 * Retour allows you to intelligently redirect legacy URLs, so that you don't
6
 * lose SEO value when rebuilding & restructuring a website
7
 *
8
 * @link      https://nystudio107.com/
0 ignored issues
show
The tag in position 1 should be the @copyright tag
Loading history...
9
 * @copyright Copyright (c) 2022 nystudio107
0 ignored issues
show
@copyright tag must contain a year and the name of the copyright holder
Loading history...
10
 */
0 ignored issues
show
PHP version not specified
Loading history...
Missing @category tag in file comment
Loading history...
Missing @package tag in file comment
Loading history...
Missing @author tag in file comment
Loading history...
Missing @license tag in file comment
Loading history...
11
12
namespace nystudio107\retour\helpers;
13
14
use Craft;
15
use craft\helpers\FileHelper;
16
use craft\log\MonologTarget;
17
use Monolog\Formatter\LineFormatter;
18
use Psr\Log\LogLevel;
19
20
/**
0 ignored issues
show
Missing short description in doc comment
Loading history...
21
 * @author    nystudio107
0 ignored issues
show
The tag in position 1 should be the @package tag
Loading history...
Content of the @author tag must be in the form "Display Name <[email protected]>"
Loading history...
Tag value for @author tag indented incorrectly; expected 2 spaces but found 4
Loading history...
22
 * @package   Retour
0 ignored issues
show
Tag value for @package tag indented incorrectly; expected 1 spaces but found 3
Loading history...
23
 * @since     4.1.0
0 ignored issues
show
The tag in position 3 should be the @author tag
Loading history...
Tag value for @since tag indented incorrectly; expected 3 spaces but found 5
Loading history...
24
 */
0 ignored issues
show
Missing @category tag in class comment
Loading history...
Missing @license tag in class comment
Loading history...
Missing @link tag in class comment
Loading history...
25
class FileLog
26
{
27
    // Constants
28
    // =========================================================================
29
30
    public const FILE_PER_DAY = 'Y-m-d';
31
32
    // Public Static Methods
33
    // =========================================================================
34
35
    /**
36
     * Create an additional file log target named $filename.log that logs messages
37
     * in the category $category
38
     *
39
     * @param string $fileName
0 ignored issues
show
Missing parameter comment
Loading history...
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
40
     * @param string $category
0 ignored issues
show
Missing parameter comment
Loading history...
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
41
     * @return void
0 ignored issues
show
Tag @return cannot be grouped with parameter tags in a doc comment
Loading history...
42
     */
43
    public static function create(string $fileName, string $category): void
44
    {
45
        // Create a new file target
46
        $fileTarget = new MonologTarget([
0 ignored issues
show
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
47
            'name' => $fileName,
48
            'categories' => [$category],
49
            'level' => LogLevel::INFO,
50
            'logContext' => false,
51
            'allowLineBreaks' => true,
52
            'formatter' => new LineFormatter(
53
                format: "%datetime% [%channel%.%level_name%] [%extra.yii_category%] %message% %context% %extra%\n",
54
                dateFormat: 'Y-m-d H:i:s',
55
                allowInlineLineBreaks: true,
56
                ignoreEmptyContextAndExtra: true,
57
            ),
58
        ]);
0 ignored issues
show
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
59
        // Add the new target file target to the dispatcher
60
        Craft::getLogger()->dispatcher->targets[] = $fileTarget;
61
    }
62
63
    /**
64
     * Delete the passed in log file $fileName
65
     *
66
     * @param string $fileName
0 ignored issues
show
Missing parameter comment
Loading history...
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
67
     * @return void
0 ignored issues
show
Tag @return cannot be grouped with parameter tags in a doc comment
Loading history...
68
     */
69
    public static function delete(string $fileName): void
70
    {
71
        FileHelper::unlink(self::getLogFilePath($fileName));
72
    }
73
74
    /**
75
     * Get the contents of the log file
76
     *
77
     * @param string $fileName
0 ignored issues
show
Missing parameter comment
Loading history...
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
78
     * @return string
0 ignored issues
show
Tag @return cannot be grouped with parameter tags in a doc comment
Loading history...
79
     */
80
    public static function getContents(string $fileName): string
81
    {
82
        $contents = @file_get_contents(self::getLogFilePath($fileName));
83
84
        return $contents === false ? '' : $contents;
85
    }
86
87
    /**
88
     * Return a full path to the log file
89
     *
90
     * @param string $fileName
0 ignored issues
show
Missing parameter comment
Loading history...
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
91
     * @return string
0 ignored issues
show
Tag @return cannot be grouped with parameter tags in a doc comment
Loading history...
92
     */
93
    public static function getLogFilePath(string $fileName): string
94
    {
95
        $logfile = $fileName . "-" . date(self::FILE_PER_DAY);
96
97
        return Craft::getAlias("@storage/logs/$logfile.log");
0 ignored issues
show
Bug Best Practice introduced by
The expression return Craft::getAlias('...logs/'.$logfile.'.log') could return the type false which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
98
    }
99
}
100