Completed
Push — master ( f91c7e...2ad33c )
by Nazar
04:05
created

Controller::common()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 6
rs 9.4285
1
<?php
2
/**
3
 * @package   Composer
4
 * @category  modules
5
 * @author    Nazar Mokrynskyi <[email protected]>
6
 * @copyright Copyright (c) 2015-2016, Nazar Mokrynskyi
7
 * @license   MIT License, see license.txt
8
 */
9
namespace cs\modules\Composer\api;
10
use
11
	cs\ExitException,
12
	cs\User,
13
	cs\modules\Composer\Composer;
14
15
class Controller {
16
	/**
17
	 * @return string
18
	 *
19
	 * @throws ExitException
20
	 */
21
	static function index_get () {
22
		static::common();
23
		$log_file = STORAGE.'/Composer/last_execution.log';
24
		return file_exists($log_file) ? ansispan(file_get_contents($log_file)) : '';
25
	}
26
	protected static function common () {
27
		if (!User::instance()->admin()) {
28
			throw new ExitException(403);
29
		}
30
		require_once __DIR__.'/../ansispan.php';
31
	}
32
	/**
33
	 * @param \cs\Request $Request
34
	 *
35
	 * @return array
36
	 *
37
	 * @throws ExitException
38
	 */
39
	static function index_post ($Request) {
40
		static::common();
41
		$force = $Request->data('force');
42
		$data  = $Request->data('name', 'category');
43
		if ($force) {
44
			$result = Composer::instance()->force_update();
45
		} elseif ($data) {
46
			$result = Composer::instance()->update($data['name'], $data['category'], Composer::MODE_ADD);
47
		} else {
48
			throw new ExitException(400);
49
		}
50
		return [
51
			'code'        => $result['code'],
52
			'description' => ansispan($result['description'])
53
		];
54
	}
55
	/**
56
	 * @param \cs\Request $Request
57
	 *
58
	 * @return array
59
	 *
60
	 * @throws ExitException
61
	 */
62
	static function index_delete ($Request) {
63
		static::common();
64
		$data = $Request->data('name', 'category');
65
		if (!$data) {
66
			throw new ExitException(400);
67
		}
68
		$result = Composer::instance()->update($data['name'], $data['category'], Composer::MODE_DELETE);
69
		return [
70
			'code'        => $result['code'],
71
			'description' => ansispan($result['description'])
72
		];
73
	}
74
}
75