• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

eliashaeussler / cache-warmup / 8472798252

28 Mar 2024 07:40PM UTC coverage: 93.917%. First build
8472798252

Pull #341

github

web-flow
Merge c60891e4b into 2245720b4
Pull Request #341: [!!!][TASK] Convert exceptions to single-case exceptions

86 of 88 new or added lines in 33 files covered. (97.73%)

1405 of 1496 relevant lines covered (93.92%)

9.15 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

67.86
/src/Log/FileLogger.php
1
<?php
2

3
declare(strict_types=1);
4

5
/*
6
 * This file is part of the Composer package "eliashaeussler/cache-warmup".
7
 *
8
 * Copyright (C) 2020-2024 Elias Häußler <elias@haeussler.dev>
9
 *
10
 * This program is free software: you can redistribute it and/or modify
11
 * it under the terms of the GNU General Public License as published by
12
 * the Free Software Foundation, either version 3 of the License, or
13
 * (at your option) any later version.
14
 *
15
 * This program is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18
 * GNU General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU General Public License
21
 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22
 */
23

24
namespace EliasHaeussler\CacheWarmup\Log;
25

26
use EliasHaeussler\CacheWarmup\Exception;
27
use EliasHaeussler\CacheWarmup\Helper;
28
use Psr\Log;
29
use Stringable;
30

31
use function dirname;
32
use function fclose;
33
use function file_exists;
34
use function fopen;
35
use function fwrite;
36
use function is_dir;
37
use function is_resource;
38
use function mkdir;
39
use function touch;
40

41
/**
42
 * FileLogger.
43
 *
44
 * @author Elias Häußler <elias@haeussler.dev>
45
 * @license GPL-3.0-or-later
46
 */
47
final class FileLogger extends Log\AbstractLogger
48
{
49
    private readonly string $file;
50
    private bool $fileCreated = false;
51

52
    /**
53
     * @var resource|null
54
     */
55
    private $stream;
56

57
    public function __construct(
3✔
58
        string $file,
59
        private readonly Formatter\LogFormatter $formatter = new Formatter\CompactLogFormatter(),
60
    ) {
61
        $this->file = Helper\FilesystemHelper::resolveRelativePath($file);
3✔
62
    }
63

64
    /**
65
     * @phpstan-param Log\LogLevel::* $level
66
     *
67
     * @param array<string, mixed> $context
68
     *
69
     * @throws Exception\FileStreamResultIsUnexpected
70
     */
71
    public function log($level, Stringable|string $message, array $context = []): void
3✔
72
    {
73
        if (!is_resource($this->stream)) {
3✔
74
            $this->stream = $this->openStream();
3✔
75
        }
76

77
        $formatted = $this->formatter->format($level, $message, $context);
3✔
78

79
        fwrite($this->stream, $formatted.PHP_EOL);
3✔
80
    }
81

82
    /**
83
     * @return resource
84
     *
85
     * @throws Exception\FileStreamResultIsUnexpected
86
     */
87
    private function openStream()
3✔
88
    {
89
        $this->createLogFileIfNotExists();
3✔
90

91
        $stream = fopen($this->file, 'a');
3✔
92

93
        if (!is_resource($stream)) {
3✔
NEW
94
            throw new Exception\FileStreamResultIsUnexpected($this->file);
×
95
        }
96

97
        return $stream;
3✔
98
    }
99

100
    private function closeStream(): void
×
101
    {
102
        if (is_resource($this->stream)) {
×
103
            fclose($this->stream);
×
104
        }
105

106
        $this->fileCreated = false;
×
107
        $this->stream = null;
×
108
    }
109

110
    private function createLogFileIfNotExists(): void
3✔
111
    {
112
        // Don't try to create file multiple times
113
        if ($this->fileCreated) {
3✔
114
            return;
×
115
        }
116

117
        // Assure directory exists
118
        if (!is_dir(dirname($this->file))) {
3✔
119
            mkdir(dirname($this->file), recursive: true);
3✔
120
        }
121

122
        // Create file if not exists
123
        if (!file_exists($this->file)) {
3✔
124
            touch($this->file);
3✔
125
        }
126

127
        // Set flag to not create file multiple times
128
        $this->fileCreated = true;
3✔
129
    }
130

131
    public function __destruct()
×
132
    {
133
        $this->closeStream();
×
134
    }
135
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc