Passed
Push — master ( c155b1...0458dd )
by Dev
03:34
created

RedirectionTrait   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 8
eloc 20
dl 0
loc 43
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A manageRedirection() 0 17 4
A getRedirection() 0 7 2
A getRedirectionCode() 0 7 2
1
<?php
2
3
namespace PiedWeb\CMSBundle\Entity;
4
5
use Doctrine\ORM\Mapping as ORM;
6
use Cocur\Slugify\Slugify;
7
use Gedmo\Mapping\Annotation as Gedmo;
8
9
trait RedirectionTrait
10
{
11
    protected $redirectionUrl;
12
    protected $redirectionCode;
13
14
    /**
15
     * Check if a content don't start by 'Location: http://valid-url.tld/eg'.
16
     */
17
    protected function manageRedirection()
18
    {
19
        $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

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