Completed
Pull Request — master (#357)
by Paul
08:47 queued 02:41
created

WidgetHelper::getCacheTimeout()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 15
rs 9.2
cc 4
eloc 9
nc 4
nop 1
1
<?php
2
3
namespace Victoire\Bundle\WidgetBundle\Helper;
4
5
use Doctrine\Common\Util\ClassUtils;
6
use Symfony\Component\DependencyInjection\Container;
7
use Victoire\Bundle\WidgetBundle\Entity\Widget;
8
9
class WidgetHelper
10
{
11
    private $container;
12
13
    public function __construct(Container $container)
14
    {
15
        $this->container = $container;
16
    }
17
18
    /**
19
     * The name of the widget.
20
     *
21
     * @return string
22
     */
23
    public function getWidgetName(Widget $widget)
24
    {
25
        $widgets = $this->container->getParameter('victoire_core.widgets');
26
        foreach ($widgets as $widgetParams) {
27
            if ($widgetParams['class'] === ClassUtils::getClass($widget)) {
28
                return $widgetParams['name'];
29
            }
30
        }
31
32
        throw new \Exception('Widget name not found for widget '.get_class($widget).'. Is this widget right declared in AppKernel ?');
33
    }
34
35
    /**
36
     * check if widget is allowed for slot.
37
     *
38
     * @param Widget $widget
39
     * @param string $slot
40
     *
41
     * @return bool
42
     */
43
    public function isWidgetAllowedForSlot(Widget $widget, $slot)
44
    {
45
        $widgetName = $this->getWidgetName($widget);
46
        $slots = $this->slots;
0 ignored issues
show
Bug introduced by
The property slots does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
47
48
        return !empty($slots[$slot]) && (array_key_exists($widgetName, $slots[$slot]['widgets']));
49
    }
50
51
    /**
52
     * create a new WidgetRedactor.
53
     *
54
     * @param string                                  $type
55
     * @param \Victoire\Bundle\CoreBundle\Entity\View $view
56
     * @param string                                  $slot
57
     * @param string                                  $mode
58
     *
59
     * @return Widget $widget
60
     */
61
    public function newWidgetInstance($type, $view, $slot, $mode)
0 ignored issues
show
Unused Code introduced by
The parameter $view 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...
Unused Code introduced by
The parameter $slot 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...
62
    {
63
        $widgetAlias = 'victoire.widget.'.strtolower($type);
64
        $widget = $this->container->get($widgetAlias);
65
66
        $widget->setMode($mode);
67
68
        return $widget;
69
    }
70
71
    /**
72
     * Get the name of the template to display for an action.
73
     *
74
     * @param string $action
75
     * @param Widget $widget
76
     *
77
     * @todo find a better way to get the requested template
78
     *
79
     * @return string
80
     */
81
    public function getTemplateName($action, Widget $widget)
82
    {
83
        //the template displayed is in the widget bundle
84
        $templateName = 'VictoireWidget'.$this->getWidgetName($widget).'Bundle::'.$action.'.html.twig';
85
86
        return $templateName;
87
    }
88
89
    /**
90
     * Delete manually a widget with its id.
91
     *
92
     * @param int $widgetId
93
     *
94
     * @return string
95
     */
96
    public function deleteById($widgetId)
97
    {
98
        $entityManager = $this->container->get('doctrine.orm.entity_manager');
99
        $connection = $entityManager->getConnection();
100
        $statement = $connection->prepare('DELETE FROM vic_widget WHERE id = :id');
101
        $statement->bindValue('id', $widgetId);
102
        $statement->execute();
103
    }
104
105
    /**
106
     * Check in the driver chain if the given widget is enabled.
107
     *
108
     * @param Widget $widget
109
     *
110
     * @return bool
111
     */
112
    public function isEnabled(Widget $widget)
113
    {
114
        $widgets = $this->container->getParameter('victoire_core.widgets');
115
        foreach ($widgets as $widgetParams) {
116
            if ($widgetParams['class'] === ClassUtils::getClass($widget)) {
117
                return true;
118
            }
119
        }
120
121
        return false;
122
    }
123
    /**
124
     * Check in the driver chain if the given widget is enabled.
125
     *
126
     * @param Widget $widget
127
     *
128
     * @return bool
129
     */
130
    public function isCacheEnabled(Widget $widget)
131
    {
132
        $widgets = $this->container->getParameter('victoire_core.widgets');
133
        foreach ($widgets as $widgetParams) {
134
            if ($widgetParams['class'] === ClassUtils::getClass($widget)) {
135
                if (array_key_exists('cache', $widgetParams)) {
136
                    return $widgetParams['cache'];
137
                } else {
138
                    return true;
139
                }
140
            }
141
        }
142
143
        throw new \Exception('Widget config not found for widget '.ClassUtils::getClass($widget).'. Is this widget right declared in AppKernel ?');
144
    }
145
146
    /**
147
     * Check in the driver chain if the given widget is enabled.
148
     *
149
     * @param Widget $widget
150
     *
151
     * @return bool
152
     */
153
    public function getCacheTimeout(Widget $widget)
154
    {
155
        $widgets = $this->container->getParameter('victoire_core.widgets');
156
        foreach ($widgets as $widgetParams) {
157
            if ($widgetParams['class'] === ClassUtils::getClass($widget)) {
158
                if (array_key_exists('cache_timout', $widgetParams)) {
159
                    return $widgetParams['cache_timout'];
160
                } else {
161
                    return 7 * 24 * 60 * 1000; // one week by default
0 ignored issues
show
Bug Best Practice introduced by
The return type of return 7 * 24 * 60 * 1000; (integer) is incompatible with the return type documented by Victoire\Bundle\WidgetBu...Helper::getCacheTimeout of type boolean.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
162
                }
163
            }
164
        }
165
166
        throw new \Exception('Widget config not found for widget '.ClassUtils::getClass($widget).'. Is this widget right declared in AppKernel ?');
167
    }
168
}
169