Completed
Pull Request — master (#29)
by ARCANEDEV
04:36
created

Factory::setLevels()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4286
cc 1
eloc 3
nc 1
nop 1
crap 1
1
<?php namespace Arcanedev\LogViewer\Utilities;
2
3
use Arcanedev\LogViewer\Contracts\FactoryInterface;
4
use Arcanedev\LogViewer\Contracts\FilesystemInterface;
5
use Arcanedev\LogViewer\Contracts\LogLevelsInterface;
6
use Arcanedev\LogViewer\Entities\LogCollection;
7
use Arcanedev\LogViewer\Tables\StatsTable;
8
9
/**
10
 * Class     Factory
11
 *
12
 * @package  Arcanedev\LogViewer\Utilities
13
 * @author   ARCANEDEV <[email protected]>
14
 */
15
class Factory implements FactoryInterface
16
{
17
    /* ------------------------------------------------------------------------------------------------
18
     |  Properties
19
     | ------------------------------------------------------------------------------------------------
20
     */
21
    /**
22
     * The filesystem instance.
23
     *
24
     * @var \Arcanedev\LogViewer\Contracts\FilesystemInterface
25
     */
26
    protected $filesystem;
27
28
    /**
29
     * @var \Arcanedev\LogViewer\Contracts\LogLevelsInterface
30
     */
31
    private $levels;
32
33
    /* ------------------------------------------------------------------------------------------------
34
     |  Constructor
35
     | ------------------------------------------------------------------------------------------------
36
     */
37
    /**
38
     * Create a new instance.
39
     *
40
     * @param  \Arcanedev\LogViewer\Contracts\FilesystemInterface  $filesystem
41
     * @param  \Arcanedev\LogViewer\Contracts\LogLevelsInterface   $levels
42
     */
43 216
    public function __construct(
44
        FilesystemInterface $filesystem,
45
        LogLevelsInterface $levels
46
    ) {
47 216
        $this->setFilesystem($filesystem);
48 216
        $this->setLevels($levels);
49 216
    }
50
51
    /* ------------------------------------------------------------------------------------------------
52
     |  Getter & Setters
53
     | ------------------------------------------------------------------------------------------------
54
     */
55
    /**
56
     * Get the filesystem instance.
57
     *
58
     * @return \Arcanedev\LogViewer\Contracts\FilesystemInterface
59
     */
60 3
    public function getFilesystem()
61
    {
62 3
        return $this->filesystem;
63
    }
64
65
    /**
66
     * Set the filesystem instance.
67
     *
68
     * @param  \Arcanedev\LogViewer\Contracts\FilesystemInterface  $filesystem
69
     *
70
     * @return self
71
     */
72 216
    public function setFilesystem(FilesystemInterface $filesystem)
73
    {
74 216
        $this->filesystem = $filesystem;
75
76 216
        return $this;
77
    }
78
79
    /**
80
     * Get the log levels instance.
81
     *
82
     * @return \Arcanedev\LogViewer\Contracts\LogLevelsInterface $levels
83
     */
84 3
    public function getLevels()
85
    {
86 3
        return $this->levels;
87
    }
88
89
    /**
90
     * Set the log levels instance.
91
     *
92
     * @param  \Arcanedev\LogViewer\Contracts\LogLevelsInterface $levels
93
     *
94
     * @return self
95
     */
96 216
    public function setLevels(LogLevelsInterface $levels)
97
    {
98 216
        $this->levels = $levels;
99
100 216
        return $this;
101
    }
102
103
    /**
104
     * Set the log storage path.
105
     *
106
     * @param  string  $storagePath
107
     *
108
     * @return self
109
     */
110 6
    public function setPath($storagePath)
111
    {
112 6
        $this->filesystem->setPath($storagePath);
113
114 6
        return $this;
115 3
    }
116
117
    /**
118
     * Get all logs.
119
     *
120
     * @return \Arcanedev\LogViewer\Entities\LogCollection
121
     */
122 129
    public function logs()
123
    {
124 129
        return LogCollection::make()->setFilesystem($this->filesystem);
125
    }
126
127
    /* ------------------------------------------------------------------------------------------------
128
     |  Main Functions
129
     | ------------------------------------------------------------------------------------------------
130
     */
131
    /**
132
     * Get all logs. (alias)
133
     *
134
     * @return \Arcanedev\LogViewer\Entities\LogCollection
135
     */
136 6
    public function all()
137
    {
138 6
        return $this->logs();
139
    }
140
141
    /**
142
     * Paginate all logs.
143
     *
144
     * @param  int  $perPage
145
     *
146
     * @return \Illuminate\Pagination\LengthAwarePaginator
147
     */
148 6
    public function paginate($perPage = 30)
149
    {
150 6
        return $this->logs()->paginate($perPage);
151
    }
152
153
    /**
154
     * Get a log by date.
155
     *
156
     * @param  string  $date
157
     *
158
     * @return \Arcanedev\LogViewer\Entities\Log
159
     */
160 18
    public function log($date)
161
    {
162 18
        return $this->logs()->log($date);
163
    }
164
165
    /**
166
     * Get a log by date (alias).
167
     *
168
     * @param  string  $date
169
     *
170
     * @return \Arcanedev\LogViewer\Entities\Log
171
     */
172 3
    public function get($date)
173
    {
174 3
        return $this->log($date);
175
    }
176
177
    /**
178
     * Get log entries.
179
     *
180
     * @param  string  $date
181
     * @param  string  $level
182
     *
183
     * @return \Arcanedev\LogViewer\Entities\LogEntryCollection
184
     */
185 15
    public function entries($date, $level = 'all')
186
    {
187 15
        return $this->logs()->entries($date, $level);
188
    }
189
190
    /**
191
     * Get logs statistics.
192
     *
193
     * @return array
194
     */
195 39
    public function stats()
196
    {
197 39
        return $this->logs()->stats();
198
    }
199
200
    /**
201
     * Get logs statistics table.
202
     *
203
     * @param  string|null  $locale
204
     *
205
     * @return \Arcanedev\LogViewer\Tables\StatsTable
206
     */
207 15
    public function statsTable($locale = null)
208
    {
209 15
        return StatsTable::make($this->stats(), $this->levels, $locale);
210
    }
211
212
    /**
213
     * List the log files (dates).
214
     *
215
     * @return array
216
     */
217 9
    public function dates()
218
    {
219 9
        return $this->logs()->dates();
220
    }
221
222
    /**
223
     * Get logs count.
224
     *
225
     * @return int
226
     */
227 9
    public function count()
228
    {
229 9
        return $this->logs()->count();
230
    }
231
232
    /**
233
     * Get total log entries.
234
     *
235
     * @param  string  $level
236
     *
237
     * @return int
238
     */
239 12
    public function total($level = 'all')
240
    {
241 12
        return $this->logs()->total($level);
242
    }
243
244
    /**
245
     * Get tree menu.
246
     *
247
     * @param  bool|false  $trans
248
     *
249
     * @return array
250
     */
251 6
    public function tree($trans = false)
252
    {
253 6
        return $this->logs()->tree($trans);
254
    }
255
256
    /**
257
     * Get tree menu.
258
     *
259
     * @param  bool|true  $trans
260
     *
261
     * @return array
262
     */
263 9
    public function menu($trans = true)
264
    {
265 9
        return $this->logs()->menu($trans);
266
    }
267
268
    /* ------------------------------------------------------------------------------------------------
269
     |  Check Functions
270
     | ------------------------------------------------------------------------------------------------
271
     */
272
    /**
273
     * Determine if the log folder is empty or not.
274
     *
275
     * @return bool
276
     */
277 6
    public function isEmpty()
278
    {
279 6
        return $this->logs()->isEmpty();
280
    }
281
}
282