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

OutputTrait   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Importance

Changes 7
Bugs 0 Features 1
Metric Value
wmc 7
eloc 23
c 7
b 0
f 1
dl 0
loc 55
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A outputCli() 0 3 1
A setOutputCode() 0 3 1
A setOutputLoader() 0 4 1
A output() 0 3 1
A outputHtmlPartial() 0 6 1
A outputHtml() 0 6 1
A outputJson() 0 10 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