Test Failed
Push — main ( d7f1b4...976a1c )
by Guillaume
02:42
created

VueJSComponent   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 26
c 2
b 0
f 0
dl 0
loc 50
ccs 28
cts 28
cp 1
rs 10
wmc 9

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A generateGlobalScript() 0 5 1
A getName() 0 2 1
A generateLocalScript() 0 4 1
A onDeactivated() 0 2 1
A generateFile() 0 11 3
A onActivated() 0 2 1
1
<?php
2
namespace PHPMV;
3
4
use PHPMV\js\JavascriptUtils;
5
use PHPMV\utils\JsUtils;
6
7
class VueJSComponent extends AbstractVueJS{
8
    protected string $name;
9
    protected array $props;
10
    protected array $template;
11
12 2
    public function __construct(string $template, array $props = []) {
13 2
        parent::__construct();
14 2
        $this->props["props"] = $props;
15 2
        $this->template["template"] = "'".\str_replace(["\n","\r","\t"]," ",(\file_get_contents($template.'.html',true))."'");
16 2
        $this->name = $template;
17
    }
18 2
19 2
    public function generateLocalScript():string {
20 2
        $script = JavascriptUtils::arrayToJsObject($this->props + $this->components + $this->filters +$this->data + $this->computeds + $this->watchers = $this->hooks + $this->methods + $this->template);
21
        $script = JsUtils::cleanJSONFunctions($script);
22 1
        return $script;
23 1
    }
24 1
25
    public function generateGlobalScript():string {
26 1
        $script = "Vue.component('".$this->name."',";
27 1
        $script .= $this->generateLocalScript();
28 1
        $script .= ");\n";
29
        return $script;
30 2
    }
31 2
32 2
    public function generateFile($global = false):void {
33 2
        $script = $this->generateGlobalScript();
34 2
        if(!$global){
35 2
            \file_put_contents($this->name.".js",$script);
36 2
        }
37
        else{
38
            if(file_exists("components.js")){
39 2
                \file_put_contents("components.js",PHP_EOL . $script,FILE_APPEND);
40 1
            }
41
            else{
42
                \file_put_contents("components.js",$script);
43 1
            }
44
        }
45
    }
46
47 2
    public function onActivated(string $body):void {
48
        $this->addHook("activated", $body);
49
    }
50 2
51 2
    public function onDeactivated(string $body):void {
52
        $this->addHook("deactivated", $body);
53 1
    }
54
55
    public function getName():string {
56
        return $this->name;
57
    }
58
}