CategoryModel::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 2
dl 0
loc 5
ccs 3
cts 3
cp 1
crap 1
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace App\Models;
4
5
use Tracy\Debugger;
6
use Nette\Utils\Strings;
7
use Nette\Database\Context;
8
use Nette\Caching\Cache;
9
10
/**
11
 * Category
12
 *
13
 * class for handling category and category styles
14
 *
15
 * @created 2012-03-06
16
 * @author Tomas Litera <[email protected]>
17
 */
18 1
class CategoryModel extends BaseModel
19
{
20
	/** @var array */
21
	public $dbColumns = [
22
		'name',
23
		'bgcolor',
24
		'bocolor',
25
		'focolor',
26
	];
27
28
	/** @var string */
29
	protected $table = 'kk_categories';
30
31
	/**
32
	 * @param Nette\Database\Context $database
33
	 */
34
	public function __construct(Context $database, Cache $cache)
35
	{
36 1
		$this->setDatabase($database);
37 1
		$this->setCache($cache);
38 1
	}
39
40
	/**
41
	 * @param	array  $data
42
	 * @return	boolean
43
	 */
44
	public function create(array $data)
45
	{
46
		$data['style'] = $this->getStyleFromName($data['name']);
47
48
		return parent::create($data);
49
	}
50
51
	/**
52
	 * @param	integer $id
53
	 * @param	array  $data
54
	 * @return	boolean
55
	 */
56
	public function update($id, array $data)
57
	{
58
		$data['style'] = $this->getStyleFromName($data['name']);
59
60
		return parent::update($id, $data);
61
	}
62
63
	/**
64
	 * @param  integer $id
65
	 * @return Nette\Database\Table\ActiveRow
66
	 */
67
	public function find($id)
68
	{
69
		return $this->getDatabase()
70
			->table($this->getTable())
71
			->where('id', $id)
72
			->limit(1)
73
			->fetch();
74
	}
75
76
	/**
77
	 * @return array
78
	 */
79
	public function all(): array
80
	{
81
		return $this->getDatabase()
82
			->table($this->getTable())
83
			->where('deleted', '0')
84
			->order('name')
85
			->fetchAll();
86
	}
87
88
	/**
89
	 * @param  string $name
90
	 * @return string
91
	 */
92
	protected function getStyleFromName($name)
93
	{
94 1
		$style = Strings::toAscii($name);
95 1
		$style = str_replace(" ", "_", $style);
96
97 1
		return $style;
98
	}
99
100
}
101