Completed
Push — master ( 580028...d7e9e8 )
by Charles
02:19
created

Json25519ResponseFormatter   A

Complexity

Total Complexity 1

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 1
c 1
b 0
f 0
lcom 0
cbo 2
dl 0
loc 33
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B formatJson() 0 26 1
1
<?php
2
3
namespace yrc\web;
4
5
use yrc\web\JsonResponseFormatter;
6
use Yii;
7
8
class Json25519ResponseFormatter extends JsonResponseFormatter
9
{
10
    /**
11
     * Take the response generated by JsonResponseFormatter and anonymously encrypt it
12
     * @param array $response
13
     */
14
    protected function formatJson($response)
15
    {
16
        parent::formatJson($response);
0 ignored issues
show
Documentation introduced by
$response is of type array, but the function expects a object<yrc\web\Response>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
17
        $response->getHeaders()->set('Content-Type', 'application/json+25519; charset=UTF-8');
0 ignored issues
show
Bug introduced by
The method getHeaders cannot be called on $response (of type array).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
18
19
        // Retrieve the token object from the user
20
        $token = Yii::$app->user->getIdentity()->getToken();
21
22
        // Calculate the keypair
23
        $keyPair = \Sodium\crypto_box_keypair_from_secretkey_and_publickey(
24
            \base64_decode($token->getCryptToken()->secret_box_kp),
25
            \base64_decode($token->client_public)
26
        );
27
28
        // Calculate a nonce and set it in the header
29
        $nonce = \Sodium\randombytes_buf(\Sodium\CRYPTO_BOX_NONCEBYTES);
30
        $response->getHeaders()->set('x-nonce', \base64_encode($nonce));
0 ignored issues
show
Bug introduced by
The method getHeaders cannot be called on $response (of type array).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
31
        $response->getHeaders()->set('x-pubkey', \base64_encode($token->getCryptToken()->getBoxPublicKey()));
0 ignored issues
show
Bug introduced by
The method getHeaders cannot be called on $response (of type array).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
32
33
        // Encrypt the content
34
        $response->content = \base64_encode(\Sodium\crypto_box(
35
            $response->content,
36
            $nonce,
37
            $keyPair
38
        ));
39
    }
40
}