Completed
Push — master ( 3fde7a...7bfc6c )
by Joram van den
03:44
created

Ajde_Resource_Local::getArguments()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
c 4
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
class Ajde_Resource_Local extends Ajde_Resource
4
{
5
    private $_filename;
6
7
    public function __construct($type, $base, $action, $format = 'html', $arguments = '')
8
    {
9
        $this->setBase($base);
0 ignored issues
show
Documentation Bug introduced by
The method setBase does not exist on object<Ajde_Resource_Local>? 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...
10
        $this->setAction($action);
0 ignored issues
show
Documentation Bug introduced by
The method setAction does not exist on object<Ajde_Resource_Local>? 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
        $this->setFormat($format);
0 ignored issues
show
Documentation Bug introduced by
The method setFormat does not exist on object<Ajde_Resource_Local>? 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...
12
        $this->setArguments($arguments);
0 ignored issues
show
Documentation Bug introduced by
The method setArguments does not exist on object<Ajde_Resource_Local>? 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...
13
        parent::__construct($type);
14
    }
15
16
    /**
17
     *
18
     * @param string $type
19
     * @param string $base
20
     * @param string $action
21
     * @param string $format (optional)
22
     * @return Ajde_Resource
23
     */
24
    public static function lazyCreate($type, $base, $action, $format = 'html')
25
    {
26
        if (self::getFilenameFromStatic($base, $type, $action, $format)) {
0 ignored issues
show
Bug Best Practice introduced by
The expression self::getFilenameFromSta...type, $action, $format) 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...
27
            return new self($type, $base, $action, $format);
28
        }
29
30
        return false;
31
    }
32
33
    /**
34
     *
35
     * @param string $hash
36
     * @return Ajde_Resource
37
     * @throws Ajde_Core_Exception_Deprecated
38
     * @throws Ajde_Exception
39
     */
40
    public static function fromHash($hash)
0 ignored issues
show
Unused Code introduced by
The parameter $hash is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
41
    {
42
        // TODO:
43
        throw new Ajde_Core_Exception_Deprecated();
44
        $session = new Ajde_Session('AC.Resource');
0 ignored issues
show
Unused Code introduced by
$session = new \Ajde_Session('AC.Resource'); does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
45
46
        return $session->get($hash);
47
    }
48
49
    public static function fromFingerprint($type, $fingerprint)
50
    {
51
        $array = self::decodeFingerprint($fingerprint);
52
        extract($array);
53
54
        return new Ajde_Resource_Local($type, $b, $a, $f);
55
    }
56
57
    public function getFingerprint()
58
    {
59
        $array = ['b' => $this->getBase(), 'a' => $this->getAction(), 'f' => $this->getFormat()];
60
61
        return $this->encodeFingerprint($array);
62
    }
63
64
    public function getBase()
65
    {
66
        return $this->get('base');
67
    }
68
69
    public function getAction()
70
    {
71
        return $this->get('action');
72
    }
73
74
    public function getFormat()
75
    {
76
        return $this->get('format');
77
    }
78
79
    public function getArguments()
80
    {
81
        return $this->get('arguments');
82
    }
83
84
    protected static function _getFilename($base, $type, $action, $format)
85
    {
86
        $dirPrefixPatterns    = [
87
            CORE_DIR,
88
            APP_DIR
89
        ];
90
        $layoutDir            = 'layout.' . Ajde::app()->getDocument()->getLayout()->getName() . DIRECTORY_SEPARATOR;
91
        $layoutPrefixPatterns = ['', $layoutDir];
92
93
        $filename = false;
94
95
        foreach ($dirPrefixPatterns as $dirPrefixPattern) {
96
97
            foreach ($layoutPrefixPatterns as $layoutPrefixPattern) {
98
99
                $prefixedBase   = $dirPrefixPattern . $base;
100
                $formatResource = $prefixedBase . 'res/' . $type . DIRECTORY_SEPARATOR . $layoutPrefixPattern . $action . '.' . $format . '.' . $type;
101
102
                if (self::exist($formatResource)) {
103
104
                    $filename = $formatResource;
105
106
                } else {
107
108
                    $noFormatResource = $prefixedBase . 'res/' . $type . DIRECTORY_SEPARATOR . $layoutPrefixPattern . $action . '.' . $type;
109
                    if (self::exist($noFormatResource)) {
110
                        $filename = $noFormatResource;
111
                    }
112
113
                }
114
            }
115
        }
116
117
        return $filename;
118
    }
119
120
    public function getFilename()
121
    {
122
        if (!isset($this->_filename)) {
123
            $this->_filename = $this->_getFilename($this->getBase(), $this->getType(), $this->getAction(),
124
                $this->getFormat());
125
        }
126
127
        if (!$this->_filename) {
128
            // TODO:
129
            throw new Ajde_Exception(sprintf('Resource %s could not be found',
130
                $this->getBase() . 'res/' . $this->getType() . DS . $this->getAction() . '[.' . $this->getFormat() . '].' . $this->getType()));
131
        }
132
133
        return $this->_filename;
134
    }
135
136
    public static function getFilenameFromStatic($base, $type, $action, $format)
137
    {
138
        return self::_getFilename($base, $type, $action, $format);
139
    }
140
141
    protected function getLinkUrl()
142
    {
143
        $base = '_core/component:resourceLocal';
144
        if (config("app.debug") === true) {
145
            $url = $base . '/' . urlencode($this->getFingerprint()) . '.' . $this->getType() . '?' . str_replace([
146
                    '%2F',
147
                    '%5C'
148
                ], ':', urlencode($this->getFilename()));
149
        } else {
150
            $url = $base . '/' . urlencode($this->getFingerprint()) . '.' . $this->getType();
151
        }
152
153
        return $url;
154
    }
155
}
156