Completed
Push — master ( 7bfc6c...554965 )
by Joram van den
03:50
created

Ajde_Resource::getLinkCode()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 24
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
c 4
b 0
f 0
dl 0
loc 24
rs 8.9713
cc 3
eloc 12
nc 4
nop 0
1
<?php
2
3
abstract class Ajde_Resource extends Ajde_Object_Standard
4
{
5
    const TYPE_JAVASCRIPT = 'js';
6
    const TYPE_STYLESHEET = 'css';
7
8
    public function __construct($type)
9
    {
10
        $this->setType($type);
0 ignored issues
show
Documentation Bug introduced by
The method setType does not exist on object<Ajde_Resource>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
11
    }
12
13
    public function __toString()
14
    {
15
        return implode(", ", $this->_data);
16
    }
17
18
    abstract public function getFilename();
19
20
    abstract protected function getLinkUrl();
21
22
    public function getType()
23
    {
24
        return $this->get('type');
25
    }
26
27
    public function setPosition($position)
28
    {
29
        $this->set('position', $position);
30
    }
31
32
    public function getPosition()
33
    {
34
        return $this->get('position');
35
    }
36
37
    protected function _getLinkTemplateFilename()
38
    {
39
        $format = $this->hasFormat() ? $this->getFormat() : null;
0 ignored issues
show
Documentation Bug introduced by
The method hasFormat does not exist on object<Ajde_Resource>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
Documentation Bug introduced by
The method getFormat does not exist on object<Ajde_Resource>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
40
41
        return self::getLinkTemplateFilename($this->getType(), $format);
42
    }
43
44
    public static function getLinkTemplateFilename($type, $format = 'null')
45
    {
46
        if (Ajde::app()->getDocument()->hasLayout()) {
0 ignored issues
show
Documentation Bug introduced by
The method hasLayout does not exist on object<Ajde_Document>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
47
            $layout = Ajde::app()->getDocument()->getLayout();
48
        } else {
49
            $layout = new Ajde_Layout(config("layout.frontend"));
50
        }
51
        $format = issetor($format, 'html');
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $format is correct as issetor($format, 'html') (which targets issetor()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
52
53
        $dirPrefixPatterns = [
54
            APP_DIR,
55
            CORE_DIR
56
        ];
57
        foreach ($dirPrefixPatterns as $dirPrefixPattern) {
58
            $prefixedLayout = $dirPrefixPattern . LAYOUT_DIR;
59
            if (self::exist($prefixedLayout . $layout->getName() . '/link/' . $type . '.' . $format . '.php')) {
60
                return $prefixedLayout . $layout->getName() . '/link/' . $type . '.' . $format . '.php';
61
            }
62
        }
63
64
        return false;
65
    }
66
67
    protected static function exist($filename)
68
    {
69
        if (is_file(self::realpath($filename))) {
70
            return true;
71
        }
72
73
        return false;
74
    }
75
76
    protected static function realpath($filename)
77
    {
78
        return LOCAL_ROOT . $filename;
79
    }
80
81
    public static function encodeFingerprint($array)
82
    {
83
        return self::_urlEncode(serialize($array));
84
    }
85
86
    public static function decodeFingerprint($fingerprint)
87
    {
88
        return unserialize(self::_urlDecode($fingerprint));
89
    }
90
91
    public static function _urlDecode($string)
92
    {
93
        // return self::_rotUrl($string);
94
        return base64_decode($string);
95
    }
96
97
    public static function _urlEncode($string)
98
    {
99
        // return self::_rotUrl($string);
100
        return base64_encode($string);
101
    }
102
103
    public static function _rotUrl($string)
104
    {
105
        return strtr($string,
106
            '/-:?=&%#{}"; QXJKVWPYRHGB abcdefghijklmnopqrstuv123456789ACDEFILMNOSTUwxyz',
107
            'QXJKVWPYRHGB /-:?=&%#{}"; 123456789ACDEFILMNOSTUabcdefghijklmnopqrstuvwxyz');
108
    }
109
110
    public function getLinkCode()
111
    {
112
        ob_start();
113
114
        // variables for use in included link template
115
        $url       = $this->getLinkUrl();
116
        $arguments = $this->hasArguments() ? $this->getArguments() : '';
0 ignored issues
show
Documentation Bug introduced by
The method hasArguments does not exist on object<Ajde_Resource>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
Documentation Bug introduced by
The method getArguments does not exist on object<Ajde_Resource>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
117
118
        // create temporary resource for link filename
119
        $linkFilename = $this->_getLinkTemplateFilename();
120
121
        // TODO: performance gain?
122
        // Ajde_Cache::getInstance()->addFile($linkFilename);
123
        if ($linkFilename) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $linkFilename of type string|false is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
124
            include LOCAL_ROOT . $linkFilename;
125
        } else {
126
            throw new Ajde_Exception('Link filename for ' . $url . ' not found');
127
        }
128
129
        $contents = ob_get_contents();
130
        ob_end_clean();
131
132
        return $contents;
133
    }
134
135
    public function getContents()
136
    {
137
        ob_start();
138
139
        $filename = $this->getFilename();
140
141
        Ajde_Cache::getInstance()->addFile($filename);
142
        if ($this->exist($filename)) {
143
            include $this->realpath($filename);
144
        }
145
146
        $contents = ob_get_contents();
147
        ob_end_clean();
148
149
        return $contents;
150
    }
151
}
152