Passed
Pull Request — master (#106)
by Guy
01:58
created

BaseHandlerTrait::getStore()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 9
c 0
b 0
f 0
rs 10
cc 3
nc 3
nop 0
1
<?php
2
3
namespace SilverStripe\MFA\RequestHandler;
4
5
use SilverStripe\Control\HTTPResponse;
6
use SilverStripe\Core\Injector\Injector;
7
use SilverStripe\MFA\Service\MethodRegistry;
8
use SilverStripe\MFA\Service\RegisteredMethodManager;
9
use SilverStripe\MFA\Store\StoreInterface;
10
use SilverStripe\Security\Member;
11
use SilverStripe\View\Requirements;
12
13
trait BaseHandlerTrait
14
{
15
    /**
16
     * Perform the necessary "Requirements" calls to ensure client side scripts are available in the response
17
     */
18
    protected function applyRequirements(): void
19
    {
20
        // Run through requirements
21
        Requirements::javascript('silverstripe/mfa: client/dist/js/injector.js');
22
        Requirements::javascript('silverstripe/admin: client/dist/js/i18n.js');
23
        Requirements::javascript('silverstripe/mfa: client/dist/js/bundle.js');
24
        Requirements::add_i18n_javascript('silverstripe/mfa: client/lang');
0 ignored issues
show
Deprecated Code introduced by
The function SilverStripe\View\Requir...::add_i18n_javascript() has been deprecated. ( Ignorable by Annotation )

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

24
        /** @scrutinizer ignore-deprecated */ Requirements::add_i18n_javascript('silverstripe/mfa: client/lang');
Loading history...
25
        Requirements::css('silverstripe/mfa: client/dist/styles/bundle.css');
26
27
        // Plugin module requirements
28
        foreach (MethodRegistry::singleton()->getMethods() as $method) {
29
            $method->applyRequirements();
30
        }
31
    }
32
33
    /**
34
     * @return StoreInterface|null
35
     */
36
    protected function getStore(): ?StoreInterface
37
    {
38
        if (!$this->store) {
39
            $spec = Injector::inst()->getServiceSpec(StoreInterface::class);
40
            $class = is_string($spec) ? $spec : $spec['class'];
41
            $this->store = call_user_func([$class, 'load'], $this->getRequest());
0 ignored issues
show
Bug introduced by
It seems like getRequest() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

41
            $this->store = call_user_func([$class, 'load'], $this->/** @scrutinizer ignore-call */ getRequest());
Loading history...
Bug Best Practice introduced by
The property store does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
42
        }
43
44
        return $this->store;
45
    }
46
47
    /**
48
     * @param Member $member
49
     * @return StoreInterface
50
     */
51
    protected function createStore(Member $member): StoreInterface
52
    {
53
        $store = Injector::inst()->create(StoreInterface::class, $member);
54
        // Ensure use of the getter returns the new store
55
        $this->store = $store;
0 ignored issues
show
Bug Best Practice introduced by
The property store does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
56
        return $store;
57
    }
58
}
59