Completed
Push — master ( 19b307...d2f1d7 )
by François
02:15
created

DefaultProfile::initLogger()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 1
nc 1
nop 1
1
<?php
2
3
/*
4
 * This file is part of the Bouncer package.
5
 *
6
 * (c) François Hodierne <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Bouncer\Profile;
13
14
use Bouncer\Bouncer;
15
16
class DefaultProfile
17
{
18
19
    public function load(Bouncer $instance)
20
    {
21
        $this->loadAnalyzers($instance);
22
23
        $this->initCache($instance);
24
25
        $this->initLogger($instance);
26
27
        $this->initResponseCodeHandler($instance);
28
    }
29
30
    public function loadAnalyzers(Bouncer $instance)
31
    {
32
        // Load Default analyzers
33
        \Bouncer\Analyzer\Hostname::load($instance);
34
    }
35
36
    public function initCache(Bouncer $instance)
37
    {
38
        // If no cache available, try to set up APC
39
        $cache = $instance->getCache();
40
        if (empty($cache)) {
41
            if (function_exists('apc_fetch')) {
42
                $cache = new \Bouncer\Cache\Apc();
43
                $instance->setOptions(array('cache' => $cache));
44
            } else {
45
                $instance->error('No cache available. A cache is needed to keep performances acceptable.');
46
            }
47
        }
48
    }
49
50
    public function initLogger(Bouncer $instance)
0 ignored issues
show
Unused Code introduced by
The parameter $instance is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
51
    {
52
        // No default logger for now
53
    }
54
55
    public function initResponseCodeHandler(Bouncer $instance)
56
    {
57
        if (function_exists('http_response_code')) {
58
            $responseCodeHandler = function($code = null) {
59
                if ($code) {
60
                    return http_response_code($code);
61
                } else {
62
                    return http_response_code();
63
                }
64
            };
65
        }
66
        else {
67
            // If http_response_code not available (PHP 5.3), set a custom response code setter
68
            $responseCodeHandler = function($code = null, $message = null) {
69
                static $currentCode = 200;
70
                if ($code && $message) {
71
                    header("HTTP/1.0 $code $message");
72
                    header("Status: $code $message");
73
                    $currentCode = $code;
74
                }
75
                return $currentCode;
76
            };
77
        }
78
79
        $instance->setOptions(array('responseCodeHandler' => $responseCodeHandler));
80
    }
81
}
82