Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
RelativeFilepathLogProcessor
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
3 / 3
7
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 __invoke
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
5
 isAbsolutePath
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2/**
3 * A logger that changes file paths to be relative to the project directory.
4 *
5 * If a variable that is being replaced as the term 'path' in it, the path will be made relative to workingdir.
6 *
7 * @see \BrianHenryIE\Strauss\Helpers\Flysystem\FileSystem::getProjectRelativePath()
8 */
9
10namespace BrianHenryIE\Strauss\Helpers\Log;
11
12use BrianHenryIE\Strauss\Helpers\Flysystem\FileSystem;
13use Monolog\Processor\ProcessorInterface;
14
15class RelativeFilepathLogProcessor implements ProcessorInterface
16{
17    protected FileSystem $fileSystem;
18
19    public function __construct(
20        FileSystem $fileSystem
21    ) {
22        $this->fileSystem = $fileSystem;
23    }
24
25    /**
26     * Checks all context values for keys containing 'path' modifies their values to be
27     * relative to the project root.
28     *
29     */
30    public function __invoke(array $record): array
31    {
32        $context = $record['context'];
33
34        foreach ($context as $key => $val) {
35            if (false !== stripos($key, 'path') && is_string($val)) {
36                if ($this->isAbsolutePath($val)) {
37                    $record['context'][ $key ] = $this->fileSystem->getProjectRelativePath($val);
38                }
39            }
40        }
41
42        return $record;
43    }
44
45    protected function isAbsolutePath(string $path): bool
46    {
47        return 0 !== strpos($path, '..');
48    }
49}