Completed
Push — master ( ee9044...c7715b )
by ARCANEDEV
9s
created

Factory::setPattern()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

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 9
cc 1
eloc 6
nc 1
nop 3
ccs 3
cts 3
cp 1
crap 1
rs 9.6666
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 888
    public function __construct(
44
        FilesystemInterface $filesystem,
45
        LogLevelsInterface $levels
46
    ) {
47 888
        $this->setFilesystem($filesystem);
48 888
        $this->setLevels($levels);
49 888
    }
50
51
    /* ------------------------------------------------------------------------------------------------
52
     |  Getter & Setters
53
     | ------------------------------------------------------------------------------------------------
54
     */
55
    /**
56
     * Get the filesystem instance.
57
     *
58
     * @return \Arcanedev\LogViewer\Contracts\FilesystemInterface
59
     */
60 12
    public function getFilesystem()
61
    {
62 12
        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 888
    public function setFilesystem(FilesystemInterface $filesystem)
73
    {
74 888
        $this->filesystem = $filesystem;
75
76 888
        return $this;
77
    }
78
79
    /**
80
     * Get the log levels instance.
81
     *
82
     * @return \Arcanedev\LogViewer\Contracts\LogLevelsInterface
83
     */
84 12
    public function getLevels()
85
    {
86 12
        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 888
    public function setLevels(LogLevelsInterface $levels)
97
    {
98 888
        $this->levels = $levels;
99
100 888
        return $this;
101
    }
102
103
    /**
104
     * Set the log storage path.
105
     *
106
     * @param  string  $storagePath
107
     *
108
     * @return self
109
     */
110 24
    public function setPath($storagePath)
111
    {
112 24
        $this->filesystem->setPath($storagePath);
113
114 24
        return $this;
115 9
    }
116
117
    /**
118
     * Get the log pattern.
119
     *
120
     * @return string
121
     */
122 24
    public function getPattern()
123
    {
124 24
        return $this->filesystem->getPattern();
125
    }
126
127
    /**
128
     * Set the log pattern.
129
     *
130
     * @param  string  $date
131
     * @param  string  $prefix
132
     * @param  string  $extension
133
     *
134
     * @return self
135
     */
136 24
    public function setPattern(
137
        $prefix    = FilesystemInterface::PATTERN_PREFIX,
138
        $date      = FilesystemInterface::PATTERN_DATE,
139
        $extension = FilesystemInterface::PATTERN_EXTENSION
140
    ) {
141 24
        $this->filesystem->setPattern($prefix, $date, $extension);
142
143 24
        return $this;
144
    }
145
146
    /**
147
     * Get all logs.
148
     *
149
     * @return \Arcanedev\LogViewer\Entities\LogCollection
150
     */
151 516
    public function logs()
152
    {
153 516
        return LogCollection::make()->setFilesystem($this->filesystem);
154
    }
155
156
    /* ------------------------------------------------------------------------------------------------
157
     |  Main Functions
158
     | ------------------------------------------------------------------------------------------------
159
     */
160
    /**
161
     * Get all logs. (alias)
162
     *
163
     * @return \Arcanedev\LogViewer\Entities\LogCollection
164
     */
165 24
    public function all()
166
    {
167 24
        return $this->logs();
168
    }
169
170
    /**
171
     * Paginate all logs.
172
     *
173
     * @param  int  $perPage
174
     *
175
     * @return \Illuminate\Pagination\LengthAwarePaginator
176
     */
177 24
    public function paginate($perPage = 30)
178
    {
179 24
        return $this->logs()->paginate($perPage);
180
    }
181
182
    /**
183
     * Get a log by date.
184
     *
185
     * @param  string  $date
186
     *
187
     * @return \Arcanedev\LogViewer\Entities\Log
188
     */
189 72
    public function log($date)
190
    {
191 72
        return $this->logs()->log($date);
192
    }
193
194
    /**
195
     * Get a log by date (alias).
196
     *
197
     * @param  string  $date
198
     *
199
     * @return \Arcanedev\LogViewer\Entities\Log
200
     */
201 12
    public function get($date)
202
    {
203 12
        return $this->log($date);
204
    }
205
206
    /**
207
     * Get log entries.
208
     *
209
     * @param  string  $date
210
     * @param  string  $level
211
     *
212
     * @return \Arcanedev\LogViewer\Entities\LogEntryCollection
213
     */
214 60
    public function entries($date, $level = 'all')
215
    {
216 60
        return $this->logs()->entries($date, $level);
217
    }
218
219
    /**
220
     * Get logs statistics.
221
     *
222
     * @return array
223
     */
224 156
    public function stats()
225
    {
226 156
        return $this->logs()->stats();
227
    }
228
229
    /**
230
     * Get logs statistics table.
231
     *
232
     * @param  string|null  $locale
233
     *
234
     * @return \Arcanedev\LogViewer\Tables\StatsTable
235
     */
236 60
    public function statsTable($locale = null)
237
    {
238 60
        return StatsTable::make($this->stats(), $this->levels, $locale);
239
    }
240
241
    /**
242
     * List the log files (dates).
243
     *
244
     * @return array
245
     */
246 36
    public function dates()
247
    {
248 36
        return $this->logs()->dates();
249
    }
250
251
    /**
252
     * Get logs count.
253
     *
254
     * @return int
255
     */
256 36
    public function count()
257
    {
258 36
        return $this->logs()->count();
259
    }
260
261
    /**
262
     * Get total log entries.
263
     *
264
     * @param  string  $level
265
     *
266
     * @return int
267
     */
268 48
    public function total($level = 'all')
269
    {
270 48
        return $this->logs()->total($level);
271
    }
272
273
    /**
274
     * Get tree menu.
275
     *
276
     * @param  bool|false  $trans
277
     *
278
     * @return array
279
     */
280 24
    public function tree($trans = false)
281
    {
282 24
        return $this->logs()->tree($trans);
283
    }
284
285
    /**
286
     * Get tree menu.
287
     *
288
     * @param  bool|true  $trans
289
     *
290
     * @return array
291
     */
292 36
    public function menu($trans = true)
293
    {
294 36
        return $this->logs()->menu($trans);
295
    }
296
297
    /* ------------------------------------------------------------------------------------------------
298
     |  Check Functions
299
     | ------------------------------------------------------------------------------------------------
300
     */
301
    /**
302
     * Determine if the log folder is empty or not.
303
     *
304
     * @return bool
305
     */
306 24
    public function isEmpty()
307
    {
308 24
        return $this->logs()->isEmpty();
309
    }
310
}
311