Ajde_Resource_Local::getAction()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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