SandboxView   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Importance

Changes 11
Bugs 1 Features 2
Metric Value
wmc 2
eloc 7
c 11
b 1
f 2
dl 0
loc 29
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A init() 0 8 2
1
<?php
2
3
namespace nystudio107\crafttwigsandbox\web;
4
5
use Craft;
6
use craft\web\View;
7
use nystudio107\crafttwigsandbox\console\SandboxErrorHandler as ConsoleSandboxErrorHandler;
8
use nystudio107\crafttwigsandbox\twig\BlacklistSecurityPolicy;
9
use nystudio107\crafttwigsandbox\web\SandboxErrorHandler as WebSandboxErrorHandler;
10
use Twig\Extension\SandboxExtension;
11
use Twig\Sandbox\SecurityPolicyInterface;
12
13
class SandboxView extends View
14
{
15
    // Public Properties
16
    // =========================================================================
17
18
    /**
19
     * @var SecurityPolicyInterface|null The security policy to use for the SandboxView
20
     */
21
    public $securityPolicy = null;
22
23
    /**
24
     * @var WebSandboxErrorHandler|ConsoleSandboxErrorHandler|null The error handler to use for the SandboxView
25
     */
26
    public $sandboxErrorHandler = null;
27
28
    // Public Methods
29
    // =========================================================================
30
31
    /**
32
     * @inheritDoc
33
     */
34
    public function init(): void
35
    {
36
        parent::init();
37
        $this->sandboxErrorHandler = Craft::$app->getRequest()->getIsConsoleRequest() ? new ConsoleSandboxErrorHandler() : new WebSandboxErrorHandler();
38
        // Use the passed in SecurityPolicy, or create a default security policy
39
        $this->securityPolicy = $this->securityPolicy ?? new BlacklistSecurityPolicy();
40
        // Add the SandboxExtension with our SecurityPolicy lazily via ::registerTwigExtension()
41
        $this->registerTwigExtension(new SandboxExtension($this->securityPolicy, true));
42
    }
43
}
44