1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Charcoal\Cms\Service\Loader; |
4
|
|
|
|
5
|
|
|
// dependencies from `charcoal-core` |
6
|
|
|
use Charcoal\Loader\CollectionLoader; |
7
|
|
|
|
8
|
|
|
// PHP dependencies |
9
|
|
|
use DateTime; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* News Loader |
13
|
|
|
*/ |
14
|
|
|
class NewsLoader extends AbstractLoader |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var string $median The median between upcoming and archive entries. |
18
|
|
|
*/ |
19
|
|
|
protected $median; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var object $objType The object to load. |
23
|
|
|
*/ |
24
|
|
|
protected $objType; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @return CollectionLoader |
28
|
|
|
*/ |
29
|
|
View Code Duplication |
public function all() |
|
|
|
|
30
|
|
|
{ |
31
|
|
|
$proto = $this->newsProto(); |
32
|
|
|
$loader = $this->collectionLoader()->setModel($proto); |
33
|
|
|
$loader->addFilter('active', true); |
34
|
|
|
|
35
|
|
|
return $loader; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function newsProto() |
39
|
|
|
{ |
40
|
|
|
return $this->modelFactory()->get($this->objType()); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @return CollectionLoader |
45
|
|
|
*/ |
46
|
|
View Code Duplication |
public function published() |
|
|
|
|
47
|
|
|
{ |
48
|
|
|
$now = new DateTime(); |
49
|
|
|
$loader = $this->all(); |
50
|
|
|
$loader->addFilter('publish_date', $now->format('Y-m-d H:i:s'), [ 'operator' => '<=' ]) |
51
|
|
|
->addFilter('expiry_date', $now->format('Y-m-d H:i:s'), [ 'operator' => '>=' ]); |
52
|
|
|
|
53
|
|
|
return $loader; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @return CollectionLoader |
58
|
|
|
*/ |
59
|
|
View Code Duplication |
public function expired() |
|
|
|
|
60
|
|
|
{ |
61
|
|
|
$now = new DateTime(); |
62
|
|
|
$loader = $this->all(); |
63
|
|
|
$loader->addFilter('publish_date', $now->format('Y-m-d H:i:s'), [ 'operator' => '<=' ]) |
64
|
|
|
->addFilter('expiry_date', $now->format('Y-m-d H:i:s'), [ 'operator' => '<=' ]); |
65
|
|
|
|
66
|
|
|
return $loader; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Fetch upcoming entries based on the median or now. |
71
|
|
|
* @return CollectionLoader |
72
|
|
|
*/ |
73
|
|
|
public function upcoming() |
74
|
|
|
{ |
75
|
|
|
$loader = $this->published(); |
76
|
|
|
|
77
|
|
|
return $loader; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Fetch upcoming entries based on the median or now. |
82
|
|
|
* @return CollectionLoader |
83
|
|
|
*/ |
84
|
|
|
public function archive() |
85
|
|
|
{ |
86
|
|
|
$loader = $this->expired(); |
87
|
|
|
|
88
|
|
|
return $loader; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
// ========================================================================== |
92
|
|
|
// GETTERS |
93
|
|
|
// ========================================================================== |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @return mixed |
97
|
|
|
*/ |
98
|
|
|
public function median() |
99
|
|
|
{ |
100
|
|
|
return $this->median; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @return object |
105
|
|
|
*/ |
106
|
|
|
public function objType() |
107
|
|
|
{ |
108
|
|
|
return $this->objType; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
// ========================================================================== |
112
|
|
|
// SETTERS |
113
|
|
|
// ========================================================================== |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @param string $median The median between upcoming and archive. |
117
|
|
|
* @return self |
118
|
|
|
*/ |
119
|
|
|
public function setMedian($median) |
120
|
|
|
{ |
121
|
|
|
$this->median = $median; |
122
|
|
|
|
123
|
|
|
return $this; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @param object $objType The object type. |
128
|
|
|
* @return self |
129
|
|
|
*/ |
130
|
|
|
public function setObjType($objType) |
131
|
|
|
{ |
132
|
|
|
$this->objType = $objType; |
133
|
|
|
|
134
|
|
|
return $this; |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|
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.