Completed
Branch master (e7ce71)
by Joao
02:26 queued 10s
created

ServiceHandler::setOutput()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 9
rs 9.6666
cc 3
eloc 4
nc 2
nop 1
1
<?php
2
3
namespace ByJG\RestServer;
4
5
use ByJG\Serialize\Formatter\JsonFormatter;
6
use ByJG\Serialize\Formatter\XmlFormatter;
7
8
class ServiceHandler implements HandlerInterface
9
{
10
11
    protected $output = Output::JSON;
12
13
    public function getOutput()
14
    {
15
        return $this->output;
16
    }
17
18
    public function setOutput($output)
19
    {
20
        // Check if output is set
21
        if ($output != Output::JSON && $output != Output::XML) {
22
            throw new \Exception('Invalid output format. Valid are XML or JSON');
23
        }
24
25
        $this->output = $output;
26
    }
27
28
    public function setHeader()
29
    {
30
        switch ($this->getOutput()) {
31
            case Output::JSON:
32
                header('Content-Type: application/json');
33
                break;
34
35
            case Output::XML:
36
                header('Content-Type: text/xml');
37
                break;
38
        }
39
    }
40
41
    /**
42
     * CORS - Better do that in your http server, but you can enable calling this
43
     */
44
    public function setHeaderCors()
0 ignored issues
show
Coding Style introduced by
setHeaderCors uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
45
    {
46
        header('Access-Control-Allow-Origin: *');
47
        header("Access-Control-Allow-Headers: X-API-KEY, Origin, X-Requested-With, Content-Type, Accept, Access-Control-Request-Method");
48
        header("Access-Control-Allow-Methods: GET, POST, OPTIONS, PUT, DELETE");
49
        $method = $_SERVER['REQUEST_METHOD'];
50
        if ($method == "OPTIONS") {
51
            return false;
52
        }
53
        return true;
54
    }
55
56
    public function execute(ServiceAbstract $instance)
57
    {
58
59
        $serialized = $instance->getResponse()->getResponseBag()->process();
60
61
        switch ($this->getOutput()) {
62
            case Output::JSON:
63
                return (new JsonFormatter())->process($serialized);
64
65
            case Output::XML:
66
                return (new XmlFormatter())->process($serialized);
67
68
        }
69
70
        return null;
71
    }
72
}
73