Completed
Push — master ( 904c19...2c4bed )
by
unknown
10:30
created

AbstractSearchHistoryWidget::setDependencies()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace Charcoal\Admin\Widget\Search;
4
5
use \DateTime;
6
use \DateTimeInterface;
7
use \InvalidArgumentException;
8
9
use \Pimple\Container;
10
11
use \Charcoal\Admin\AdminWidget;
12
13
/**
14
 * A basic search history widget.
15
 */
16
abstract class AbstractSearchHistoryWidget extends AdminWidget
17
{
18
    /**
19
     * The default lower bound to filter searches by.
20
     *
21
     * @const string
22
     */
23
    const DEFAULT_FROM_DATE  = '30 days ago';
24
25
    /**
26
     * The default upper bound to filter searches by.
27
     *
28
     * @const string
29
     */
30
    const DEFAULT_UNTIL_DATE = 'now';
31
32
    /**
33
     * The lower bound (exclusive) for a search's timestamp to filter by.
34
     *
35
     * The default is to filter by 30 days ago (@see self::DEFAULT_FROM_DATE).
36
     *
37
     * @var DateTimeInterface
38
     */
39
    private $startDate;
40
41
    /**
42
     * The upper bound (inclusive) for a search's timestamp to filter by.
43
     *
44
     * The default is to filter by the current time (@see self::DEFAULT_UNTIL_DATE).
45
     *
46
     * @var DateTimeInterface
47
     */
48
    private $endDate;
49
50
    /**
51
     * The latest search requests.
52
     *
53
     * @var SearchLog[]
54
     */
55
    private $searchHistory;
56
57
    /**
58
     * Set the lower bound (exclusive) for a search's timestamp to filter by.
59
     *
60
     * @param  string|DateTimeInterface|null $date The starting date/time.
61
     * @throws InvalidArgumentException If the date/time is invalid.
62
     * @return TopSearchWidget Chainable
63
     */
64 View Code Duplication
    public function setStartDate($date)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
65
    {
66
        if ($date === null || $date === '') {
67
            $this->startDate = new DateTime(self::DEFAULT_FROM_DATE);
68
            return $this;
69
        }
70
71
        if (is_string($date)) {
72
            $date = new DateTime($date);
73
        }
74
75
        if (!$date instanceof DateTimeInterface) {
76
            throw new InvalidArgumentException(
77
                'Invalid "Start Date" value. Must be a date/time string or a DateTimeInterface object.'
78
            );
79
        }
80
81
        $this->startDate = $date;
82
83
        return $this;
84
    }
85
86
    /**
87
     * Retrieve the lower bound (exclusive) for a search's timestamp to filter by.
88
     *
89
     * @return DateTimeInterface
90
     */
91
    public function startDate()
92
    {
93
        if ($this->startDate === null) {
94
            // Force default value.
95
            $this->setStartDate(null);
96
        }
97
98
        return $this->startDate;
99
    }
100
101
    /**
102
     * Set the upper bound (inclusive) for a search's timestamp to filter by.
103
     *
104
     * @param  string|DateTimeInterface|null $date The ending date/time.
105
     * @throws InvalidArgumentException If the date/time is invalid.
106
     * @return TopSearchWidget Chainable
107
     */
108 View Code Duplication
    public function setEndDate($date)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
109
    {
110
        if ($date === null || $date === '') {
111
            $this->endDate = new DateTime(self::DEFAULT_UNTIL_DATE);
112
            return $this;
113
        }
114
115
        if (is_string($date)) {
116
            $date = new DateTime($date);
117
        }
118
119
        if (!($date instanceof DateTimeInterface)) {
120
            throw new InvalidArgumentException(
121
                'Invalid "End Date" value. Must be a date/time string or a DateTimeInterface object.'
122
            );
123
        }
124
125
        $this->endDate = $date;
126
127
        return $this;
128
    }
129
130
    /**
131
     * Retrieve the upper bound (inclusive) for a search's timestamp to filter by.
132
     *
133
     * @return DateTimeInterface
134
     */
135
    public function endDate()
136
    {
137
        if ($this->endDate === null) {
138
            $this->setEndDate(null);
139
        }
140
141
        return $this->endDate;
142
    }
143
144
    /**
145
     * Retrieve the search history.
146
     *
147
     * @return SearchLog[]
148
     */
149
    public function searchHistory()
150
    {
151
        if ($this->searchHistory === null) {
152
            $this->searchHistory = $this->loadSearchHistory();
153
        }
154
155
        return $this->searchHistory;
156
    }
157
158
    /**
159
     * Determine if there's a search history.
160
     *
161
     * @return boolean
162
     */
163
    public function hasSearchHistory()
164
    {
165
        return (count($this->searchHistory()) > 0);
166
    }
167
168
    /**
169
     * Load the search history from the source.
170
     *
171
     * @return SearchLog[]
172
     */
173
    abstract public function loadSearchHistory();
174
}
175