Passed
Push — master ( 9ceb95...784902 )
by Russell
10:38
created

Trillian::hashFunc()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
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;
0 ignored issues
show
Bug introduced by
The type PhpTek\Verifiable\Verifiable 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...
12
use PhpTek\Verifiable\Exception\VerifiableValidationException;
13
use GuzzleHttp\Client;
14
use Guzzle\Http\Exception\RequestException;
0 ignored issues
show
Bug introduced by
The type Guzzle\Http\Exception\RequestException 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...
15
use Guzzle\Http\Message\Request;
0 ignored issues
show
Bug introduced by
The type Guzzle\Http\Message\Request 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...
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 hashFunc() : string
43
    {
44
        return 'sha256';
45
    }
46
47
    /**
48
     *
49
     * {@inheritdoc}
50
     */
51
    public function connect() : bool
52
    {
53
        // TODO
54
        $response = $this->client('/auth', 'GET', [
55
            'auth' => [
56
                $this->config()->get('connection', 'username'),
57
                $this->config()->get('connection', 'password'),
58
                'digest'
59
            ]
60
        ]);
61
62
        return $response->getStatusCode() === 200;
63
    }
64
65
    /**
66
     *
67
     * {@inheritdoc}
68
     */
69
    public function writeHash(array $hash) : string
70
    {
71
72
    }
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...
73
74
    /**
75
     *
76
     * {@inheritdoc}
77
     */
78
    public function getProof(string $hash) : string
79
    {
80
        if (!$this->connect()) {
81
            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...
82
        }
0 ignored issues
show
Bug Best Practice introduced by
The function implicitly returns null when the if condition on line 80 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...
83
    }
84
85
    /**
86
     *
87
     * {@inheritdoc}
88
     */
89
    public function verifyProof(string $proof) : bool
90
    {
91
92
    }
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...
93
94
    /**
95
     * Return a client to use for all RPC traffic to this backend.
96
     *
97
     * @param  string             $url
98
     * @param  string             $verb
99
     * @param  array              $payload
100
     * @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...
101
     * @throws VerifiableBackendException
102
     */
103
    private function client(string $url, string $verb, array $payload = [])
104
    {
105
        $verb = strtoupper($verb);
106
        // See Client()->setSslVerification() if required
107
        $client = new Client([
108
            'base_uri' => $this->config()->get('trillian', 'params')['base_uri'],
109
            'timeout'  => $this->config()->get('trillian', 'params')['timeout'],
110
        ]);
111
        $request = new Request($verb, $url, $payload);
112
113
        try {
114
            $client->send($request);
115
116
            if (!preg_match("#^2#", $code = $request->getStatusCode())) {
117
                throw new VerifiableBackendException(sprintf('Request gave HTTP status: %d', $code));
118
            }
119
        } catch (RequestException $e) {
120
            throw new VerifiableBackendException($e->getMessage());
121
        }
122
    }
123
124
    /**
125
     * @param  string $data                  The data to be verified
126
     * @throws VerifiableValidationException In the event invalid data is detected
127
     *                                       Sure-fire way to prevent a malformed
128
     *                                       write to the backend.
129
     * @return void
130
     * @todo   Implement a dedicated hash-specific handler
131
     */
132
    public function validate(string $data)
133
    {
134
        $func = Verifiable::config()->get('hash_func');
135
136
        if ($func == 'sha1') {
137
            if (strlen($data) !== 40) {
138
                throw new VerifiableValidationException(sprintf('Invalid %s hash: Length', $func));
139
            }
140
        }
141
    }
142
143
}
144