AuditFactory   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 20
c 1
b 0
f 0
dl 0
loc 28
rs 10
wmc 3

1 Method

Rating   Name   Duplication   Size   Complexity  
A create() 0 26 3
1
<?php
2
3
namespace SilverStripe\Auditor;
4
5
use Exception;
6
use Monolog\Formatter\LineFormatter;
7
use Monolog\Handler\SyslogHandler;
8
use Monolog\Logger;
9
use Monolog\Processor\WebProcessor;
10
use SilverStripe\Core\Injector\Factory;
11
12
/**
13
 * Logs are written using a side-channel, because audit trail should not be mixed
14
 * up with regular PHP errors.
15
 */
16
class AuditFactory implements Factory
17
{
18
    public function create($service, array $params = [])
19
    {
20
        if (!empty($params)) {
21
            throw new Exception('AuditFactory does not support passing params.');
22
        }
23
24
        $obj = null;
0 ignored issues
show
Unused Code introduced by
The assignment to $obj is dead and can be removed.
Loading history...
25
        switch ($service) {
26
            case 'AuditLogger':
27
                $log = new Logger('audit');
28
                $syslog = new SyslogHandler('SilverStripe_audit', LOG_AUTH, Logger::DEBUG);
29
                $syslog->pushProcessor(new WebProcessor($_SERVER, [
30
                    'url'         => 'REQUEST_URI',
31
                    'http_method' => 'REQUEST_METHOD',
32
                    'server'      => 'SERVER_NAME',
33
                    'referrer'    => 'HTTP_REFERER',
34
                ]));
35
36
                $syslog->pushProcessor(new RealIPProcessor());
37
                $formatter = new LineFormatter("%level_name%: %message% %context% %extra%");
38
                $syslog->setFormatter($formatter);
39
                $log->pushHandler($syslog);
40
41
                return $log;
42
            default:
43
                throw new Exception(sprintf("AuditFactory does not support creation of '%s'.", $service));
44
        }
45
    }
46
}
47