Passed
Pull Request — master (#118)
by Guy
02:37
created

BaseHandlerTrait::setStore()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace SilverStripe\MFA\RequestHandler;
4
5
use SilverStripe\Core\Injector\Injector;
6
use SilverStripe\MFA\Service\MethodRegistry;
7
use SilverStripe\MFA\Store\StoreInterface;
8
use SilverStripe\Security\Member;
9
use SilverStripe\View\Requirements;
10
11
trait BaseHandlerTrait
12
{
13
    /**
14
     * A "session store" object that helps contain MFA specific session detail
15
     *
16
     * @var StoreInterface
17
     */
18
    protected $store;
19
20
    /**
21
     * Perform the necessary "Requirements" calls to ensure client side scripts are available in the response
22
     *
23
     * @param bool $frontEndRequirements Indicates dependencies usually provided by admin should also be required
24
     */
25
    protected function applyRequirements(bool $frontEndRequirements = true): void
26
    {
27
        // Run through requirements
28
        if ($frontEndRequirements) {
29
            Requirements::javascript('silverstripe/mfa: client/dist/js/injector.js');
30
            Requirements::javascript('silverstripe/admin: client/dist/js/i18n.js');
31
        }
32
33
        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

33
        /** @scrutinizer ignore-deprecated */ Requirements::add_i18n_javascript('silverstripe/mfa: client/lang');
Loading history...
34
35
        $suffix = $frontEndRequirements ? '' : '-cms';
36
        Requirements::javascript("silverstripe/mfa: client/dist/js/bundle{$suffix}.js");
37
        Requirements::css("silverstripe/mfa: client/dist/styles/bundle{$suffix}.css");
38
39
        // Plugin module requirements
40
        foreach (MethodRegistry::singleton()->getMethods() as $method) {
41
            $method->applyRequirements();
42
        }
43
    }
44
45
    /**
46
     * @return StoreInterface|null
47
     */
48
    protected function getStore(): ?StoreInterface
49
    {
50
        if (!$this->store) {
51
            $spec = Injector::inst()->getServiceSpec(StoreInterface::class);
52
            $class = is_string($spec) ? $spec : $spec['class'];
53
            $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

53
            $this->store = call_user_func([$class, 'load'], $this->/** @scrutinizer ignore-call */ getRequest());
Loading history...
54
        }
55
56
        return $this->store;
57
    }
58
59
    /**
60
     * @param StoreInterface $store
61
     * @return $this
62
     */
63
    public function setStore(StoreInterface $store): self
64
    {
65
        $this->store = $store;
66
        return $this;
67
    }
68
69
    /**
70
     * @param Member $member
71
     * @return StoreInterface
72
     */
73
    protected function createStore(Member $member): StoreInterface
74
    {
75
        $store = Injector::inst()->create(StoreInterface::class, $member);
76
        // Ensure use of the getter returns the new store
77
        $this->store = $store;
78
        return $store;
79
    }
80
}
81