Completed
Push — master ( e6adfa...acb755 )
by Nekrasov
02:15
created

WidgetFactory   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 19
Bugs 2 Features 0
Metric Value
wmc 12
c 19
b 2
f 0
lcom 1
cbo 4
dl 0
loc 73
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A run() 0 14 2
A getReloadTimeout() 0 4 3
A getCacheTime() 0 4 3
A getContent() 0 6 2
A getContentFromCache() 0 10 2
1
<?php
2
3
namespace Arrilot\Widgets\Factories;
4
5
class WidgetFactory extends AbstractWidgetFactory
6
{
7
    /**
8
     * Run widget without magic method.
9
     *
10
     * @return mixed
11
     */
12
    public function run()
13
    {
14
        $args = func_get_args();
15
        $this->instantiateWidget($args);
16
17
        $content = $this->getContentFromCache($args);
18
19
        if ($timeout = (float) $this->getReloadTimeout()) {
20
            $content .= $this->javascriptFactory->getReloader($timeout);
21
            $content = $this->wrapContentInContainer($content);
22
        }
23
24
        return $this->convertToViewExpression($content);
25
    }
26
27
    /**
28
     * Get widget reload timeout or false if it's not reloadable.
29
     *
30
     * @return bool|float|int
31
     */
32
    protected function getReloadTimeout()
33
    {
34
        return isset($this->widget) && $this->widget->reloadTimeout ? $this->widget->reloadTimeout : false;
35
    }
36
37
    /**
38
     * Get widget cache time or false if it's not meant to be cached.
39
     *
40
     * @return bool|float|int
41
     */
42
    protected function getCacheTime()
43
    {
44
        return isset($this->widget) && $this->widget->cacheTime ? $this->widget->cacheTime : false;
45
    }
46
47
    /**
48
     * Make call and get return widget content.
49
     *
50
     * @return mixed
51
     */
52
    protected function getContent()
53
    {
54
        $content = $this->app->call([$this->widget, 'run'], $this->widgetParams);
55
56
        return is_object($content) ? $content->__toString() : $content;
57
    }
58
59
    /**
60
     * Gets content from cache if it's turned on.
61
     * Runs widget class otherwise.
62
     *
63
     * @param $args
64
     *
65
     * @return mixed
66
     */
67
    protected function getContentFromCache($args)
68
    {
69
        if ($cacheTime = (float) $this->getCacheTime()) {
70
            return $this->app->cache($this->widget->cacheKey($args), $cacheTime, function () {
71
                return $this->getContent();
72
            });
73
        }
74
75
        return $this->getContent();
76
    }
77
}
78