Passed
Branch issue_11 (3b88d6)
by Jay
13:12 queued 23s
created

Factory   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 51
ccs 11
cts 11
cp 1
rs 10
c 0
b 0
f 0
wmc 9
lcom 1
cbo 4

4 Methods

Rating   Name   Duplication   Size   Complexity  
A get() 0 10 3
A isCli() 0 4 1
A isJson() 0 5 3
A getHeaders() 0 4 2
1
<?php
2
namespace FMUP\Request;
3
4
class Factory
5
{
6
    use \FMUP\Sapi\OptionalTrait;
7
8
    const CONTENT_TYPE = 'Content-Type';
9
    const JSON_HEADER = 'application/json';
10
11
    /**
12
     * Build the correct request object
13
     * @return Cli|Http|Json
14
     */
15 7
    public function get()
16
    {
17 7
        if ($this->isCli()) {
18 2
            return new \FMUP\Request\Cli();
19
        }
20 5
        if ($this->isJson()) {
21 1
            return new \FMUP\Request\Json();
22
        }
23 4
        return new \FMUP\Request\Http();
24
    }
25
26
    /**
27
     * Checks if request should be cli
28
     * @return bool
29
     */
30 7
    private function isCli()
31
    {
32 7
        return $this->getSapi()->get() == \FMUP\Sapi::CLI;
33
    }
34
35
    /**
36
     * Checks if request should be json
37
     * @return bool
38
     */
39 5
    private function isJson()
40
    {
41 5
        $headers = !$this->isCli() ? (array)$this->getHeaders() : [];
42 5
        return isset($headers[self::CONTENT_TYPE]) && in_array(self::JSON_HEADER, (array)$headers[self::CONTENT_TYPE]);
43
    }
44
45
    /**
46
     * Retrieve headers of the request
47
     * @codeCoverageIgnore
48
     * @return array|false
49
     */
50
    protected function getHeaders()
51
    {
52
        return function_exists('getallheaders') ? getallheaders() : false;
53
    }
54
}
55