RequestData::__construct()   A
last analyzed

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;
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($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
}