Completed
Push — master ( 16047b...229a37 )
by
02:54
created

PageBuilder::getSetting()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 10
rs 9.4285
cc 2
eloc 5
nc 2
nop 1
1
<?php
2
3
namespace DoS\ResourceBundle\Templating;
4
5
use Sonata\SeoBundle\Seo\SeoPageInterface;
6
use Sylius\Bundle\SettingsBundle\Templating\Helper\SettingsHelper;
7
use Symfony\Component\OptionsResolver\OptionsResolver;
8
9
class PageBuilder
10
{
11
    /**
12
     * @var SettingsHelper
13
     */
14
    protected $settingsHelper;
15
16
    /**
17
     * @var array
18
     */
19
    protected $options = array();
20
21
    /**
22
     * @var SeoPageInterface
23
     */
24
    protected $seoPage;
25
26
    //private $theme = null;
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
27
28
    public function __construct(SettingsHelper $settingsHelper, $defaultOptions = array())
29
    {
30
        $this->settingsHelper = $settingsHelper;
31
        $this->options = $defaultOptions;
32
    }
33
34
    /**
35
     * @param SeoPageInterface|null $seoPage
36
     */
37
    public function setSeoPage($seoPage = null)
38
    {
39
        $this->seoPage = $seoPage;
40
    }
41
42
    /**
43
     * Set page options.
44
     *
45
     * @param array $options
46
     */
47
    public function setOptions(array $options = array())
48
    {
49
        $options['inited'] = true;
50
51
        // Resolve merged options
52
        $resolver = new OptionsResolver();
53
        $this->configureOptions($resolver);
54
        $options = $resolver->resolve($options);
55
        $this->options = array_merge($this->options, $options);
56
57
        if (empty($this->options['blocks']['header'])) {
58
            $this->options['header'] = false;
59
        }
60
61
        if (!$this->options['header']) {
62
            $this->options['css'] = trim($this->options['css'].' no page header');
63
        }
64
65
        if (isset($options['keywords'])) {
66
            $this->options['metas']['keywords'] = $options['keywords'];
67
        }
68
69
        if (isset($options['description'])) {
70
            $this->options['metas']['description'] = $options['description'];
71
        }
72
73
        if ($seo = $this->seoPage) {
74
            $seo->setTitle($this->options['title']);
75
            $seo->addHtmlAttributes('lang', $this->options['locale']);
76
77
            if ($this->options['canonical']) {
78
                $seo->setLinkCanonical($this->options['canonical']);
79
            }
80
81
            foreach ($this->options['metas'] as $key => $value) {
82
                if (is_array($value)) {
83
                    foreach ($value as $k => $v) {
84
                        $seo->addMeta('property', $k, $v);
85
                    }
86
                } else {
87
                    $seo->addMeta('name', $key, $value);
88
                }
89
            }
90
        }
91
    }
92
93
    /**
94
     * @param $name
95
     * @param null $default
96
     *
97
     * @return null|string
98
     */
99
    public function getOption($name, $default = null)
100
    {
101
        return $this->getOptionValue($name, $this->options) ?: $default;
102
    }
103
104
    /**
105
     * @param string     $option
106
     * @param string     $printOut
107
     * @param null|mixed $defaultOptionValue
108
     *
109
     * @return string
110
     */
111
    public function get($option, $printOut = '%s', $defaultOptionValue = null)
112
    {
113
        if ($value = $this->getOption($option, $defaultOptionValue)) {
114
            if (is_array($value) || is_object($value)) {
115
                return $value;
116
            }
117
118
            return sprintf($printOut, $value);
119
        }
120
121
        return;
122
    }
123
124
    /**
125
     * @param string $steps
126
     * @param array  $options
127
     *
128
     * @return null|string
129
     */
130
    private function getOptionValue($steps, array $options)
131
    {
132
        $value = $options;
133
        $steps = explode('.', $steps);
134
135
        foreach ($steps as $step) {
136
            if (!array_key_exists($step, $value)) {
137
                return;
138
            }
139
140
            $value = $value[$step];
141
        }
142
143
        return $value;
144
    }
145
146
    /**
147
     * @param string $key
148
     *
149
     * @return null|mixed
150
     */
151
    private function getSetting($key)
152
    {
153
        $settings = $this->settingsHelper->getSettings('general');
154
155
        if (array_key_exists($key, $settings)) {
156
            return $settings[$key];
157
        }
158
159
        return null;
160
    }
161
162
    /**
163
     * Configure the option resolver.
164
     *
165
     * @param OptionsResolver $resolver
166
     */
167
    protected function configureOptions(OptionsResolver $resolver)
168
    {
169
        $resolver->setRequired(array());
170
171
        $resolver->setDefaults(array(
172
            'id' => null,
173
            'css' => null,
174
            'charset' => 'UTF-8',
175
            'domain' => $title = $this->getSetting('domain'),
176
            'title' => $title = $this->getSetting('title'),
177
            'locale' => $this->getSetting('locale'),
178
            'metas' => array(
179
                'keywords' => $keywords = $this->getSetting('meta_keywords'),
180
                'description' => $description = $this->getSetting('meta_description'),
181
                'robots' => $this->getSetting('meta_robots'),
182
            ),
183
            'blocks' => null,
184
            'styles' => null,
185
            'scripts' => null,
186
            'keywords' => $keywords,
187
            'description' => $description,
188
            'breadcrumbs' => null,
189
            'reset_script' => false,
190
            'reset_style' => false,
191
            'reset_meta' => false,
192
            'reset_breadcrumb' => false,
193
            'heading' => null,
194
            'icon' => null,
195
            'canonical' => null,
196
            'inited' => false,
197
            'extras' => array(),
198
        ));
199
200
        $resolver->setAllowedTypes(array(
0 ignored issues
show
Documentation introduced by
array('inited' => array(...array('null', 'array')) is of type array<string,array<integ...",\"1\":\"string\"}>"}>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
201
            'inited' => array('boolean'),
202
            'heading' => array('null', 'string'),
203
            'icon' => array('null', 'string'),
204
            'metas' => array('null', 'array'),
205
            'blocks' => array('null', 'array'),
206
            'extras' => array('null', 'array'),
207
        ));
208
    }
209
}
210