Completed
Push — master ( ee94a5...d975bd )
by Nicolaas
04:02
created

EcommerceSearchHistoryFormField::setShowMoreLink()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
eloc 3
nc 1
nop 1
1
<?php
2
3
class EcommerceSearchHistoryFormField extends LiteralField
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
4
{
5
    /**
6
     * total number days to search back.
7
     *
8
     * @var int
9
     */
10
    protected $numberOfDays = 100;
11
12
    /**
13
     * how many days ago the data-analysis should end.
14
     *
15
     * @var int
16
     */
17
    protected $endingDaysBack = 0;
18
19
    /**
20
     * minimum number of searches for the data to show up.
21
     *
22
     * @var int
23
     */
24
    protected $minimumCount = 30;
25
26
    /**
27
     * maximum number of searches for the data to show up.
28
     *
29
     * @var int
30
     */
31
    protected $maxRows = 20;
32
33
    /**
34
     *
35
     * @var bool
36
     */
37
    protected $addTitle = true;
38
39
    /**
40
     *
41
     * @var bool
42
     */
43
    protected $addAtoZ = true;
44
45
    /**
46
     * minimum number of searches for the data to show up.
47
     *
48
     * @var bool
49
     */
50
    protected $showMoreLink = false;
51
52
    public function __construct($name, $title = '')
53
    {
54
        return parent::__construct($name, $title);
0 ignored issues
show
Bug introduced by
Constructors do not have meaningful return values, anything that is returned from here is discarded. Are you sure this is correct?
Loading history...
55
    }
56
57
    /**
58
     * @param int
59
     *
60
     * @return EcommerceSearchHistoryFormField
61
     */
62
    public function setNumberOfDays($days)
63
    {
64
        $this->numberOfDays = intval($days);
65
66
        return $this;
67
    }
68
69
    /**
70
     * @param int
71
     *
72
     * @return EcommerceSearchHistoryFormField
73
     */
74
    public function setMinimumCount($count)
75
    {
76
        $this->minimumCount = intval($count);
77
78
        return $this;
79
    }
80
81
    /**
82
     * @param int
83
     *
84
     * @return EcommerceSearchHistoryFormField
85
     */
86
    public function setShowMoreLink($b)
87
    {
88
        $this->showMoreLink = $b;
89
90
        return $this;
91
    }
92
93
    /**
94
     * @param int
95
     *
96
     * @return EcommerceSearchHistoryFormField
97
     */
98
    public function setEndingDaysBack($count)
99
    {
100
        $this->endingDaysBack = intval($count);
101
102
        return $this;
103
    }
104
105
    /**
106
     * @param int
107
     *
108
     * @return EcommerceSearchHistoryFormField
109
     */
110
    public function setMaxRows($number)
111
    {
112
        $this->maxRows = $number;
113
114
        return $this;
115
    }
116
117
    /**
118
     * @param bool
119
     *
120
     * @return EcommerceSearchHistoryFormField
121
     */
122
    public function setAddTitle($b)
123
    {
124
        $this->addTitle = $b;
125
126
        return $this;
127
    }
128
129
    /**
130
     * @param bool
131
     *
132
     * @return EcommerceSearchHistoryFormField
133
     */
134
    public function setAddAtoZ($b)
135
    {
136
        $this->addAtoZ = $b;
137
138
        return $this;
139
    }
140
141
142
    public function FieldHolder($properties = array())
143
    {
144
        return $this->Field($properties);
145
    }
146
147
    public function Field($properties = array())
148
    {
149
        $title = $this->getContent();
150
        $totalNumberOfDaysBack = $this->numberOfDays + $this->endingDaysBack;
151
        $minCountSafety = SearchHistory::get()->count() / 500;
152
        if($this->minimumCount < $minCountSafety) {
153
            $this->minimumCount = $minCountSafety;
154
        }
155
        $data = DB::query('
156
            SELECT COUNT(ID) myCount, "Title"
157
            FROM "SearchHistory"
158
            WHERE Created > ( NOW() - INTERVAL '.$totalNumberOfDaysBack.' DAY )
159
                AND Created < ( NOW() - INTERVAL '.$this->endingDaysBack." DAY )
160
            GROUP BY \"Title\"
161
            HAVING COUNT(\"ID\") >= $this->minimumCount
162
            ORDER BY myCount DESC
163
            LIMIT ".$this->maxRows."
164
        ");
165
        if (!$this->minimumCount) {
166
            ++$this->minimumCount;
167
        }
168
        $content = '';
169
        $tableContent = '';
170
        if ($title && $this->addTitle) {
171
            $content .= '<h3>'.$title.'</h3>';
172
        }
173
        $content .= '
174
        <div id="SearchHistoryTableForCMS">
175
            <h3>
176
                Search Phrases'
177
                .($this->minimumCount > 1 ? ', entered at least '.$this->minimumCount.' times' : '')
178
                .($this->maxRows < 1000 ? ', limited to '.$this->maxRows.' entries, ' : '')
179
                .' between '.date('j-M-Y', strtotime('-'.$totalNumberOfDaysBack.' days')).' and '.date('j-M-Y', strtotime('-'.$this->endingDaysBack.' days')).'
180
            </h3>';
181
        $count = 0;
182
        if ($data && count($data)) {
183
            $tableContent .= '
184
                <table class="highToLow" style="widht: 100%">';
185
            $list = array();
186
            foreach ($data as $key => $row) {
187
                $count++;
188
                //for the highest count, we work out a max-width
189
                if (!$key) {
190
                    $maxWidth = $row['myCount'];
191
                }
192
                $multipliedWidthInPercentage = floor(($row['myCount'] / $maxWidth) * 100);
0 ignored issues
show
Bug introduced by
The variable $maxWidth does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
193
                $list[$row['myCount'].'-'.$key] = $row['Title'];
194
                $tableContent .= '
195
                    <tr>
196
                        <td style="text-align: right; width: 30%; padding: 5px;">'.$row['Title'].'</td>
197
                        <td style="background-color: silver;  padding: 5px; width: 70%;">
198
                            <div style="width: '.$multipliedWidthInPercentage.'%; background-color: #C51162; color: #fff;">'.$row['myCount'].'</div>
199
                        </td>
200
                    </tr>';
201
            }
202
            $tableContent .= '
203
                </table>';
204
            if ($count && $this->addAtoZ) {
205
                asort($list);
206
                $tableContent .= '
207
                    <h3>A - Z</h3>
208
                    <table class="aToz" style="widht: 100%">';
209
                foreach ($list as $key => $title) {
210
                    $array = explode('-', $key);
211
                    $multipliedWidthInPercentage = floor(($array[0] / $maxWidth) * 100);
212
                    $tableContent .= '
213
                        <tr>
214
                            <td style="text-align: right; width: 30%; padding: 5px;">'.$title.'</td>
215
                            <td style="background-color: silver;  padding: 5px; width: 70%">
216
                                <div style="width: '.$multipliedWidthInPercentage.'%; background-color: #004D40; color: #fff;">'.trim($array[0]).'</div>
217
                            </td>
218
                        </tr>';
219
                }
220
                $tableContent .= '
221
                    </table>';
222
            }
223
        }
224
        if ($count === 0) {
225
            //we replace table content here...
226
            $tableContent = '<p class="warning message">No searches found.</p>';
227
        }
228
        $content .= $tableContent;
229
        if ($this->showMoreLink) {
230
            $content .= '
231
            <p>
232
                <a href="/dev/tasks/EcommerceTaskReviewSearches/">Query more resuts</a>
233
            </p>';
234
        } else {
0 ignored issues
show
Unused Code introduced by
This else statement is empty and can be removed.

This check looks for the else branches of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These else branches can be removed.

if (rand(1, 6) > 3) {
print "Check failed";
} else {
    //print "Check succeeded";
}

could be turned into

if (rand(1, 6) > 3) {
    print "Check failed";
}

This is much more concise to read.

Loading history...
235
        }
236
        $content .= '
237
        </div>';
238
239
        return $content;
240
    }
241
}
242