Completed
Push — master ( c5384d...c1ead9 )
by Nazar
04:21
created

Content::clean_cache()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
/**
3
 * @package   Content
4
 * @category  modules
5
 * @author    Nazar Mokrynskyi <[email protected]>
6
 * @copyright Copyright (c) 2014-2016, Nazar Mokrynskyi
7
 * @license   MIT License, see license.txt
8
 */
9
namespace cs\modules\Content;
10
11
use
12
	cs\Cache\Prefix,
13
	cs\Config,
14
	cs\Language,
15
	cs\CRUD_helpers,
16
	cs\Singleton;
17
18
/**
19
 * @method static $this instance($check = false)
20
 */
21
class Content {
22
	use
23
		CRUD_helpers,
24
		Singleton;
25
26
	protected $data_model                  = [
27
		'key'     => 'text',
28
		'title'   => 'ml:text',
29
		'content' => 'ml:',
30
		'type'    => 'set:text,html'
31
	];
32
	protected $table                       = '[prefix]content';
33
	protected $data_model_ml_group         = 'Content';
34
	protected $data_model_files_tag_prefix = 'Content';
35
	/**
36
	 * @var Prefix
37
	 */
38
	protected $cache;
39
40
	protected function construct () {
41
		$this->cache = new Prefix('Content');
42
	}
43
	/**
44
	 * @inheritdoc
45
	 */
46
	protected function cdb () {
47
		return Config::instance()->module('Content')->db('content');
48
	}
49
	/**
50
	 * Add new content
51
	 *
52
	 * @param string $key     Key associated with content, works like id
53
	 * @param string $title   Content title
54
	 * @param string $content Content itself
55
	 * @param string $type    Type of content: <b>text</b> or <b>html</b>. Influences on editor type
56
	 *
57
	 * @return bool
58
	 */
59
	function add ($key, $title, $content, $type) {
60
		$key    = str_replace(['/', '?', '#', '"', '<', '>'], '_', $key);
61
		$result = $this->create($key, $title, $content, $type);
62
		if ($result) {
63
			$this->clean_cache($key);
64
		}
65
		return (bool)$result;
66
	}
67
	/**
68
	 * @param string $key
69
	 */
70
	protected function clean_cache ($key) {
71
		$this->cache->del("$key/".Language::instance()->clang);
72
	}
73
	/**
74
	 * Get content
75
	 *
76
	 * @param string|string[] $key
77
	 *
78
	 * @return false|mixed
79
	 */
80
	function get ($key) {
81
		if (is_array($key)) {
82
			foreach ($key as &$k) {
83
				$k = $this->get($k);
84
			}
85
			return $key;
86
		}
87
		$key = str_replace(['/', '?', '#', '"', '<', '>'], '_', $key);
88
		return $this->cache->get(
89
			"$key/".Language::instance()->clang,
90
			function () use ($key) {
91
				return $this->read($key);
92
			}
93
		);
94
	}
95
	/**
96
	 * Get keys of all content items
97
	 *
98
	 * @return int[]|false
1 ignored issue
show
Documentation introduced by
Should the return type not be false|integer|integer[]|string[]?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
99
	 */
100
	function get_all () {
101
		return $this->search([], 1, PHP_INT_MAX, 'key', true);
102
	}
103
	/**
104
	 * Set content
105
	 *
106
	 * @param string $key     Key associated with content, works like id
107
	 * @param string $title   Content title
108
	 * @param string $content Content itself
109
	 * @param string $type    Type of content: <b>text</b> or <b>html</b>. Influences on editor type
110
	 *
111
	 * @return bool
112
	 */
113
	function set ($key, $title, $content, $type) {
114
		$result = $this->update($key, $title, $content, $type);
115
		if ($result) {
116
			$this->clean_cache($key);
117
		}
118
		return $result;
119
	}
120
	/**
121
	 * Delete content
122
	 *
123
	 * @param string $key
124
	 *
125
	 * @return bool
126
	 */
127
	function del ($key) {
128
		$result = $this->delete($key);
129
		if ($result) {
130
			$this->clean_cache($key);
131
		}
132
		return $result;
133
	}
134
}
135