Completed
Push — update-travis ( 67cde9...6f3c47 )
by Kevin
07:18 queued 03:01
created

View::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 1.0008

Importance

Changes 5
Bugs 2 Features 1
Metric Value
c 5
b 2
f 1
dl 0
loc 13
ccs 10
cts 11
cp 0.9091
rs 9.4286
cc 1
eloc 11
nc 1
nop 5
crap 1.0008
1
<?php
2
3
namespace Zenstruck\ControllerUtil;
4
5
/**
6
 * @author Kevin Bond <[email protected]>
7
 */
8
class View
9
{
10
    const DEFAULT_STATUS_CODE = 200;
11
12
    private $data;
13
    private $statusCode;
14
    private $template;
15
    private $cache;
16
    private $headers;
17
18
    /**
19
     * @param mixed $data
20
     * @param int   $sharedMaxAge
21
     * @param int   $statusCode
22
     *
23
     * @return static
24
     */
25 1
    public static function createCached($data, $sharedMaxAge, $statusCode = self::DEFAULT_STATUS_CODE)
26
    {
27
        return new static($data, $statusCode, null, array('s_maxage' => $sharedMaxAge));
28 1
    }
29
30
    /**
31
     * @param mixed|null   $data
32
     * @param int          $statusCode
33
     * @param string|array $template
34
     * @param array        $cache
35
     * @param array        $headers
36
     */
37 16
    public function __construct(
38
        $data = null,
39 8
        $statusCode = self::DEFAULT_STATUS_CODE,
40 8
        $template = null,
41 8
        array $cache = array(),
42 9
        array $headers = array()
43
    ) {
44 16
        $this->data = $data;
45 16
        $this->statusCode = $statusCode;
46 16
        $this->template = $template;
47 16
        $this->cache = $cache;
48 16
        $this->headers = $headers;
49
    }
50
51
    /**
52
     * @return mixed|null
53
     */
54 10
    public function getData()
55
    {
56 10
        return $this->data;
57
    }
58
59
    /**
60
     * @param string $key The key to use when creating array if data isn't already an array
61
     *
62
     * @return array
63
     */
64 4
    public function getDataAsArray($key = 'data')
65
    {
66
        $data = $this->getData();
67
68 4
        if (null === $data) {
69 1
            return array();
70
        }
71
72
        if (!is_array($data)) {
73 2
            $data = array($key => $data);
74
        }
75
76 3
        return $data;
77 4
    }
78
79
    /**
80
     * @return int
81
     */
82 5
    public function getStatusCode()
83
    {
84 5
        return $this->statusCode;
85
    }
86
87
    /**
88
     * Either the template name or an array of templates.
89
     *
90
     * @return string|array|null
91
     */
92 7
    public function getTemplate()
93
    {
94 7
        return $this->template;
95
    }
96
97
    /**
98
     * Returns an array of cache options.
99
     *
100
     * @see Symfony\Component\HttpFoundation\Response::setCache
101
     *
102
     * @return array
103
     */
104 7
    public function getCache()
105
    {
106 7
        return $this->cache;
107
    }
108
109
    /**
110
     * @return array
111
     */
112 6
    public function getHeaders()
113
    {
114 6
        return $this->headers;
115
    }
116
}
117