Ajde_Resource::getLinkUrl()
last analyzed

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
nc 1
dl 0
loc 1
c 0
b 0
f 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
    protected static function exist($filename)
23
    {
24
        if (is_file(self::realpath($filename))) {
25
            return true;
26
        }
27
28
        return false;
29
    }
30
31
    protected static function realpath($filename)
32
    {
33
        //        dump($filename, realpath(LOCAL_ROOT . $filename));
34
        return realpath(LOCAL_ROOT.$filename);
35
    }
36
37
    public static function encodeFingerprint($array)
38
    {
39
        return self::_urlEncode(serialize($array));
40
    }
41
42
    public static function decodeFingerprint($fingerprint)
43
    {
44
        return unserialize(self::_urlDecode($fingerprint));
45
    }
46
47
    public static function _urlDecode($string)
48
    {
49
        return base64_decode($string);
50
    }
51
52
    public static function _urlEncode($string)
53
    {
54
        return base64_encode($string);
55
    }
56
57
    public static function getLinkTemplateFilename($type, $format = 'null')
58
    {
59
        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...
60
            $layout = Ajde::app()->getDocument()->getLayout();
61
        } else {
62
            $layout = new Ajde_Layout(config('layout.frontend'));
63
        }
64
        $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...
65
66
        $dirPrefixPatterns = [
67
            APP_DIR,
68
            CORE_DIR,
69
        ];
70
        foreach ($dirPrefixPatterns as $dirPrefixPattern) {
71
            $prefixedLayout = $dirPrefixPattern.LAYOUT_DIR;
72
            if (self::exist($prefixedLayout.$layout->getName().'/link/'.$type.'.'.$format.'.php')) {
73
                return $prefixedLayout.$layout->getName().'/link/'.$type.'.'.$format.'.php';
74
            }
75
        }
76
77
        return false;
78
    }
79
80
    public function getType()
81
    {
82
        return $this->get('type');
83
    }
84
85
    public function setPosition($position)
86
    {
87
        $this->set('position', $position);
88
    }
89
90
    public function getPosition()
91
    {
92
        return $this->get('position');
93
    }
94
95
    protected function _getLinkTemplateFilename()
96
    {
97
        $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...
98
99
        return self::getLinkTemplateFilename($this->getType(), $format);
100
    }
101
102
    public function getLinkCode()
103
    {
104
        ob_start();
105
106
        // variables for use in included link template
107
        $url = $this->getLinkUrl();
108
        $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...
109
110
        // create temporary resource for link filename
111
        $linkFilename = $this->_getLinkTemplateFilename();
112
113
        // TODO: performance gain?
114
        // Ajde_Cache::getInstance()->addFile($linkFilename);
115
        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...
116
            include LOCAL_ROOT.$linkFilename;
117
        } else {
118
            throw new Ajde_Exception('Link filename for '.$url.' not found');
119
        }
120
121
        $contents = ob_get_contents();
122
        ob_end_clean();
123
124
        return $contents;
125
    }
126
127
    public function getContents()
128
    {
129
        ob_start();
130
131
        $filename = $this->getFilename();
132
133
        Ajde_Cache::getInstance()->addFile($filename);
134
        if ($this->exist($filename)) {
135
            include $this->realpath($filename);
136
        } else {
137
            throw new Exception("Couldn't find resource ".$filename);
138
        }
139
140
        $contents = ob_get_contents();
141
        ob_end_clean();
142
143
        return $contents;
144
    }
145
}
146