Test Failed
Pull Request — master (#5)
by
unknown
10:04
created

RequestData::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
c 0
b 0
f 0
ccs 5
cts 5
cp 1
rs 9.4285
cc 1
eloc 4
nc 1
nop 3
crap 1
1
<?php namespace Phabricator\Request;
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 3 and the first side effect is on line 87.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
3
class RequestData {
4
5
    /**
6
     * Raw data passed to API
7
     *
8
     * @type array
9
     */
10
    protected $rawData;
11
12
    /**
13
     * API token
14
     *
15
     * @type string
16
     */
17
    protected $token;
18
19
    /**
20
     * The wanted output format
21
     *
22
     * @type string
23
     */
24
    protected $output;
25
26 2
    public function __construct(array $rawParams, $authToken, $output = 'json') {
27 2
        $this->rawData = $rawParams;
28 2
        $this->token = $authToken;
29 2
        $this->output = $output;
30 2
    }
31
32
    /**
33
     * {@inheritDoc}
34
     */
35 1
    public function getResult() {
36 1
        return $this->process();
37
    }
38
39 1
    protected function process() {
40 1
        $returnedData = [];
41
42 1
        $params = $this->mergeConduitMetaData($this->rawData, $this->token);
43
44 1
        $returnedData['params'] = json_encode(self::array_utf8_encode($params));;
45 1
        $returnedData['output'] = $this->output;
46
47
        //This indicates the API to the client expects conduit response.
48
        //This sometimes provide more detailed error messages.
49 1
        $returnedData['__conduit__'] = TRUE;
50
51 1
        return $returnedData;
52
    }
53
54 2
    protected function mergeConduitMetaData($rawData, $token) {
55 2
        $params = $rawData;
56 2
        $tokenMeta = ['token' => $token];
57
58 2
        if(!isset($params['__conduit__'])) {
59 1
            $params['__conduit__'] = $tokenMeta;
60
61 1
            return $params;
62
        }
63
64 1
        $existingMeta = $params['__conduit__'];
65 1
        $params['__conduit__'] = array_merge($tokenMeta, $existingMeta);
66
67 1
        return $params;
68
    }
69
70
    protected function array_utf8_encode($data) 
71
    {
72
          if (is_string($data)) {
73
            return utf8_encode($data);
74
          } elseif (is_array($data)) {
75
            $ret = array();
76
77
            foreach ($data as $index => $value) {
78
              $ret[$index] = self::array_utf8_encode($value);
79
            }
80
81
           return $ret;
82
          }
83
84
          return $data;
85
       }
86
   }
87
}
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected '}'
Loading history...