Passed
Push — master ( 33103d...13ffe5 )
by Russell
02:55
created

Service::setExtra()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 10
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
3
/**
4
 * @author  Russell Michell 2018 <[email protected]>
5
 * @package silverstripe-verifiable
6
 */
7
8
namespace PhpTek\Verifiable\Backend\Chainpoint;
9
10
use SilverStripe\Core\Injector\Injector;
11
use SilverStripe\Core\Injector\Injectable;
12
use SilverStripe\Core\Config\Configurable;
13
use PhpTek\Verifiable\Exception\VerifiableBackendException;
14
use PhpTek\Verifiable\Backend\GatewayProvider;
15
use PhpTek\Verifiable\Backend\ServiceProvider;
16
use PhpTek\Verifiable\Backend\Chainpoint\Gateway;
17
18
/**
19
 * Service class that works as an intermediary between any data model and the
20
 * currently selected Merkle Tree backend gateway.
21
 */
22
class Service implements ServiceProvider
23
{
24
    use Injectable;
25
    use Configurable;
26
27
    /**
28
     * @var GatewayProvider
29
     */
30
    protected $backend;
31
32
    /**
33
     *
34
     * @var array
35
     */
36
    protected $extra = [];
37
38
    /**
39
     * @return void
40
     */
41
    public function __construct()
42
    {
43
        $this->setGateway();
44
    }
45
46
    /**
47
     * @return string
48
     */
49
    public function name() : string
50
    {
51
        return $this->getGateway()->name();
52
    }
53
54
    /**
55
     * Wrapper around all gateway methods.
56
     *
57
     * @param  string $method The name of the method to call
58
     * @param  mixed  $arg    The argument to pass to $method
59
     * @return mixed
60
     * @throws InvalidArgumentException
61
     */
62
    public function call($method, $arg)
63
    {
64
        if (!method_exists($this, $method)) {
65
            throw new \InvalidArgumentException("$method doesn't exist.");
66
        }
67
68
        $this->gateway->setDiscoveredNodes($this->getExtra());
69
70
        return $this->$method($arg);
71
    }
72
73
    /**
74
     * Write a hash of data as per the "verifiable_fields" config static on each
75
     * {@link DataObject}.
76
     *
77
     * @param  array $data
78
     * @return mixed The result of this call to the backend.
79
     */
80
    protected function write(array $data)
81
    {
82
        return $this->gateway->hashes([$this->hash($data)]);
83
    }
84
85
    /**
86
     * Fetch a chainpoint proof for the passed $uuid.
87
     *
88
     * @param  mixed string | array $uuid
89
     * @return string The JSON-LD chainpoint proof.
90
     */
91
    protected function read($uuid) : string
92
    {
93
        if (is_array($uuid)) {
94
            return $this->gateway->proofs(array_unique($uuid));
95
        }
96
97
        return $this->gateway->proofs($uuid);
98
    }
99
100
    /**
101
     * Verify the given JSON-LD chainpoint proof against the backend.
102
     *
103
     * @param  string $proof A JSON-LD chainpoint proof.
104
     * @return mixed
105
     */
106
    protected function verify(string $proof)
107
    {
108
        return $this->gateway->verify($proof);
109
    }
110
111
    /**
112
     * Set some arbitrary data onto the service. Used as a way of acting as
113
     * an intermediary or broker between DataOBjects and the backend.
114
     *
115
     * @param  array $extra
116
     * @return VerifiableService
0 ignored issues
show
Bug introduced by
The type PhpTek\Verifiable\Backen...point\VerifiableService 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...
117
     */
118
    public function setExtra(array $extra = [])
119
    {
120
        if (!$extra) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $extra of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
121
            $this->gateway->setDiscoveredNodes();
122
            $this->extra = $this->gateway->getDiscoveredNodes();
123
        } else {
124
            $this->extra = $extra;
125
        }
126
127
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type PhpTek\Verifiable\Backend\Chainpoint\Service which is incompatible with the documented return type PhpTek\Verifiable\Backen...point\VerifiableService.
Loading history...
128
    }
129
130
    /**
131
     * Return arbitrary data set to this service.
132
     *
133
     * @return array
134
     */
135
    public function getExtra()
136
    {
137
        return $this->extra;
138
    }
139
140
    /**
141
     * Set, configure and return a new Merkle Tree storage backend.
142
     *
143
     * @param  GatewayProvider   $provider Optional manually passed backend.
144
     * @return VerifiableService
145
     * @throws VerifiableBackendException
146
     */
147
    public function setGateway(GatewayProvider $provider = null)
148
    {
149
        if ($provider) {
150
            $this->gateway = $provider;
0 ignored issues
show
Bug Best Practice introduced by
The property gateway does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
151
152
            return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type PhpTek\Verifiable\Backend\Chainpoint\Service which is incompatible with the documented return type PhpTek\Verifiable\Backen...point\VerifiableService.
Loading history...
153
        }
154
155
        $this->gateway = Injector::inst()->create(Gateway::class);
156
    }
157
158
    /**
159
     * @return GatewayProvider
160
     */
161
    public function getGateway()
162
    {
163
        return $this->gateway;
164
    }
165
166
    /**
167
     * Hashes the data passed into the $hash param.
168
     *
169
     * @param  array  $data An array of data who's values should be hashed.
170
     * @return string       The resulting hashed data.
171
     */
172
    public function hash(array $data) : string
173
    {
174
        $func = $this->gateway->hashFunc();
175
        $text = json_encode($data, true);
0 ignored issues
show
Bug introduced by
true of type true is incompatible with the type integer expected by parameter $options of json_encode(). ( Ignorable by Annotation )

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

175
        $text = json_encode($data, /** @scrutinizer ignore-type */ true);
Loading history...
176
177
        return hash($func, $text);
178
    }
179
180
}
181