Completed
Push — master ( 1ce70a...0adf65 )
by Litera
11:25 queued 21s
created

SettingsModel::encodeValue()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace App\Models;
4
5
use App\Models\BaseModel;
6
use Nette\Utils\Json;
7
use Nette\Database\Context;
8
use Tracy\Debugger;
9
use \Exception;
10
11
/**
12
 * Settings
13
 *
14
 * class for handling settings
15
 *
16
 * @created 2012-03-06
17
 * @author Tomas Litera <[email protected]>
18
 */
19
class SettingsModel extends BaseModel
20
{
21
22
	/** @var string */
23
	protected $table = 'kk_settings';
24
25
	/**
26
	 * @param Context $database
27
	 */
28
	public function __construct(Context $database)
29
	{
30
		$this->setDatabase($database);
31
	}
32
33
	/**
34
	 * @return	Nette\Database\Table\ActiveRow
35
	 */
36
	public function allOrFail()
37
	{
38
		$data = $this->all();
39
40
		if(!$data) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $data of type Nette\Database\IRow[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
41
			throw new Exception('Settings: no data found!');
42
		} else {
43
			return $data;
44
		}
45
	}
46
47
	/**
48
	 * Modify mail subject and message
49
	 *
50
	 * @param	string 	$subject 	e-mail subject
51
	 * @param	string	$message 	e-mail message
52
	 * @return	string 				error code
53
	 */
54
	public function modifyMailJSON($type, $subject, $message)
55
	{
56
		$mailData = [
57
			'subject' => $subject,
58
			'message' => $message
59
		];
60
61
		$result = $this->updateByName($mailData, 'mail_' . $type);
62
63
		if(!$result) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $result of type integer|null is loosely compared to false; 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...
64
			throw new Exception('Mail modification failed!');
65
		} else {
66
			return $result;
67
		}
68
	}
69
70
	/**
71
	 * @param  string $type
72
	 * @return object
73
	 */
74
	public function getMailJSON($type)
75
	{
76
		return $this->findByName('mail_' . $type);
77
	}
78
79
	/**
80
	 * @return ActiveRow
81
	 */
82
	public function all()
83
	{
84
		return $this->getDatabase()
85
			->table($this->getTable())
86
			->order('name')
87
			->fetchAll();
88
	}
89
90
	/**
91
	 * @param  string $name
92
	 * @return ActiveRow
93
	 */
94
	public function findByName($name)
95
	{
96
		$result = $this->getDatabase()
97
			->table($this->getTable())
98
			->where('name', $name)
99
			->fetch();
100
101
		return Json::decode($result->value);
102
	}
103
104
	/**
105
	 * @param  mixed  $value
106
	 * @param  string $name
107
	 * @return ActiveRow
108
	 */
109
	public function updateByName($value, $name)
110
	{
111
		return $this->getDatabase()
112
			->table($this->getTable())
113
			->where('name', $name)
114
			->update($this->encodeValue($value));
115
	}
116
117
	/**
118
	 * @param  mixed $data
119
	 * @return Row
120
	 */
121
	public function updateDebugRegime($data)
122
	{
123
		return $this->updateByName($data, 'debug');
124
	}
125
126
	/**
127
	 * @return bool
128
	 */
129
	public function findDebugRegime()
130
	{
131
		return (bool) $this->findByName('debug');
132
	}
133
134
	/**
135
	 * @param  mixed $value
136
	 * @return array
137
	 */
138
	protected function encodeValue($value): array
139
	{
140
		return [
141
			'value' => Json::encode($value)
142
		];
143
	}
144
145
}
146