Completed
Push — master ( c30967...3bd52a )
by Andrii
03:06
created

QueryBuilder::buildMethod()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 15
ccs 3
cts 3
cp 1
rs 9.4285
cc 2
eloc 11
nc 2
nop 1
crap 2
1
<?php
2
/**
3
 * ActiveRecord for API
4
 *
5
 * @link      https://github.com/hiqdev/yii2-hiart
6
 * @package   yii2-hiart
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2015-2017, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hiqdev\hiart\rest;
12
13
use hiqdev\hiart\Query;
14
15
class QueryBuilder extends \hiqdev\hiart\AbstractQueryBuilder
16
{
17
    protected $authHeaders = [];
18
19
    /**
20
     * This function is for you to provide your authentication.
21
     * @param Query $query
22
     */
23 2
    public function buildAuth(Query $query)
24
    {
25 2
        $auth = $this->db->getAuth();
26 2
        if (isset($auth['headerToken'])) {
27 2
            $this->authHeaders['Authorization'] = 'token ' . $auth['headerToken'];
28
        }
29 2
        if (isset($auth['headerBearer'])) {
30
            $this->authHeaders['Authorization'] = 'Bearer ' . $auth['headerBearer'];
31
        }
32 2
    }
33
34 2
    public function buildMethod(Query $query)
35
    {
36 2
        static $defaultMethods = [
37
            'get'       => 'GET',
38
            'put'       => 'PUT',
39
            'head'      => 'HEAD',
40
            'post'      => 'GET',
41
            'search'    => 'GET',
42
            'insert'    => 'POST',
43
            'update'    => 'PUT',
44
            'delete'    => 'DELETE',
45
        ];
46
47 2
        return isset($defaultMethods[$query->action]) ? $defaultMethods[$query->action] : 'POST';
48
    }
49
50
    public function buildUri(Query $query)
51
    {
52
        return $query->from;
53
    }
54
55 2
    public function buildHeaders(Query $query)
56
    {
57 2
        return $this->authHeaders;
58
    }
59
60 2
    public function buildProtocolVersion(Query $query)
61
    {
62 2
        return null;
63
    }
64
65 2
    public function buildQueryParams(Query $query)
66
    {
67 2
        return $query->where;
68
    }
69
70 2
    public function buildFormParams(Query $query)
71
    {
72 2
        return [];
73
    }
74
75 2
    public function buildBody(Query $query)
76
    {
77 2
        return null;
78
    }
79
}
80