Baracoa::getSsrCode()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 6
cts 6
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 9
nc 1
nop 3
crap 1
1
<?php
2
/**
3
 * This file is part of the Koriym.Baracoa package.
4
 *
5
 * @license http://opensource.org/licenses/MIT MIT
6
 */
7
namespace Koriym\Baracoa;
8
9
use Koriym\Baracoa\Exception\JsFileNotExistsException;
10
use Nacmartin\PhpExecJs\PhpExecJs;
11
use V8Js;
12
13
final class Baracoa implements BaracoaInterface
14
{
15
    /**
16
     * @var string
17
     */
18
    private $bundleSrcBasePath;
19
20
    /**
21
     * @var ExceptionHandler
22
     */
23
    private $handler;
24
25
    /*
26
     * @var V8Js
27
     */
28
    private $v8;
29
30
    private $execJs;
31
32
    /**
33
     * @param string                    $bundleSrcBasePath Bundled application code base dir path
34
     * @param ExceptionHandlerInterface $handler           V8JsScriptException exception handler
35
     * @param V8Js                      $v8Js              V8Js exception handler
36
     */
37 5
    public function __construct(string $bundleSrcBasePath, ExceptionHandlerInterface $handler, V8Js $v8Js = null)
38
    {
39 5
        $this->bundleSrcBasePath = $bundleSrcBasePath;
40 5
        $this->handler = $handler;
0 ignored issues
show
Documentation Bug introduced by
$handler is of type object<Koriym\Baracoa\ExceptionHandlerInterface>, but the property $handler was declared to be of type object<Koriym\Baracoa\ExceptionHandler>. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
41 5
        $this->v8 = $v8Js;
42 5
        if ($v8Js === null) {
43 2
            $this->execJs = new PhpExecJs();
44
        }
45 5
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50 5
    public function render(string $appName, array $store, array $metas = []) : string
51
    {
52 5
        $bundleSrcPath = sprintf('%s/%s.bundle.js', $this->bundleSrcBasePath, $appName);
53 5
        if (! file_exists($bundleSrcPath)) {
54 1
            throw new JsFileNotExistsException($bundleSrcPath);
55
        }
56 4
        $bundleSrc = file_get_contents($bundleSrcPath);
57 4
        $code = $this->getSsrCode($bundleSrc, $store, $metas);
58
        try {
59 4
            $html = $this->execCode($code);
60 1
        } catch (\V8JsScriptException $e) {
61 1
            $handler = $this->handler;
62 1
            $html = $handler($e);
63
        }
64
65 3
        return $html;
66
    }
67
68 4
    private function execCode(string $code) : string
69
    {
70 4
        if ($this->v8 instanceof V8Js) {
71 2
            return (string) $this->v8->executeString($code);
72
        }
73
74 2
        return (string) $this->execJs->evalJs($code);
75
    }
76
77 4
    private function getSsrCode($bundleSrc, array $store, array $metas) : string
78
    {
79 4
        $storeJson = json_encode($store);
80 4
        $metasJson = json_encode($metas);
81
        $code = <<< "EOT"
82
var console = {warn: function(){}, error: function(){}};
83
var global = global || this, self = self || this, window = window || this;
84 4
{$bundleSrc}
85 4
render($storeJson, $metasJson);
86
EOT;
87
88 4
        return $code;
89
    }
90
}
91