DefaultProfile::initResponseCodeHandler()   B
last analyzed

Complexity

Conditions 5
Paths 2

Size

Total Lines 26
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 26
rs 8.439
c 0
b 0
f 0
cc 5
eloc 16
nc 2
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
            }
45
        }
46
    }
47
48
    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...
49
    {
50
        // No default logger for now
51
    }
52
53
    public function initResponseCodeHandler(Bouncer $instance)
54
    {
55
        if (function_exists('http_response_code')) {
56
            $responseCodeHandler = function($code = null) {
57
                if ($code) {
58
                    return http_response_code($code);
59
                } else {
60
                    return http_response_code();
61
                }
62
            };
63
        }
64
        else {
65
            // If http_response_code not available (PHP 5.3), set a custom response code setter
66
            $responseCodeHandler = function($code = null, $message = null) {
67
                static $currentCode = 200;
68
                if ($code && $message) {
69
                    header("HTTP/1.0 $code $message");
70
                    header("Status: $code $message");
71
                    $currentCode = $code;
72
                }
73
                return $currentCode;
74
            };
75
        }
76
77
        $instance->setOptions(array('responseCodeHandler' => $responseCodeHandler));
78
    }
79
}
80