DateRange   A
last analyzed

Complexity

Total Complexity 22

Size/Duplication

Total Lines 106
Duplicated Lines 5.66 %

Coupling/Cohesion

Components 0
Dependencies 5

Importance

Changes 0
Metric Value
wmc 22
lcom 0
cbo 5
dl 6
loc 106
c 0
b 0
f 0
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A accept() 0 4 1
A handle() 0 15 1
A createFacetResult() 0 30 4
F convertDateInterval() 6 29 16

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Kaliop\EzFindSearchEngineBundle\Core\Persistence\eZFind\Content\Search\Common\Gateway\FacetHandler;
4
5
use eZ\Publish\API\Repository\Values\Content\Query\FacetBuilder;
6
use eZ\Publish\API\Repository\Values\Content\Search\Facet\DateRangeFacet;
7
use eZ\Publish\API\Repository\Values\Content\Search\Facet\RangeFacetEntry;
8
use Kaliop\EzFindSearchEngineBundle\API\Repository\Values\Content\Query\FacetBuilder\DateRangeFacetBuilder;
9
use Kaliop\EzFindSearchEngineBundle\Core\Persistence\eZFind\Content\Search\Common\Gateway\FacetHandler;
10
use DateInterval;
11
use DateTime;
12
13
class DateRange extends FacetHandler
14
{
15
    // SOLR does not support TimeZones for DateTimes
16
    const DATE_FORMAT = 'Y-m-d\TH:i:s\Z';
17
18
    /**
19
     * @inheritdoc
20
     */
21
    public function accept(FacetBuilder $facetBuilder)
22
    {
23
        return $facetBuilder instanceof DateRangeFacetBuilder;
24
    }
25
26
    /**
27
     * @inheritdoc
28
     *
29
     * @param DateRangeFacetBuilder $facetBuilder
30
     */
31
    public function handle(FacetBuilder $facetBuilder)
32
    {
33
        $facet = $this->buildFacet($facetBuilder);
34
        $facet['range'] = [
35
            'field' => $facetBuilder->fieldPath,
36
            'start' => $facetBuilder->start->format(self::DATE_FORMAT),
37
            'end' => $facetBuilder->end->format(self::DATE_FORMAT),
38
            'gap' => $this->convertDateInterval($facetBuilder->gap),
39
            'hardend' => $facetBuilder->hardend,
40
            'include' => $facetBuilder->include,
41
            'other' => $facetBuilder->other,
42
        ];
43
44
        return $facet;
45
    }
46
47
    /**
48
     * @inheritdoc
49
     *
50
     * @param DateRangeFacetBuilder $facetBuilder
51
     */
52
    public function createFacetResult(FacetBuilder $facetBuilder, $fields = [], $queries = [], $dates = [], $ranges = [])
53
    {
54
        $facetKey = $this->getFacetKey($facetBuilder);
55
        $fieldName = \eZSolr::getFieldName($facetBuilder->fieldPath, false, 'facet');
56
57
        $entries = [];
58
        foreach ($ranges as $field => $range) {
59
            if (in_array($field, [$facetKey, $fieldName])) {
60
                foreach ($range['counts'] as $date => $count) {
61
                    $from = new DateTime($date);
62
                    $to = clone $from;
63
                    $to->add($facetBuilder->gap);
64
65
                    $facetEntry = new RangeFacetEntry();
66
                    $facetEntry->from = $from;
67
                    $facetEntry->to = $to;
68
                    $facetEntry->totalCount = $count;
69
70
                    $entries[] = $facetEntry;
71
                }
72
73
                break;
74
            }
75
        }
76
77
        return new DateRangeFacet([
78
            'name' => $facetBuilder->name,
79
            'entries' => $entries,
80
        ]);
81
    }
82
83
    /**
84
     * Convert PHP DateInterval into JAVA DateMathParser syntax.
85
     *
86
     * @param DateInterval $dateInterval
87
     * @return string
88
     */
89
    protected function convertDateInterval(DateInterval $dateInterval)
90
    {
91
        $sign = $dateInterval->invert? '-' : '+';
92 View Code Duplication
        if ($dateInterval->days) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $dateInterval->days of type integer|false is loosely compared to true; this is ambiguous if the integer can be zero. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
93
            return $sign . $dateInterval->days . 'DAY' . (($dateInterval->y > 1)? 'S' : '');
94
        }
95
96
        $syntax = '';
97 View Code Duplication
        if ($dateInterval->y) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
98
            $syntax .= $sign . $dateInterval->y . 'YEAR' . (($dateInterval->y > 1)? 'S' : '');
99
        }
100
        if ($dateInterval->m) {
101
            $syntax .= $sign . $dateInterval->m . 'MONTH' . (($dateInterval->m > 1)? 'S' : '');
102
        }
103
        if ($dateInterval->d) {
104
            $syntax .= $sign . $dateInterval->d . 'DAY' . (($dateInterval->d > 1)? 'S' : '');
105
        }
106
        if ($dateInterval->h) {
107
            $syntax .= $sign . $dateInterval->h . 'HOUR' . (($dateInterval->h > 1)? 'S' : '');
108
        }
109
        if ($dateInterval->i) {
110
            $syntax .= $sign . $dateInterval->i . 'MINUTE' . (($dateInterval->i > 1)? 'S' : '');
111
        }
112
        if ($dateInterval->s) {
113
            $syntax .= $sign . $dateInterval->s . 'SECOND' . (($dateInterval->s > 1)? 'S' : '');
114
        }
115
116
        return $syntax;
117
    }
118
}