Passed
Push — master ( 4c9478...3e3b54 )
by Russell
10:59
created

VerificationController::getVersionedRecord()   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 2
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\Controller;
9
10
use SilverStripe\Control\Controller;
11
use SilverStripe\Control\HTTPRequest;
12
use SilverStripe\Versioned\Versioned;
13
use SilverStripe\ORM\DataObject;
14
15
/**
16
 * Accepts incoming requests for data verification e.g. from within the CMS
17
 * or framework's admin area, and sends them on their way.
18
 *
19
 * Will proxy validation requests to the currently configured backend for both
20
 * {@link SiteTree} and {@link DataObject} subclasses.
21
 */
22
class VerificationController extends Controller
23
{
24
25
    /**
26
     * @var array
27
     */
28
    private static $allowed_actions = [
0 ignored issues
show
introduced by
The private property $allowed_actions is not used, and could be removed.
Loading history...
29
        'verify',
30
    ];
31
32
    /**
33
     * Responds to URIs of the following prototype: /verifiable/verify/<model>/<ID>
34
     * by echoing a JSON response for consumption by client-side logic.
35
     *
36
     * Also provides x2 extension points, both of which are passed a copy of
37
     * the contents of the managed record's "Proof" {@link ChainpointProof} "Proof"
38
     * field, an indirect {@link DBField} subclass.
39
     *
40
     * - onBeforeVerify()
41
     * - onAfterVerify()
42
     *
43
     * @param  HTTPRequest $request
44
     * @return void
45
     * @todo Update "Status" to be one of: "Pending" | "Verified" | "Failed"
46
     */
47
    public function verify(HTTPRequest $request)
48
    {
49
        $class = $request->param('ClassName');
50
        $id = $request->param('ModelID');
51
52
        if (empty($id) || !is_numeric($id) || empty($class)) {
53
            return $this->httpError(400, 'Bad request');
54
        }
55
56
        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

56
        if (!$record = $this->getVersionedRecord($class, /** @scrutinizer ignore-type */ $id)) {
Loading history...
57
            return $this->httpError(400, 'Bad request');
58
        }
59
60
        echo json_encode([
61
            'ID' => "$record->ID",
62
            'Class' => get_class($record),
63
            'Status' => $this->verifiableService->getVerificationStatus($record),
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...
Bug introduced by
The method getVerificationStatus() 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

63
            'Status' => $this->verifiableService->/** @scrutinizer ignore-call */ getVerificationStatus($record),

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...
64
        ], JSON_UNESCAPED_UNICODE);
65
    }
66
67
    /**
68
     * Fetch a record directly from the relevant table, for the given class
69
     * and ID.
70
     *
71
     * @param  string     $class A fully-qualified PHP class name.
72
     * @param  int        $id    The RecordID of the desired Versioned record.
73
     * @return DataObject
74
     * @todo Add an instanceof DataObject check to prevent "SiteTree" being passed for example
75
     */
76
    private function getVersionedRecord(string $class, int $id) : DataObject
77
    {
78
        return Versioned::get_latest_version($class, $id);
79
    }
80
81
}
82