Passed
Push — master ( 7f61ea...38db0f )
by Russell
13:01
created

VerifiableService::queuePing()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 5
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;
9
10
use SilverStripe\Core\Injector\Injector;
11
use SilverStripe\Core\Injector\Injectable;
12
use SilverStripe\Core\Config\Configurable;
13
use SilverStripe\Core\ClassInfo;
14
use PhpTek\Verifiable\Exception\VerifiableBackendException;
15
use PhpTek\Verifiable\Exception\VerifiableServiceException;
16
use PhpTek\Verifiable\Job\BackendVerificationJob;
17
18
if (!class_exists(Symbiote\QueuedJobs\Services\QueuedJobService)) {
0 ignored issues
show
Bug introduced by
The constant PhpTek\Verifiable\Symbio...rvices\QueuedJobService was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
19
    throw new VerifiableServiceException('QueuedJobs module is not installed.');
20
}
21
22
/**
23
 * Service class that works as an intermediary between any data model and the
24
 * currently selected Merkle Tree storage backend.
25
 */
26
class VerifiableService
27
{
28
    use Injectable;
29
    use Configurable;
30
31
    /**
32
     * The hashing function to use.
33
     *
34
     * @var string
35
     * @see {@link $this->hash()}
36
     * @config
37
     */
38
    private static $hash_func = 'sha1';
0 ignored issues
show
introduced by
The private property $hash_func is not used, and could be removed.
Loading history...
39
40
    /**
41
     * @var BackendProvider
0 ignored issues
show
Bug introduced by
The type PhpTek\Verifiable\BackendProvider 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...
42
     */
43
    protected $backend;
44
45
    /**
46
     * @return void
47
     * @throws VerifiableBackendException
48
     */
49
    public function __construct()
50
    {
51
        $this->setBackend();
52
    }
53
54
    /**
55
     * Write a hash of data as per the "verifiable_fields" confifg static on each
56
     * {@link DataObject}.
57
     *
58
     * @param  array  $data
59
     * @return boolean True if the write went through OK. False otherwise.
60
     */
61
    public function write(array $data) : boolean
0 ignored issues
show
Bug introduced by
The type PhpTek\Verifiable\boolean 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...
62
    {
63
        return $this->backend->writeHash($this->hash($data));
64
    }
65
66
    /**
67
     * Fetch a chainpoint proof for the passed $hash.
68
     *
69
     * @param  string $hash
70
     * @return string The JSON-LD chainpoint proof.
71
     */
72
    public function read(string $hash) : string
73
    {
74
        return $this->backend->getProof($hash);
75
    }
76
77
    /**
78
     * Verify the given JSON-LD chainpoint proof against the backend.
79
     *
80
     * @param  string $proof A JSON-LD chainpoint proof.
81
     * @return bool
82
     */
83
    public function verify(string $proof) : bool
84
    {
85
        return $this->backend->verifyProof($proof);
86
    }
87
88
    /**
89
     * Set, instantiate and return a new Merkle Tree storage backend.
90
     *
91
     * @param  BackendProvider $provider Optional manually pased backend.
92
     * @return VerifiableService
93
     * @throws VerifiableBackendException
94
     */
95
    public function setBackend(BackendProvider $provider = null)
96
    {
97
        if ($provider) {
98
            $this->backend = $provider;
99
100
            return $this;
101
        }
102
103
        $namedBackend = $this->config()->get('backend');
104
        $backends = ClassInfo::implementorsOf('BackendProvider');
105
106
        foreach ($backends as $backend) {
107
            if (singleton($backend)->name() === $namedBackend) {
108
                $this->backend = Injector::inst()->create($backend);
109
110
                return $this;
111
            }
112
        }
113
114
        // Cannt continue without a legit backend
115
        throw new VerifiableBackendException('No backend found');
116
    }
117
118
    /**
119
     * @return BackendProvider
120
     */
121
    public function getBackend()
122
    {
123
        return $this->backend;
124
    }
125
126
    /**
127
     * Hashes the data passed into the $hash param.
128
     *
129
     * @param  array $data An array of data who's values should be hashed.
130
     * @return string      The resulting hashed data.
131
     * @todo               Take user input in the form of a digital signature
132
     */
133
    public function hash(array $data) : string
134
    {
135
        $func = $this->config()->get('hash_func');
136
        $text = implode('', $data);
137
138
        return $func($text);
139
    }
140
141
    /**
142
     * Setup a {@link QueuedJob} to ping a backend and update the passed dataobject's
143
     * "Proof" field when a chainpoint proof has been generated.
144
     *
145
     * @param  DataObject $model The {@link DataObject} model subclass with a "Proof" field
146
     * @return void
147
     */
148
    public function queuePing(DataObject $model)
0 ignored issues
show
Bug introduced by
The type PhpTek\Verifiable\DataObject 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...
149
    {
150
        $job = BackendVerificationJob::create()->setObject($model);
151
152
        singleton(QueuedJobService::class)->queue($job);
0 ignored issues
show
Bug introduced by
The type PhpTek\Verifiable\QueuedJobService 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...
153
    }
154
155
}
156