Passed
Push — master ( 546f84...80d964 )
by Dev
04:01
created

PageRedirectionTrait::getRedirection()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
1
<?php
2
3
namespace PiedWeb\CMSBundle\Entity;
4
5
trait PageRedirectionTrait
6
{
7
    protected $redirectionUrl;
8
    protected $redirectionCode;
9
10
    /**
11
     * Check if a content don't start by 'Location: http://valid-url.tld/eg'.
12
     */
13
    protected function manageRedirection()
14
    {
15
        $content = $this->getMainContent();
0 ignored issues
show
Bug introduced by
It seems like getMainContent() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

15
        /** @scrutinizer ignore-call */ 
16
        $content = $this->getMainContent();
Loading history...
16
        $code = 301; // default symfony is 302...
17
        if ('Location:' == substr($content, 0, 9)) {
18
            $url = trim(substr($content, 9));
19
            if (preg_match('/ [1-5][0-9]{2}$/', $url, $match)) {
20
                $code = intval(trim($match[0]));
21
                $url = preg_replace('/ [1-5][0-9]{2}$/', '', $url);
22
            }
23
            if (filter_var($url, FILTER_VALIDATE_URL)) {
24
                $this->redirectionUrl = $url;
25
                $this->redirectionCode = $code;
26
27
                return $url;
28
            }
29
        }
30
31
        $this->redirectionUrl = false;
32
    }
33
34
    public function getRedirection()
35
    {
36
        if (null === $this->redirectionUrl) {
37
            $this->manageRedirection();
38
        }
39
40
        return $this->redirectionUrl;
41
    }
42
43
    public function getRedirectionCode()
44
    {
45
        if (null === $this->redirectionUrl) {
46
            $this->manageRedirection();
47
        }
48
49
        return $this->redirectionCode;
50
    }
51
}
52