Completed
Push — master ( a85116...adb355 )
by Francis
01:32
created

RESTResponse::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 4
rs 10
1
<?php
2
defined('BASEPATH') OR exit('No direct script access allowed');
3
4
class RESTResponse extends CI_Controller
0 ignored issues
show
Bug introduced by
The type CI_Controller was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
5
{
6
  // Response Codes
7
  // 40*
8
  const BAD_REQUEST           = 400;
9
  const UN_AUTHORIZED         = 401;
10
  const FORBIDDEN             = 403;
11
  const NOT_ACCEPTABLE        = 406;
12
  const TOO_MANY_REQUESTS     = 429;
13
  // 50*
14
  const INTERNAL_SERVER_ERROR = 500;
15
  const NOT_IMPLEMENTED       = 501;
16
  /**
17
   * [protected HTTP Response Code]
18
   * @var int
19
   */
20
  protected $code;
21
  /**
22
   * [protected Response Data]
23
   * @var mixed
24
   */
25
  protected $data;
26
  /**
27
   * [protected Shoud Response be JSON Encoded?]
28
   * @var bool
29
   */
30
  protected $json;
31
  function __construct($data, int $code)
32
  {
33
    $this->data = $data;
34
    $this->code = $code;
35
  }
36
  /**
37
   * [__toString description]
38
   * @date   2019-11-09
39
   * @return string     [description]
40
   */
41
  public function __toString():string
42
  {
43
    return !$this->json ? $this->data : n_encode($this->data);
0 ignored issues
show
Bug introduced by
The function n_encode was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

43
    return !$this->json ? $this->data : /** @scrutinizer ignore-call */ n_encode($this->data);
Loading history...
44
  }
45
  /**
46
   * [setJSON description]
47
   * @date   2019-11-09
48
   * @param  bool       $json [description]
49
   * @return RESTResponse         [description]
50
   */
51
  public function setJSON(bool $json):RESTResponse
52
  {
53
    $this->json = $json;
54
    return $this;
55
  }
56
  /**
57
   * [send description]
58
   * @date 2019-11-09
59
   */
60
  public function send():void
61
  {
62
    http_response_code($this->code);
63
64
    if ($data != null) echo !$this->json ? $this->data : json_encode($this->data);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $data seems to be never defined.
Loading history...
65
66
    if (get_instance()->config->item('rest')['response_exit']) {
0 ignored issues
show
Bug introduced by
The function get_instance was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

66
    if (/** @scrutinizer ignore-call */ get_instance()->config->item('rest')['response_exit']) {
Loading history...
67
      exit(EXIT_SUCCESS);
0 ignored issues
show
Bug introduced by
The constant EXIT_SUCCESS was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
68
    }
69
  }
70
}
71
?>
0 ignored issues
show
Best Practice introduced by
It is not recommended to use PHP's closing tag ?> in files other than templates.

Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.

A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.

Loading history...
72