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

WidgetFactory::getContent()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 2
eloc 3
nc 2
nop 0
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