Completed
Push — master ( 12a557...496eb3 )
by François
02:35
created

DefaultProfile::initResponseCodeSetter()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 12
rs 9.4285
cc 2
eloc 6
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::initResponseCodeSetter($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 initResponseCodeSetter(Bouncer $instance)
49
    {
50
        // If http_response_code not available (PHP 5.3), set a custom response code setter
51
        if (!function_exists('http_response_code')) {
52
            $responseCodeSetter = function($code, $message) {
53
                header("HTTP/1.0 $code $message");
54
                header("Status: $code $message");
55
            };
56
57
            $instance->setOptions(array('responseCodeSetter' => $responseCodeSetter));
58
        }
59
    }
60
}
61