Completed
Push — master ( bb5d55...8400a7 )
by Piotr
02:12
created

EsiRequest::isNoCache()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace CrazyGoat\Octophpus;
4
5
use CrazyGoat\Octophpus\Exception\EsiTagParseException;
6
use CrazyGoat\Octophpus\Validator\EsiAttributeValidator;
7
8
class EsiRequest
9
{
10
    /**
11
     * @var string
12
     */
13
    private $src;
14
15
    /**
16
     * @var string
17
     */
18
    private $esiTag;
19
20
    /**
21
     * @var float|null
22
     */
23
    private $timeout = null;
24
25
    /**
26
     * @var bool
27
     */
28
    private $noCache = false;
29
30
    public function __construct(string $esiTag)
31
    {
32
        $this->esiTag = $esiTag;
33
        $this->parse($esiTag);
34
    }
35
36
    private function parse(string $esiTag) : void
37
    {
38
        $p = xml_parser_create();
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 11 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
39
        $parseStatus = xml_parse_into_struct($p, $esiTag, $values);
40
        xml_parser_free($p);
41
42
        if ($parseStatus == 0) {
43
            throw new EsiTagParseException("Unable to parse xml: ".$esiTag);
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal Unable to parse xml: does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
44
        }
45
46
        $parsedTag = $this->getTag($values);
47
48
        $validator = new EsiAttributeValidator($parsedTag['attributes']);
49
        $validator->validate();
50
51
        $this->src = $parsedTag['attributes']['SRC'];
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 5 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
52
        $this->timeout = isset($parsedTag['attributes']['TIMEOUT']) ? (float)$parsedTag['attributes']['TIMEOUT'] : null;
53
        $this->noCache = isset($parsedTag['attributes']['NOCACHE']);
54
55
    }
56
57
    private function getTag(array $tags) : array
58
    {
59
        if (!is_array($tags) || !isset($tags[0])) {
60
            throw new EsiTagParseException("No valid html tags found");
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal No valid html tags found does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
61
        }
62
63
        $tag = $tags[0];
64
65
        if ($tag['tag'] !== 'ESI:INCLUDE') {
66
            throw new EsiTagParseException("No valid html tags found");
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal No valid html tags found does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
67
        }
68
69
        if (!isset($tag['attributes']['SRC'])) {
70
            throw new EsiTagParseException("Esi tag does not have required parameter src");
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal Esi tag does not have required parameter src does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
71
        }
72
73
        return $tag;
74
    }
75
76
    /**
77
     * @return string
78
     */
79
    public function getSrc(): string
80
    {
81
        return $this->src;
82
    }
83
84
    /**
85
     * @return string
86
     */
87
    public function getEsiTag(): string
88
    {
89
        return $this->esiTag;
90
    }
91
92
    public function requestOptions() : array
93
    {
94
        $options = [];
95
96
        if (!empty($this->timeout)) {
97
            $options['connect_timeout'] = $this->timeout;
98
        }
99
100
        return $options;
101
    }
102
103
    /**
104
     * @return bool
105
     */
106
    public function isNoCache(): bool
107
    {
108
        return $this->noCache;
109
    }
110
}