Completed
Push — master ( 734eba...03e6ad )
by
unknown
02:56
created

AbstractSearchHistoryWidget::searchHistory()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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