Passed
Push — master ( 8beb07...b6ab00 )
by Radu
03:02
created

OutputTrait::setOutputCode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 1
1
<?php
2
namespace WebServCo\Framework\Traits;
3
4
use WebServCo\Framework\Http\Response;
5
6
trait OutputTrait
7
{
8
    protected $outputCode;
9
    protected $outputLoader;
10
11
    final protected function setOutputLoader(\WebServCo\Framework\AbstractOutputLoader $outputLoader)
12
    {
13
        $this->outputCode = 200; // default
14
        $this->outputLoader = $outputLoader;
15
    }
16
17
    final protected function output()
18
    {
19
        return $this->outputLoader;
20
    }
21
22
    final protected function outputCli($string = '', $eol = true)
23
    {
24
        return $this->output()->cli($string, $eol);
25
    }
26
27
    protected function outputHtmlPartial($data, $template)
28
    {
29
        return new Response(
30
            $this->output()->html($data, $template),
31
            $this->outputCode,
32
            ['Content-Type' => 'text/html']
33
        );
34
    }
35
36
    protected function outputHtml($data, $pageTemplate, $mainTemplate = null)
37
    {
38
        return new Response(
39
            $this->output()->htmlPage($data, $pageTemplate, $mainTemplate),
40
            $this->outputCode,
41
            ['Content-Type' => 'text/html']
42
        );
43
    }
44
45
    protected function outputJson($content, $result = true)
46
    {
47
        $data = [
48
            'result' => $result,
49
            'data' => $content,
50
        ];
51
        return new Response(
52
            $this->output()->json($data),
53
            $this->outputCode,
54
            ['Content-Type' => 'application/json']
55
        );
56
    }
57
58
    protected function setOutputCode($outputCode)
59
    {
60
        $this->outputCode = $outputCode;
61
    }
62
}
63