Completed
Push — master ( b4e787...2e096b )
by Nazar
04:03
created

admin::admin_sections_post()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 16
rs 9.4285
cc 3
eloc 12
nc 3
nop 2
1
<?php
2
/**
3
 * @package   Blogs
4
 * @category  modules
5
 * @author    Nazar Mokrynskyi <[email protected]>
6
 * @copyright Copyright (c) 2011-2016, Nazar Mokrynskyi
7
 * @license   MIT License, see license.txt
8
 */
9
namespace cs\modules\Blogs\api\Controller;
10
use
11
	cs\Config,
12
	cs\ExitException,
13
	cs\Language,
14
	cs\User,
15
	cs\modules\Blogs\Posts,
16
	cs\modules\Blogs\Sections;
17
18
trait admin {
19
	static function admin___get_settings () {
20
		$module_data = Config::instance()->module('Blogs');
21
		return [
22
			'posts_per_page'                => $module_data->posts_per_page,
23
			'max_sections'                  => $module_data->max_sections,
24
			'enable_comments'               => $module_data->enable_comments,
25
			'new_posts_only_from_admins'    => $module_data->new_posts_only_from_admins,
26
			'allow_iframes_without_content' => $module_data->allow_iframes_without_content
27
		];
28
	}
29
	/**
30
	 * @param \cs\Request $Request
31
	 *
32
	 * @throws ExitException
33
	 */
34
	static function admin___save_settings ($Request) {
35
		$data = $Request->data('posts_per_page', 'max_sections', 'enable_comments', 'new_posts_only_from_admins', 'allow_iframes_without_content');
36
		if (!$data) {
37
			throw new ExitException(400);
38
		}
39
		if (!Config::instance()->module('Blogs')->set($data)) {
40
			throw new ExitException(500);
41
		}
42
	}
43
	/**
44
	 * @param \cs\Request $Request
45
	 *
46
	 * @return array
1 ignored issue
show
Documentation introduced by
Should the return type not be array|false? Also, consider making the array more specific, something like array<String>, or 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.

If the return type contains the type array, this check recommends the use of a more specific type like String[] or array<String>.

Loading history...
47
	 *
48
	 * @throws ExitException
49
	 */
50
	static function admin_posts_get ($Request) {
51
		$id    = $Request->route_ids(0);
52
		$Posts = Posts::instance();
53
		if ($id) {
54
			$data = $Posts->get($id);
55
			if (!$data) {
56
				throw new ExitException(404);
57
			}
58
			return $data;
59
		}
60
		$posts = $Posts->get(
61
			$Posts->get_all(1, PHP_INT_MAX)
62
		);
63
		$L     = Language::instance();
64
		$User  = User::instance();
65
		foreach ($posts as &$post) {
1 ignored issue
show
Bug introduced by
The expression $posts of type array|false is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
66
			$post['username']       = $User->username($post['user']);
67
			$post['date_formatted'] = date($L->_datetime, $post['date']);
68
		}
69
		return $posts;
70
	}
71
	/**
72
	 * @param \cs\Request $Request
73
	 *
74
	 * @throws ExitException
75
	 */
76
	static function admin_posts_delete ($Request) {
77
		$id = $Request->route_ids(0);
78
		if (!$id) {
79
			throw new ExitException(400);
80
		}
81
		$Posts = Posts::instance();
82
		if (!$Posts->get($id)) {
83
			throw new ExitException(404);
84
		}
85
		if (!$Posts->del($id)) {
86
			throw new ExitException(Language::instance()->blogs_changes_save_error, 500);
87
		}
88
	}
89
	/**
90
	 * @param \cs\Request $Request
91
	 *
92
	 * @return array
93
	 *
94
	 * @throws ExitException
95
	 */
96
	static function admin_sections_get ($Request) {
97
		return static::sections_get($Request);
98
	}
99
	/**
100
	 * @param \cs\Request  $Request
101
	 * @param \cs\Response $Response
102
	 *
103
	 * @return array
104
	 *
105
	 * @throws ExitException
106
	 */
107
	static function admin_sections_post ($Request, $Response) {
108
		$data = $Request->data('title', 'path', 'parent');
109
		if (!$data) {
110
			throw new ExitException(400);
111
		}
112
		$Sections = Sections::instance();
113
		$id       = $Sections->add($data['parent'], $data['title'], $data['path']);
114
		if (!$id) {
115
			throw new ExitException(Language::instance()->blogs_changes_save_error, 500);
116
		}
117
		$Response->code = 201;
118
		return [
119
			'id'  => $id,
120
			'url' => Config::instance()->base_url().'/Blogs/section/'.$Sections->get($id)['full_path']
121
		];
122
	}
123
	/**
124
	 * @param \cs\Request $Request
125
	 *
126
	 * @throws ExitException
127
	 */
128
	static function admin_sections_put ($Request) {
129
		$id   = $Request->route_ids(0);
130
		$data = $Request->data('title', 'path', 'parent');
131
		if (!$id || !$data) {
132
			throw new ExitException(400);
133
		}
134
		$Sections = Sections::instance();
135
		if (!$Sections->get($id)) {
136
			throw new ExitException(404);
137
		}
138
		if (!$Sections->set($id, $data['parent'], $data['title'], $data['path'])) {
139
			throw new ExitException(Language::instance()->blogs_changes_save_error, 500);
140
		}
141
	}
142
	/**
143
	 * @param \cs\Request $Request
144
	 *
145
	 * @throws ExitException
146
	 */
147
	static function admin_sections_delete ($Request) {
148
		$id = $Request->route_ids(0);
149
		if (!$id) {
150
			throw new ExitException(400);
151
		}
152
		$Sections = Sections::instance();
153
		if (!$Sections->get($id)) {
154
			throw new ExitException(404);
155
		}
156
		if (!$Sections->del($id)) {
157
			throw new ExitException(Language::instance()->blogs_changes_save_error, 500);
158
		}
159
	}
160
}
161