Passed
Push — master ( a4754c...e0c6ec )
by Nazar
05:32
created

Pages::get_map()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 7
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 1
nop 0
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * @package   Static Pages
4
 * @category  modules
5
 * @author    Nazar Mokrynskyi <[email protected]>
6
 * @copyright Copyright (c) 2011-2017, Nazar Mokrynskyi
7
 * @license   MIT License, see license.txt
8
 */
9
namespace cs\modules\Static_pages;
10
use
11
	cs\CRUD_helpers,
12
	cs\Singleton,
13
	cs\Cache\Prefix,
14
	cs\Config,
15
	cs\Language;
16
17
/**
18
 * @method static $this instance($check = false)
19
 */
20
class Pages {
21
	use
22
		CRUD_helpers,
23
		Singleton;
24
	protected $data_model                  = [
25
		'id'        => 'int',
26
		'category'  => 'int',
27
		'title'     => 'ml:text',
28
		'path'      => 'ml:text',
29
		'content'   => 'ml:',
30
		'interface' => 'int:0..1'
31
	];
32
	protected $table                       = '[prefix]static_pages';
33
	protected $data_model_ml_group         = 'Static_pages/pages';
34
	protected $data_model_files_tag_prefix = 'Static_pages/pages';
35
	/**
36
	 * @var Prefix
37
	 */
38
	protected $cache;
39
40
	protected function construct () {
41
		$this->cache = new Prefix('Static_pages');
42
	}
43
	/**
44
	 * Returns database index
45
	 *
46
	 * @return int
47
	 */
48
	protected function cdb () {
49
		return Config::instance()->module('Static_pages')->db('pages');
50
	}
51
	/**
52
	 * Get data of specified page
53
	 *
54
	 * @param int|int[] $id
55
	 *
56
	 * @return array|array|false
57
	 */
58
	public function get ($id) {
59
		if (is_array($id)) {
60
			return array_map([$this, 'get'], $id);
61
		}
62
		$L  = Language::instance();
63
		$id = (int)$id;
64
		return $this->cache->get(
65
			"pages/$id/$L->clang",
66
			function () use ($id) {
67
				$data = $this->read($id);
68
				if ($data) {
0 ignored issues
show
introduced by
The condition $data can never be true.
Loading history...
69
					if ($data['category']) {
70
						$category          = Categories::instance()->get($data['category']);
71
						$data['full_path'] = "$category[full_path]/$data[path]";
72
					} else {
73
						$data['full_path'] = $data['path'];
74
					}
75
				}
76
				return $data;
77
			}
78
		);
79
	}
80
	/**
81
	 * Get pages for category
82
	 *
83
	 * @param int $category
84
	 *
85
	 * @return int[]
86
	 */
87
	public function get_for_category ($category) {
88
		$search_parameters = [
89
			'category' => $category
90
		];
91
		return $this->search($search_parameters, 1, PHP_INT_MAX, 'title', true) ?: [];
92
	}
93
	/**
94
	 * Get number of pages for category
95
	 *
96
	 * @param int $category
97
	 *
98
	 * @return int
99
	 */
100
	public function get_for_category_count ($category) {
101
		$search_parameters = [
102
			'category'    => $category,
103
			'total_count' => true
104
		];
105
		return $this->search($search_parameters);
106
	}
107
	/**
108
	 * Get array of pages structure
109
	 *
110
	 * @return array|false
111
	 */
112
	public function get_map () {
113
		$L = Language::instance();
114
		return $this->cache->get(
115
			"map/$L->clang",
116
			function () {
117
				$pages = $this->get($this->search([], 1, PHP_INT_MAX) ?: []);
118
				return array_column($pages, 'id', 'full_path');
0 ignored issues
show
Bug introduced by
It seems like $pages can also be of type false; however, parameter $input of array_column() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

118
				return array_column(/** @scrutinizer ignore-type */ $pages, 'id', 'full_path');
Loading history...
119
			}
120
		);
121
	}
122
	/**
123
	 * Add new page
124
	 *
125
	 * @param int    $category
126
	 * @param string $title
127
	 * @param string $path
128
	 * @param string $content
129
	 * @param int    $interface
130
	 *
131
	 * @return false|int Id of created page on success of <b>false</> on failure
132
	 */
133
	public function add ($category, $title, $path, $content, $interface) {
134
		$id = $this->create($category, $title, path($path ?: $title), $content, $interface);
0 ignored issues
show
Bug introduced by
$category of type integer is incompatible with the type array expected by parameter $arguments of cs\modules\Static_pages\Pages::create(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

134
		$id = $this->create(/** @scrutinizer ignore-type */ $category, $title, path($path ?: $title), $content, $interface);
Loading history...
Bug introduced by
$title of type string is incompatible with the type array expected by parameter $arguments of cs\modules\Static_pages\Pages::create(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

134
		$id = $this->create($category, /** @scrutinizer ignore-type */ $title, path($path ?: $title), $content, $interface);
Loading history...
135
		if ($id) {
136
			$this->cache->del('/');
137
		}
138
		return $id;
139
	}
140
	/**
141
	 * Set data of specified page
142
	 *
143
	 * @param int    $id
144
	 * @param int    $category
145
	 * @param string $title
146
	 * @param string $path
147
	 * @param string $content
148
	 * @param int    $interface
149
	 *
150
	 * @return bool
151
	 */
152
	public function set ($id, $category, $title, $path, $content, $interface) {
153
		$result = $this->update($id, $category, $title, path($path ?: $title), $content, $interface);
0 ignored issues
show
Bug introduced by
$title of type string is incompatible with the type array expected by parameter $arguments of cs\modules\Static_pages\Pages::update(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

153
		$result = $this->update($id, $category, /** @scrutinizer ignore-type */ $title, path($path ?: $title), $content, $interface);
Loading history...
Bug introduced by
$id of type integer is incompatible with the type array expected by parameter $arguments of cs\modules\Static_pages\Pages::update(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

153
		$result = $this->update(/** @scrutinizer ignore-type */ $id, $category, $title, path($path ?: $title), $content, $interface);
Loading history...
154
		if ($result) {
155
			$this->cache->del('/');
156
		}
157
		return $result;
158
	}
159
	/**
160
	 * Delete specified page
161
	 *
162
	 * @param int $id
163
	 *
164
	 * @return bool
165
	 */
166
	public function del ($id) {
167
		$result = $this->delete($id);
168
		if ($result) {
169
			$this->cache->del('/');
170
		}
171
		return $result;
172
	}
173
}
174