Completed
Push — master ( 0ea243...da58d4 )
by Henry
10:25 queued 33s
created

ContentAbstract::publishByDate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 0
cts 0
cp 0
rs 9.9
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 2
1
<?php
2
namespace Redaxscript\Model;
3
4
/**
5
 * abstract class to create a model class
6
 *
7
 * @since 4.0.0
8
 *
9
 * @package Redaxscript
10
 * @category Model
11
 * @author Henry Ruhs
12
 */
13
14
abstract class ContentAbstract extends ModelAbstract
15
{
16
	/**
17
	 * get all by order
18
	 *
19
	 * @since 4.0.0
20
	 *
21
	 * @param string $orderColumn name of the column to order
22
	 *
23
	 * @return object|null
24
	 */
25
26
	public function getAllByOrder(string $orderColumn = null) : ?object
27
	{
28
		return $this->query()->orderBySetting($orderColumn)->findMany() ? : null;
29
	}
30
31
	/**
32
	 * get the content by language and order
33
	 *
34
	 * @since 4.0.0
35
	 *
36
	 * @param string $language
37
	 * @param string $orderColumn name of the column to order
38
	 *
39
	 * @return object|null
40
	 */
41
42
	public function getByLanguageAndOrder(string $language = null, string $orderColumn = null) : ?object
43
	{
44
		return $this
45
			->query()
46
			->whereLanguageIs($language)
47
			->where('status', 1)
48
			->orderBySetting($orderColumn)
49
			->findMany() ? : null;
50
	}
51
52
	/**
53
	 * get the content by id and language and order
54
	 *
55
	 * @since 4.0.0
56
	 *
57
	 * @param int $contentId
58
	 * @param string $language
59
	 * @param string $orderColumn name of the column to order
60
	 *
61
	 * @return object|null
62
	 */
63
64
	public function getByIdAndLanguageAndOrder(int $contentId = null, string $language = null, string $orderColumn = null) : ?object
65
	{
66
		return $this
67
			->query()
68
			->whereIn('id', $this->getIdArrayBySibling($contentId))
69
			->whereLanguageIs($language)
70
			->where('status', 1)
71
			->orderBySetting($orderColumn)
72
			->findMany() ? : null;
73
	}
74
75
	/**
76
	 * get id array by sibling
77
	 *
78
	 * @param int $siblingId identifier of the content
79
	 *
80
	 * @return array|null
81
	 */
82
83
	public function getIdArrayBySibling(int $siblingId = null) : ?array
84
	{
85
		return $this
86
			->query()
87
			->select('id')
88
			->whereAnyIs(
89
			[
90
				[
91
					'id' => $siblingId
92
				],
93
				[
94
					'sibling' => $siblingId
95
				]
96
			])
97
			->findFlatArray();
98
	}
99
100
	/**
101
	 * publish by date
102
	 *
103
	 * @since 3.3.0
104
	 *
105
	 * @param string $date
106
	 *
107
	 * @return int
108
	 */
109
110
	public function publishByDate(string $date = null) : int
111
	{
112
		return $this
113
			->query()
114
			->whereLt('date', $date)
115
			->where('status', 2)
116
			->findMany()
117
			->set('status', 1)
118
			->save()
119
			->count();
120
	}
121
}
122