Test Failed
Push — master ( 40a2d1...7a5bc4 )
by Valentin
40s
created

LogService::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 3
1
<?php
2
3
namespace Spiral\LogViewer\Services;
4
5
use Spiral\Files\FileManager;
6
use Spiral\LogViewer\Config;
7
use Spiral\LogViewer\Entities\LogFile;
8
use Symfony\Component\Finder\Finder;
9
use Symfony\Component\Finder\SplFileInfo;
10
11
class LogService
12
{
13
    /** @var Finder */
14
    private $finder;
15
16
    /** @var Config */
17
    private $config;
18
19
    /** @var FileManager */
20
    private $files;
21
22
    /**
23
     * LogService constructor.
24
     *
25
     * @param Finder      $finder
26
     * @param Config      $config
27
     * @param FileManager $files
28
     */
29
    public function __construct(Finder $finder, Config $config, FileManager $files)
30
    {
31
        $this->finder = $finder;
32
        $this->files = $files;
33
        $this->config = $config;
34
    }
35
36
    /**
37
     * @return array
38
     */
39
    public function getLogs(): array
40
    {
41
        $logs = [];
42
        $files = $this->finder->files()->sortByName()->in($this->config->directories());
43
44
        foreach ($files as $file) {
45
            /** @var SplFileInfo $file */
46
            $logs[$file->getFilename()] = new LogFile($file);
47
        }
48
49
        return $logs;
50
    }
51
52
    /**
53
     * @return null|LogFile
54
     */
55
    public function lastLog()
56
    {
57
        $files = $this->finder->files()->in($this->config->directories())->sort(function ($a, $b) {
58
            /**
59
             * @var SplFileInfo $a
60
             * @var SplFileInfo $b
61
             */
62
            return $b->getMTime() - $a->getMTime();
63
        });
64
65
        /** @var SplFileInfo $file */
66
        foreach ($files as $file) {
67
            return new LogFile($file);
68
        }
69
70
        return null;
71
    }
72
73
    /**
74
     * @param string $filename
75
     * @return null|LogFile
76
     */
77
    public function getLogByName(string $filename)
78
    {
79
        foreach ($this->config->directories() as $directory) {
80
            if (!file_exists($directory . $filename)) {
81
                continue;
82
            }
83
84
            return new LogFile($directory . $filename);
85
        }
86
87
        return null;
88
    }
89
90
    /**
91
     * @param LogFile $filename
92
     */
93
    public function removeLog(LogFile $filename)
94
    {
95
        foreach ($this->config->directories() as $directory) {
96
            if (!file_exists($directory . $filename->name())) {
97
                continue;
98
            }
99
100
            $this->files->delete($directory . $filename->name());
101
102
            return;
103
        }
104
    }
105
106
    /**
107
     *
108
     */
109
    public function removeAll()
110
    {
111
        /** @var SplFileInfo $file */
112
        foreach ($this->finder->in($this->config->directories()) as $file) {
113
            $this->files->delete($file->getRealPath());
114
        }
115
    }
116
}