Completed
Push — master ( 2ce496...5c1ad5 )
by Sherif
06:14
created

ApiDocumentController::index()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 52
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 52
rs 9.4929
c 0
b 0
f 0
cc 1
eloc 28
nc 1
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
namespace App\Modules\V1\Core\Http\Controllers;
3
4
use App\Http\Controllers\Controller;
5
use Illuminate\Http\Request;
6
7
class ApiDocumentController extends Controller
8
{
9
	public function index() 
0 ignored issues
show
Coding Style introduced by
index uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
10
	{
11
		$path       = str_replace($_SERVER['DOCUMENT_ROOT'], '',str_replace('\\', '/', __FILE__));
12
		$baseUrl    = str_replace('Http/Controllers/ApiDocumentController.php', '', $path);
13
		$jsonDoc    = json_decode(file_get_contents(app_path('Modules/V1/Core/Resources/api.json')), true);
14
		$modules    = $jsonDoc['modules'];
15
		$errors     = $jsonDoc['errors'];
16
		$conditions = [
17
			[
18
				"title"   => "email equal [email protected]:",
19
				"content" => "{\n  \"email\" =>\"[email protected]\"\n}"
20
			],
21
			[
22
				"title"   => "email equal [email protected] and user is blocked:",
23
				"content" => "{\n  \"and\":{\n  \"email\" =>\"[email protected]\",\n  \"blocked\" =>1\n  }\n}"
24
			],
25
			[
26
				"title"   => "email equal [email protected] or user is blocked:",
27
				"content" => "{\n  \"or\":{\n  \"email\" =>\"[email protected]\",\n  \"blocked\" =>1\n  {\n}"
28
			],
29
			[
30
				"title"   => "email contain John:",
31
				"content" => "{\n  \"email\" =>{\n  \"op\" =>\"like\",\n  \"val\" =>\"%John%\"\n  }\n}"
32
			],
33
			[
34
				"title"   => "user created after 2016-10-25:",
35
				"content" => "{\n  \"created_at\" =>{\n  \"op\" =>\">\",\n  \"val\" =>\"2016-10-25\"\n  }\n}"
36
			],
37
			[
38
				"title"   => "user created between 2016-10-20 and 2016-10-25:",
39
				"content" => "{\n  \"created_at\" =>{\n  \"op\" =>\"between\",\n  \"val1\" =>\"2016-10-20\",\n  \"val2\" =>\"2016-10-25\"\n  {\n}"
40
			],
41
			[
42
				"title"   => "user id in 1,2,3:",
43
				"content" => "{\n  \"id\" =>{\n  \"op\" =>\"in\",\n  \"val\" =>[1, 2, 3]\n}"
44
			],
45
			[
46
				"title"   => "user name is null:",
47
				"content" => "{\n  \"name\" =>{\n  \"op\" =>\"null\"\n}"
48
			],
49
			[
50
				"title"   => "user name is not null:",
51
				"content" => "{\n  \"name\" =>{\n  \"op\" =>\"not null\"\n}"
52
			],
53
			[
54
				"title"   => "user has group admin:",
55
				"content" => "{\n  \"groups\" =>{\n  \"op\" =>\"has\",\n  \"val\" =>{\n  \t\"name\" =>\"Admin\"\n  }\n}"
56
			]
57
		];
58
59
		return view('core::doc', ['baseUrl' => $baseUrl, 'modules' => $modules, 'errors' => $errors, 'conditions' => $conditions]);
60
	}
61
62
	public function testApi(Request $request)
63
	{
64
		$actoinArray     = explode('@', $request->get('action'));
65
		$controller      = $actoinArray[0];
66
		$method          = $actoinArray[1];
67
		$method          = $method !== 'index' ? $method : 'all';
68
		$reflectionClass = new \ReflectionClass($controller);
69
		$classProperties = $reflectionClass->getDefaultProperties();
70
		
71
		return \Response::json(call_user_func_array("\Core::{$classProperties['model']}", [])->$method(), 200);;
72
	}
73
}
74