FileExecutor::execute()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 0
cts 6
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 6
nc 1
nop 3
crap 2
1
<?php declare(strict_types=1);
2
3
/*
4
 * This file is part of the pinepain/js-sandbox PHP library.
5
 *
6
 * Copyright (c) 2016-2017 Bogdan Padalko <[email protected]>
7
 *
8
 * Licensed under the MIT license: http://opensource.org/licenses/MIT
9
 *
10
 * For the full copyright and license information, please view the
11
 * LICENSE file that was distributed with this source or visit
12
 * http://opensource.org/licenses/MIT
13
 */
14
15
16
namespace Pinepain\JsSandbox\Executors;
17
18
19
use League\Flysystem\FilesystemInterface;
20
use League\Flysystem\Util;
21
use V8\Context;
22
use V8\Isolate;
23
use V8\Script;
24
use V8\ScriptOrigin;
25
use V8\StringValue;
26
use V8\Value;
27
28
29
class FileExecutor implements ExecutorInterface
30
{
31
    /**
32
     * @var FilesystemInterface
33
     */
34
    private $filesystem;
35
36
    public function __construct(FilesystemInterface $filesystem)
37
    {
38
        $this->filesystem = $filesystem;
39
    }
40
41
    /**
42
     * {@inheritdoc}
43
     */
44
    public function execute(Isolate $isolate, Context $context, string $value): Value
45
    {
46
        $path = Util::normalizePath($value);
47
48
        $source = new StringValue($isolate, $this->filesystem->read($path));
49
        $origin = new ScriptOrigin($path);
50
        $script = new Script($context, $source, $origin);
51
52
        return $script->run($context);
53
    }
54
55
}
56