Passed
Push — master ( fcfa5b...4c9478 )
by Russell
09:54
created

VerificationController::verificationResponse()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 2
dl 0
loc 9
rs 9.6666
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\Controller;
9
10
use SilverStripe\Control\Controller;
11
use SilverStripe\Control\HTTPRequest;
12
use SilverStripe\Versioned\Versioned;
13
use SilverStripe\CMS\Model\SiteTree;
0 ignored issues
show
Bug introduced by
The type SilverStripe\CMS\Model\SiteTree 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 SilverStripe\ORM\DataObject;
15
16
/**
17
 * Accepts incoming requests for data verification e.g. from within the CMS
18
 * or framework's admin area, and sends them on their way.
19
 *
20
 * Will proxy validation requests to the currently configured backend for both
21
 * {@link SiteTree} and {@link DataObject} subclasses.
22
 */
23
class VerificationController extends Controller
24
{
25
    /**
26
     * @config
27
     * @var array
28
     */
29
    private static $allowed_actions = [
0 ignored issues
show
introduced by
The private property $allowed_actions is not used, and could be removed.
Loading history...
30
        'page',
31
        'model',
32
    ];
33
34
    /**
35
     * Verify a page: /verify/page/<ID> by echoing a JSON response for
36
     * consumption by client-side logic.
37
     *
38
     * @param  HTTPRequest $request
39
     * @return void
40
     */
41
    public function page(HTTPRequest $request)
42
    {
43
        $id = $request->param('ID');
44
45
        if (empty($id) || !is_numeric($id)) {
46
            return $this->httpError(400, 'Bad request');
47
        }
48
49
        if (!$record = $this->getVersionedRecord(SiteTree::class, $id)) {
0 ignored issues
show
Bug introduced by
$id of type string is incompatible with the type integer expected by parameter $id of PhpTek\Verifiable\Contro...r::getVersionedRecord(). ( Ignorable by Annotation )

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

49
        if (!$record = $this->getVersionedRecord(SiteTree::class, /** @scrutinizer ignore-type */ $id)) {
Loading history...
50
            return false;
0 ignored issues
show
Bug Best Practice introduced by
The expression return false returns the type false which is incompatible with the documented return type void.
Loading history...
51
        }
52
53
        $proof = $record->dbObject('Proof');
54
        $result = json_decode($this->verifiableService->read($proof->getHashIdNode()), true);
0 ignored issues
show
Bug introduced by
The method getHashIdNode() does not exist on SilverStripe\ORM\FieldType\DBField. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

54
        $result = json_decode($this->verifiableService->read($proof->/** @scrutinizer ignore-call */ getHashIdNode()), true);
Loading history...
Bug Best Practice introduced by
The property verifiableService does not exist on PhpTek\Verifiable\Contro...\VerificationController. Since you implemented __get, consider adding a @property annotation.
Loading history...
Bug introduced by
The method read() does not exist on null. ( Ignorable by Annotation )

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

54
        $result = json_decode($this->verifiableService->/** @scrutinizer ignore-call */ read($proof->getHashIdNode()), true);

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...
55
56
        echo $this->verificationResponse($record, $result);
57
    }
58
59
    /**
60
     * Verify a data model: /verify/model/<Class>/<ID> by echoing a JSON response for
61
     * consumption by client-side logic.
62
     *
63
     * @param  HTTPRequest $request
64
     * @return void
65
     */
66
    public function model(HTTPRequest $request)
67
    {
68
        $class = $request->param('Class');
69
        $id = $request->param('ID');
70
71
        if (empty($id) || !is_numeric($id) || empty($class)) {
72
            return $this->httpError(400, 'Bad request');
73
        }
74
75
        if (!$record = $this->getVersionedRecord($class, $id)) {
0 ignored issues
show
Bug introduced by
$id of type string is incompatible with the type integer expected by parameter $id of PhpTek\Verifiable\Contro...r::getVersionedRecord(). ( Ignorable by Annotation )

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

75
        if (!$record = $this->getVersionedRecord($class, /** @scrutinizer ignore-type */ $id)) {
Loading history...
76
            return false;
0 ignored issues
show
Bug Best Practice introduced by
The expression return false returns the type false which is incompatible with the documented return type void.
Loading history...
77
        }
78
79
        $proof = $record->dbObject('Proof');
80
        $result = json_decode($this->verifiableService->read($proof->getHashIdNode()), true);
0 ignored issues
show
Bug Best Practice introduced by
The property verifiableService does not exist on PhpTek\Verifiable\Contro...\VerificationController. Since you implemented __get, consider adding a @property annotation.
Loading history...
81
82
        echo $this->verificationResponse($record, $result);
83
    }
84
85
    /**
86
     * Fetch a record directly from the relevant table, for the given class
87
     * and ID.
88
     *
89
     * @param  string     $class A fully-qualified PHP class name.
90
     * @param  int        $id    The RecordID of the desired Versioned record.
91
     * @return DataObject
92
     */
93
    private function getVersionedRecord(string $class, int $id) : DataObject
94
    {
95
        return Versioned::get_latest_version($class, $id);
96
    }
97
98
    /**
99
     * Return an JSON representation of the verification result for internal
100
     * use.
101
     *
102
     * @param  DataObject $record
103
     * @param  string     $result
104
     * @return string
105
     */
106
    private function verificationResponse($record, $result)
107
    {
108
        $isVerified = $record->verify($result, false) ? 'true' : 'false';
0 ignored issues
show
Bug introduced by
The method verify() does not exist on SilverStripe\ORM\DataObject. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

108
        $isVerified = $record->/** @scrutinizer ignore-call */ verify($result, false) ? 'true' : 'false';
Loading history...
109
110
        return json_encode([
111
            'ID' => "$record->ID",
112
            'Class' => get_class($record),
113
            'IsVerified' => $isVerified,
114
        ], JSON_UNESCAPED_UNICODE);
115
    }
116
117
}
118