Ajde_Component_Js::requireJsLibrary()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 7
nc 2
nop 2
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
class Ajde_Component_Js extends Ajde_Component_Resource
4
{
5
    public static function processStatic(Ajde_Template_Parser $parser, $attributes)
6
    {
7
        $instance = new self($parser, $attributes);
8
9
        return $instance->process();
10
    }
11
12
    public function process()
13
    {
14
        //		TODO: check for required attributes
15
        //		if (!array_key_exists('library', $this->attributes) || !array_key_exists('version', $this->attributes)) {
16
        //			throw new Ajde_Component_Exception();
17
        //		}
18
        if (array_key_exists('library', $this->attributes)) {
19
            $this->requireJsLibrary($this->attributes['library'], $this->attributes['version']);
20
        } elseif (array_key_exists('action', $this->attributes)) {
21
            $this->requireResource(
22
                Ajde_Resource_Local::TYPE_JAVASCRIPT,
23
                $this->attributes['action'],
24
                issetor($this->attributes['format'], null),
25
                issetor($this->attributes['base'], null),
26
                issetor($this->attributes['position'], null),
27
                issetor($this->attributes['arguments'], '')
28
            );
29
        } elseif (array_key_exists('filename', $this->attributes)) {
30
            $this->requirePublicResource(
31
                Ajde_Resource_Local::TYPE_JAVASCRIPT,
32
                $this->attributes['filename'],
33
                issetor($this->attributes['position'], null),
34
                issetor($this->attributes['arguments'], '')
35
            );
36
        } elseif (array_key_exists('url', $this->attributes)) {
37
            $this->requireRemoteResource(
38
                Ajde_Resource_Local::TYPE_JAVASCRIPT,
39
                $this->attributes['url'],
40
                issetor($this->attributes['position'], null),
41
                issetor($this->attributes['arguments'], '')
42
            );
43
        }
44
    }
45
46
    public function requireJsLibrary($library, $version = false)
47
    {
48
        if ($version === false) {
49
            $url = Ajde_Resource_JsLibrary::getCdnJsUrl($library);
50
        } else {
51
            $url = Ajde_Resource_JsLibrary::getUrl($library, $version);
52
        }
53
54
        $resource = new Ajde_Resource_Remote(Ajde_Resource::TYPE_JAVASCRIPT, $url);
55
        $this->getParser()->getDocument()->addResource($resource, Ajde_Document_Format_Html::RESOURCE_POSITION_TOP);
0 ignored issues
show
Documentation Bug introduced by
The method getDocument does not exist on object<Ajde_Template_Parser>? 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...
56
    }
57
}
58