Completed
Push — master ( ab09db...eef690 )
by Nazar
04:19
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\modules\Blogs\Sections;
15
16
trait admin {
17
	static function admin___get_settings () {
18
		$module_data = Config::instance()->module('Blogs');
19
		return [
20
			'posts_per_page'                => $module_data->posts_per_page,
21
			'max_sections'                  => $module_data->max_sections,
22
			'enable_comments'               => $module_data->enable_comments,
23
			'new_posts_only_from_admins'    => $module_data->new_posts_only_from_admins,
24
			'allow_iframes_without_content' => $module_data->allow_iframes_without_content
25
		];
26
	}
27
	/**
28
	 * @param \cs\Request $Request
29
	 *
30
	 * @throws ExitException
31
	 */
32
	static function admin___save_settings ($Request) {
33
		$data = $Request->data('posts_per_page', 'max_sections', 'enable_comments', 'new_posts_only_from_admins', 'allow_iframes_without_content');
34
		if (!$data) {
35
			throw new ExitException(400);
36
		}
37
		if (!Config::instance()->module('Blogs')->set($data)) {
38
			throw new ExitException(500);
39
		}
40
	}
41
	/**
42
	 * @param \cs\Request $Request
43
	 *
44
	 * @return array
45
	 *
46
	 * @throws ExitException
47
	 */
48
	static function admin_sections_get ($Request) {
49
		return static::sections_get($Request);
1 ignored issue
show
Bug introduced by
The method sections_get() does not exist on cs\modules\Blogs\api\Controller\admin. Did you maybe mean admin_sections_get()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
50
	}
51
	/**
52
	 * @param \cs\Request  $Request
53
	 * @param \cs\Response $Response
54
	 *
55
	 * @return array
1 ignored issue
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use array<string,integer|string>.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
56
	 *
57
	 * @throws ExitException
58
	 */
59
	static function admin_sections_post ($Request, $Response) {
60
		$data = $Request->data('title', 'path', 'parent');
61
		if (!$data) {
62
			throw new ExitException(400);
63
		}
64
		$Sections = Sections::instance();
65
		$id       = $Sections->add($data['parent'], $data['title'], $data['path']);
66
		if (!$id) {
67
			throw new ExitException(Language::instance()->blogs_changes_save_error, 500);
68
		}
69
		$Response->code = 201;
70
		return [
71
			'id'  => $id,
72
			'url' => Config::instance()->base_url().'/Blogs/section/'.$Sections->get($id)['full_path']
73
		];
74
	}
75
	/**
76
	 * @param \cs\Request $Request
77
	 *
78
	 * @return array
1 ignored issue
show
Documentation introduced by
Should the return type not be array|null? 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...
79
	 *
80
	 * @throws ExitException
81
	 */
82
	static function admin_sections_put ($Request) {
83
		$id   = $Request->route_ids(0);
84
		$data = $Request->data('title', 'path', 'parent');
85
		if (!$id || !$data) {
86
			throw new ExitException(400);
87
		}
88
		$Sections = Sections::instance();
89
		if (!$Sections->get($id)) {
90
			throw new ExitException(404);
91
		}
92
		if (!$Sections->set($id, $data['parent'], $data['title'], $data['path'])) {
93
			throw new ExitException(Language::instance()->blogs_changes_save_error, 500);
94
		}
95
	}
96
	/**
97
	 * @param \cs\Request $Request
98
	 *
99
	 * @return array
1 ignored issue
show
Documentation introduced by
Should the return type not be array|null? 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...
100
	 *
101
	 * @throws ExitException
102
	 */
103
	static function admin_sections_delete ($Request) {
104
		$id = $Request->route_ids(0);
105
		if (!$id) {
106
			throw new ExitException(400);
107
		}
108
		$Sections = Sections::instance();
109
		if (!$Sections->get($id)) {
110
			throw new ExitException(404);
111
		}
112
		if (!$Sections->del($id)) {
113
			throw new ExitException(Language::instance()->blogs_changes_save_error, 500);
114
		}
115
	}
116
}
117