SizeCriterion   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
wmc 8
lcom 0
cbo 0
dl 0
loc 61
c 0
b 0
f 0
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A alter_conditions() 0 18 3
B alter_query_with_value() 0 29 5
1
<?php
2
3
/*
4
 * This file is part of the Icybee package.
5
 *
6
 * (c) Olivier Laviale <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Icybee\Modules\Files\Facets;
13
14
use ICanBoogie\ActiveRecord\Query;
15
use ICanBoogie\Facets\Criterion\BasicCriterion;
16
17
class SizeCriterion extends BasicCriterion
18
{
19
	/**
20
	 * Adds support for the `size` filter.
21
	 *
22
	 * @inheritdoc
23
	 */
24
	public function alter_conditions(array &$filters, array $modifiers)
25
	{
26
		parent::alter_conditions($filters, $modifiers);
27
28
		if (isset($modifiers['size']))
29
		{
30
			$value = $modifiers['size'];
31
32
			if (in_array($value, [ 'l', 'm', 's' ]))
33
			{
34
				$filters['size'] = $value;
35
			}
36
			else
37
			{
38
				unset($filters['size']);
39
			}
40
		}
41
	}
42
43
	/**
44
	 * Adds support for the `size` filter.
45
	 *
46
	 * @inheritdoc
47
	 */
48
	public function alter_query_with_value(Query $query, $value)
49
	{
50
		if ($value)
51
		{
52
			list($avg, $max, $min) = $query->model
53
				->similar_site
54
				->select('AVG(size), MAX(size), MIN(size)')
55
				->one(\PDO::FETCH_NUM);
56
57
			$bounds = [
58
59
				$min,
60
				round($avg - ($avg - $min) / 3),
61
				round($avg),
62
				round($avg + ($max - $avg) / 3),
63
				$max
64
65
			];
66
67
			switch ($value)
68
			{
69
				case 'l': $query->and('size >= ?', $bounds[3]); break;
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
Coding Style introduced by
Terminating statement must be on a line by itself

As per the PSR-2 coding standard, the break (or other terminating) statement must be on a line of its own.

switch ($expr) {
     case "A":
         doSomething();
         break; //wrong
     case "B":
         doSomething();
         break; //right
     case "C:":
         doSomething();
         return true; //right
 }

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
70
				case 'm': $query->and('size >= ? AND size < ?', $bounds[2], $bounds[3]); break;
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
Coding Style introduced by
Terminating statement must be on a line by itself

As per the PSR-2 coding standard, the break (or other terminating) statement must be on a line of its own.

switch ($expr) {
     case "A":
         doSomething();
         break; //wrong
     case "B":
         doSomething();
         break; //right
     case "C:":
         doSomething();
         return true; //right
 }

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
71
				case 's': $query->and('size < ?', $bounds[2]); break;
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
Coding Style introduced by
Terminating statement must be on a line by itself

As per the PSR-2 coding standard, the break (or other terminating) statement must be on a line of its own.

switch ($expr) {
     case "A":
         doSomething();
         break; //wrong
     case "B":
         doSomething();
         break; //right
     case "C:":
         doSomething();
         return true; //right
 }

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
72
			}
73
		}
74
75
		return $query;
76
	}
77
}
78