Passed
Push — master ( 9c4857...eecc1f )
by Russell
02:55
created

Verifiable::read()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 0
nc 1
nop 1
dl 0
loc 2
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;
9
10
use SilverStripe\Core\Injector\Injector;
11
use SilverStripe\Core\Injector\Injectable;
12
use SilverStripe\Core\Config\Configurable;
13
use SilverStripe\ORM\DataObject;
14
use SilverStripe\Core\ClassInfo;
15
use PhpTek\Verifiable\Exception\VerifiableBackendException;
16
use SilverStripe\ORM\DataObjectSchema;
17
18
/**
19
 * Does all the connect/read/write heavy-lifting.
20
 */
21
class Verifiable
22
{
23
    use Injectable;
24
    use Configurable;
25
26
    /**
27
     * The hashing function to use.
28
     *
29
     * @var string
30
     * @see {@link $this->hash()}
31
     * @config
32
     */
33
    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...
34
35
    /**
36
     * @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...
37
     */
38
    protected $backend;
39
40
    /**
41
     * @var DataObjectProvider
0 ignored issues
show
Bug introduced by
The type PhpTek\Verifiable\DataObjectProvider 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 $model;
44
45
    /**
46
     * @return void
47
     * @throws VerifiableBackendException
48
     */
49
    public function __construct()
50
    {
51
        if (!$this->backend = $this->backend()) {
52
            throw new VerifiableBackendException('Backend not found or not specified.');
53
        }
54
    }
55
56
    /**
57
     * Write a hash of data as per the "verifiable_fields" confif static on each
58
     * {@link DataObject}.
59
     *
60
     * @return boolean True if the write went through OK. False otherwise.
61
     */
62
    public function write() : 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...
63
    {
64
        $fields = $this->model->config()->get('verifiable_fields');
65
    	$hash = $this->hash($fields);
66
67
        return $this->backend->write($hash);
68
    }
69
70
    /**
71
     * Get and instantiate a new backend
72
     *
73
     * @return mixed null | BackendProvider
74
     */
75
    public function backend()
76
    {
77
        $namedBackend = $this->config()->get('backend');
78
        $backends = ClassInfo::implementorsOf('BackendProvider');
79
80
        foreach ($backends as $backend) {
81
            if (singleton($backend)->name() == $namedBackend) {
82
                return Injector::inst()->create($backend);
83
            }
84
        }
85
86
        return null;
87
    }
88
89
    /**
90
     * Hashes the data found in all the fields of the current Data Model.
91
     *
92
     * @param  array $fields The fields on the current {@link DataObject} subclass
93
     *                       who's values should be hashed.
94
     * @return string
95
     * @todo   Take use input in the form of a digital signature
96
     */
97
    public function hash(array $fields) : string
98
    {
99
        $text = '';
100
        $class = get_class($this->model);
101
        $func = $this->config()->get('hash_func');
102
        $specs = array_keys(
103
            $this->model->getSchema()->fielSpecs($class, DataObjectSchema::UNINHERITED)
104
        );
105
106
        foreach ($specs as $name) {
107
            if (isset($fields[$name])) {
108
                $text .= $this->model->getField($name);
109
            }
110
        }
111
112
        return $func($test);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $test seems to be never defined.
Loading history...
113
    }
114
115
    /**
116
     * @param  DataObject $model
117
     * @return Verifiable
118
     */
119
    public function setModel(DataObject $model) : Verifiable
120
    {
121
        $this->model = $model;
0 ignored issues
show
Documentation Bug introduced by
It seems like $model of type SilverStripe\ORM\DataObject is incompatible with the declared type PhpTek\Verifiable\DataObjectProvider of property $model.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
122
123
        return $this;
124
    }
125
126
    /**
127
     * Return an array of the fetched hash, a timestamp and everything else the current backend
128
     * gives us.
129
     *
130
     * @return array
131
     */
132
     public function read(string $hash) : array
0 ignored issues
show
Unused Code introduced by
The parameter $hash is not used and could be removed. ( Ignorable by Annotation )

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

132
     public function read(/** @scrutinizer ignore-unused */ string $hash) : array

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
133
     {
134
     }
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 array. 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...
135
136
}
137