PostmanAuthAction::post()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 37

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 37
rs 9.328
c 0
b 0
f 0
cc 3
nc 4
nop 1
1
<?php
2
3
namespace api\actions;
4
5
use yrc\rest\Action;
6
use yrc\web\Response;
7
use yii\helpers\Json;
8
use Yii;
9
use DateTime;
10
11
/**
12
 * @class PostmanAuthAction
13
 * Helper to assist with generation the necessary auth headers for postman
14
 */
15
class PostmanAuthAction extends Action
16
{
17
    /**
18
     * @param array $params
19
     */
20
    public function post(array $params = [])
21
    {
22
        $response = new Response;
23
        $response->format = Response::FORMAT_JSON;
24
        
25
        $now = new \DateTime();
26
        $time = $now->format(\DateTime::RFC1123);
27
28
        $requestUrl = Yii::$app->request->post('url');
29
        $uri = '/' . implode('/', $requestUrl['path']);
30
        $queryParams = [];
31
        foreach ($requestUrl['query'] as $param) {
32
            $queryParams[$param['key']] = $param['value'];
33
        }
34
        $query = \http_build_query($queryParams);
35
36
        require __DIR__ . '/../../tests/_support/HMAC.php';
37
38
        $payload = Yii::$app->request->post('payload');
39
        $url = $uri;
40
        if ($query != '') {
41
            $url .= '?' . $query;
42
        }
43
44
        $hmac = \tests\_support\HMAC::generate(
45
            $url,
46
            Yii::$app->request->post('tokens'),
47
            Yii::$app->request->post('method'),
48
            $time,
49
            $payload,
50
            true
51
        );
52
53
        $response->data = explode(',', $hmac);
54
        $response->data[] = $time;
55
        return $response;
56
    }
57
}
58