Completed
Push — master ( 17d0ae...6d8d7c )
by François
02:20
created

DefaultProfile::initResponseCodeHandler()   B

Complexity

Conditions 4
Paths 2

Size

Total Lines 22
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 22
rs 8.9197
cc 4
eloc 13
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
        self::loadAnalyzers($instance);
22
23
        self::initCache($instance);
24
25
        self::initResponseCodeHandler($instance);
26
    }
27
28
    public function loadAnalyzers(Bouncer $instance)
29
    {
30
        // Load Default analyzers
31
        \Bouncer\Analyzer\Hostname::load($instance);
32
    }
33
34
    public function initCache(Bouncer $instance)
35
    {
36
        // If no cache available, try to set up APC
37
        $cache = $instance->getCache();
38
        if (empty($cache)) {
39
            if (function_exists('apc_fetch')) {
40
                $cache = new \Bouncer\Cache\Apc();
41
                $instance->setOptions(array('cache' => $cache));
42
            } else {
43
                $instance->error('No cache available. A cache is needed to keep performances acceptable.');
44
            }
45
        }
46
    }
47
48
    public function initResponseCodeHandler(Bouncer $instance)
49
    {
50
        if (function_exists('http_response_code')) {
51
            $responseCodeHandler = function($code = null) {
52
                return http_response_code($code);
53
            };
54
        }
55
        else {
56
            // If http_response_code not available (PHP 5.3), set a custom response code setter
57
            $responseCodeHandler = function($code = null, $message = null) {
58
                static $currentCode = 200;
59
                if ($code && $message) {
60
                    header("HTTP/1.0 $code $message");
61
                    header("Status: $code $message");
62
                    $currentCode = $code;
63
                }
64
                return $currentCode;
65
            };
66
        }
67
68
        $instance->setOptions(array('responseCodeHandler' => $responseCodeHandler));
69
    }
70
}
71