Passed
Push — master ( a193ba...b79231 )
by Russell
09:06
created

Trillian::client()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 11
nc 4
nop 3
dl 0
loc 18
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * @author  Russell Michell 2018 <[email protected]>
5
 * @package silverstripe-verifiable
6
 */
7
8
namespace PhpTek\Verifiable\Backend;
9
10
use PhpTek\Verifiable\Backend\BackendProvider;
11
use PhpTek\Verifiable\Verifiable;
12
use PhpTek\Verifiable\Exception\VerifiableValidationException;
13
use GuzzleHttp\Client;
0 ignored issues
show
Bug introduced by
The type GuzzleHttp\Client was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
14
use Guzzle\Http\Exception\RequestException;
15
use Guzzle\Http\Message\Request;
16
use PhpTek\Verifiable\Exception\VerifiableBackendException;
17
use SilverStripe\Core\Config\Configurable;
18
19
/**
20
 * Trillian relies on something called a "Personality" to supply it with the exact
21
 * type and format of data, that the overall application is expecting it to store.
22
 * As such Trillian itself will perform no data validation or normalisation, favouring
23
 * instead to farm out this responsibility to personalities.
24
 */
25
class Trillian implements BackendProvider
26
{
27
    use Configurable;
28
29
    /**
30
     *
31
     * {@inheritdoc}
32
     */
33
    public function name() : string
34
    {
35
        return 'trillian';
36
    }
37
38
    /**
39
     *
40
     * {@inheritdoc}
41
     */
42
    public function connect() : bool
43
    {
44
        // TODO
45
        $response = $this->client('/auth', 'GET', [
46
            'auth' => [
47
                $this->config()->get('connection', 'username'),
48
                $this->config()->get('connection', 'password'),
49
                'digest'
50
            ]
51
        ]);
52
53
        return $response->getStatusCode() === 200;
54
    }
55
56
    /**
57
     *
58
     * {@inheritdoc}
59
     */
60
    public function writeHash(string $hash) : string
61
    {
62
63
    }
0 ignored issues
show
Bug Best Practice introduced by
In this branch, the function will implicitly return null which is incompatible with the type-hinted return string. Consider adding a return statement or allowing null as return value.

For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example:

interface ReturnsInt {
    public function returnsIntHinted(): int;
}

class MyClass implements ReturnsInt {
    public function returnsIntHinted(): int
    {
        if (foo()) {
            return 123;
        }
        // here: null is implicitly returned
    }
}
Loading history...
64
65
    /**
66
     *
67
     * {@inheritdoc}
68
     */
69
    public function getProof(string $hash) : string
70
    {
71
        if (!$this->connect()) {
72
            return [];
0 ignored issues
show
Bug Best Practice introduced by
The expression return array() returns the type array which is incompatible with the type-hinted return string.
Loading history...
73
        }
0 ignored issues
show
Bug Best Practice introduced by
The function implicitly returns null when the if condition on line 71 is false. This is incompatible with the type-hinted return string. Consider adding a return statement or allowing null as return value.

For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example:

interface ReturnsInt {
    public function returnsIntHinted(): int;
}

class MyClass implements ReturnsInt {
    public function returnsIntHinted(): int
    {
        if (foo()) {
            return 123;
        }
        // here: null is implicitly returned
    }
}
Loading history...
74
    }
75
76
    /**
77
     *
78
     * {@inheritdoc}
79
     */
80
    public function verifyProof(string $proof) : bool
81
    {
82
83
    }
0 ignored issues
show
Bug Best Practice introduced by
In this branch, the function will implicitly return null which is incompatible with the type-hinted return boolean. Consider adding a return statement or allowing null as return value.

For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example:

interface ReturnsInt {
    public function returnsIntHinted(): int;
}

class MyClass implements ReturnsInt {
    public function returnsIntHinted(): int
    {
        if (foo()) {
            return 123;
        }
        // here: null is implicitly returned
    }
}
Loading history...
84
85
    /**
86
     * Return a client to use for all RPC traffic to this backend.
87
     *
88
     * @param  string             $url
89
     * @param  string             $verb
90
     * @param  array              $payload
91
     * @return GuzzleHTTPResponse
0 ignored issues
show
Bug introduced by
The type PhpTek\Verifiable\Backend\GuzzleHTTPResponse was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
92
     * @throws VerifiableBackendException
93
     */
94
    private function client(string $url, string $verb, array $payload = [])
95
    {
96
        $verb = strtoupper($verb);
97
        // See Client()->setSslVerification() if required
98
        $client = new Client([
99
            'base_uri' => $this->config()->get('trillian', 'params')['base_uri'],
100
            'timeout'  => $this->config()->get('trillian', 'params')['timeout'],
101
        ]);
102
        $request = new Request($verb, $url, $payload);
103
104
        try {
105
            $client->send($request);
106
107
            if (!preg_match("#^2#", $code = $request->getStatusCode())) {
0 ignored issues
show
Bug introduced by
The method getStatusCode() does not exist on Guzzle\Http\Message\Request. Did you maybe mean getState()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

107
            if (!preg_match("#^2#", $code = $request->/** @scrutinizer ignore-call */ getStatusCode())) {

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
108
                throw new VerifiableBackendException(sprintf('Request gave HTTP status: %d', $code));
109
            }
110
        } catch (RequestException $e) {
111
            throw new VerifiableBackendException($e->getMessage());
112
        }
113
    }
114
115
    /**
116
     * @param  string $data                  The data to be verified
117
     * @throws VerifiableValidationException In the event invalid data is detected
118
     *                                       Sure-fire way to prevent a malformed
119
     *                                       write to the backend.
120
     * @return void
121
     * @todo   Implement a dedicated hash-specific handler
122
     */
123
    public function validate(string $data)
124
    {
125
        $func = Verifiable::config()->get('hash_func');
126
127
        if ($func == 'sha1') {
128
            if (strlen($data) !== 40) {
129
                throw new VerifiableValidationException(sprintf('Invalid %s hash: Length', $func));
130
            }
131
        }
132
    }
133
134
}
135