Completed
Push — master ( a74b95...6c1afa )
by
unknown
03:12
created

Application::getHttpBody()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace HelePartnerSyncApi;
4
5
use Closure;
6
use HelePartnerSyncApi\Method\CancelReservation;
7
use HelePartnerSyncApi\Method\CheckHealth;
8
use HelePartnerSyncApi\Method\CreateReservation;
9
use HelePartnerSyncApi\Method\GetSlots;
10
use HelePartnerSyncApi\Request\DefaultRequestFactory;
11
use HelePartnerSyncApi\Response\DefaultResponseFactory;
12
13
class Application
14
{
15
16
	/**
17
	 * @var Client
18
	 */
19
	private $client;
20
21
	/**
22
	 * @param string $secret
23
	 */
24
	public function __construct($secret)
25
	{
26
		$this->client = new Client(
27
			new DefaultRequestFactory($secret),
28
			new DefaultResponseFactory($secret)
29
		);
30
		$this->client->registerMethod(new CheckHealth());
31
	}
32
33
	/**
34
	 * @param Closure $callback function (DateTime $date, array $parameters)
35
	 */
36
	public function onGetSlots(Closure $callback)
37
	{
38
		$this->client->registerMethod(new GetSlots($callback));
39
	}
40
41
	/**
42
	 * @param Closure $callback function (DateTime $startDateTime, DateTime $endDateTime, int $quantity, array $parameters)
43
	 */
44
	public function onCreateReservation(Closure $callback)
45
	{
46
		$this->client->registerMethod(new CreateReservation($callback));
47
	}
48
49
	/**
50
	 * @param Closure $callback function (DateTime $startDateTime, DateTime $endDateTime, int $quantity, array $parameters)
51
	 */
52
	public function onCancelReservation(Closure $callback)
53
	{
54
		$this->client->registerMethod(new CancelReservation($callback));
55
	}
56
57
	public function run()
58
	{
59
		$this->client->run(
60
			$this->getHttpBody(),
61
			$this->getHttpHeaders()
62
		)->render();
63
	}
64
65
	/**
66
	 * @return string
67
	 */
68
	private function getHttpBody()
69
	{
70
		return file_get_contents('php://input');
71
	}
72
73
	/**
74
	 * @return string[]
75
	 */
76
	private function getHttpHeaders()
0 ignored issues
show
Coding Style introduced by
getHttpHeaders 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...
77
	{
78
		if (function_exists('apache_request_headers')) {
79
			return apache_request_headers();
80
		}
81
82
		$headers = array();
83
		foreach ($_SERVER as $k => $v) {
84
			if (strncmp($k, 'HTTP_', 5) == 0) {
85
				$k = substr($k, 5);
86
			} elseif (strncmp($k, 'CONTENT_', 8)) {
87
				continue;
88
			}
89
			$headers[strtr($k, '_', '-')] = $v;
90
		}
91
92
		return $headers;
93
	}
94
95
}
96