Completed
Push — master ( 604bee...544e34 )
by Jonathan
05:30
created

JsonController   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
wmc 5
c 0
b 0
f 0
lcom 0
cbo 2
dl 0
loc 49
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A pageHeader() 0 9 1
A pageFooter() 0 3 1
A restrictAccess() 0 7 2
A encode() 0 3 1
1
<?php
2
/**
3
 * webtrees-lib: MyArtJaub library for webtrees
4
 *
5
 * @package MyArtJaub\Webtrees
6
 * @subpackage Controller
7
 * @author Jonathan Jaubart <[email protected]>
8
 * @copyright Copyright (c) 2012, Jonathan Jaubart
9
 * @license http://www.gnu.org/licenses/gpl.html GNU General Public License, version 3
10
 */
11
 namespace MyArtJaub\Webtrees\Controller;
12
13
use Fisharebest\Webtrees\Controller\BaseController;
14
use MyArtJaub\Webtrees\Mvc\MvcException;
15
16
/**
17
 * Base controller for all Json pages
18
 */
19
class JsonController extends BaseController {
20
    
21
    /**
22
     * {@inheritDoc}
23
     * @see \Fisharebest\Webtrees\Controller\BaseController::pageHeader()
24
     */
25
    public function pageHeader() {        
26
        header('Content-Type: application/json');
27
        header('Cache-Control: no-cache, must-revalidate');
28
        header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
29
        // We've displayed the header - display the footer automatically
30
        register_shutdown_function(array($this, 'pageFooter'));
31
        
32
        return $this;
33
    }
34
    
35
    /**
36
     * {@inheritDoc}
37
     * @see \Fisharebest\Webtrees\Controller\BaseController::pageFooter()
38
     */
39
    public function pageFooter() {
40
        return $this;
41
    }
42
    
43
    /**
44
     * Restrict access.
45
     *
46
     * @param bool $condition
47
     *
48
     * @return $this
49
     */
50
    public function restrictAccess($condition) {
51
        if ($condition !== true) {
52
            throw new MvcException(403);
53
        }
54
    
55
        return $this;
56
    }
57
    
58
    /**
59
     * Encode the data to JSON format.
60
     * 
61
     * @param array $data Data to encode
62
     * @param number $options JSON options mask. See http://php.net/manual/fr/json.constants.php
63
     */
64
    public function encode(array $data, $options = 0) {
65
        echo json_encode($data, $options);
66
    }
67
}
68