Passed
Push — master ( b34d93...aea763 )
by Thierry
03:02 queued 24s
created

DataBagPlugin   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 12
eloc 18
c 0
b 0
f 0
dl 0
loc 91
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 2
A getHash() 0 4 1
A getReadyScript() 0 3 1
A bag() 0 3 1
A getName() 0 3 1
A readData() 0 8 4
A writeCommand() 0 5 2
1
<?php
2
0 ignored issues
show
Coding Style introduced by
Missing file doc comment
Loading history...
3
namespace Jaxon\Plugin\Response\DataBag;
4
5
use Jaxon\Di\Container;
6
use Jaxon\Plugin\ResponsePlugin;
7
8
use function is_array;
9
use function is_string;
10
use function json_decode;
11
12
class DataBagPlugin extends ResponsePlugin
0 ignored issues
show
Coding Style introduced by
Missing doc comment for class DataBagPlugin
Loading history...
13
{
14
    /**
15
     * @const The plugin name
16
     */
17
    const NAME = 'bags';
18
19
    /**
20
     * @var DataBag
21
     */
22
    protected $xDataBag;
23
24
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $di should have a doc-comment as per coding-style.
Loading history...
25
     * The constructor
26
     */
27
    public function __construct(Container $di)
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines before function; 1 found
Loading history...
28
    {
29
        $xRequest = $di->getRequest();
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 7 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
30
        $aBody = $xRequest->getParsedBody();
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 10 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
31
        $aParams = $xRequest->getQueryParams();
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 8 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
32
        $aData = is_array($aBody) ?
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 10 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
Coding Style introduced by
Expected 1 space after "?"; newline found
Loading history...
33
            $this->readData($aBody['jxnbags'] ?? []) :
0 ignored issues
show
Coding Style introduced by
Expected 1 space after ":"; newline found
Loading history...
34
            $this->readData($aParams['jxnbags'] ?? []);
35
        $this->xDataBag = new DataBag($aData);
36
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
37
38
    /**
39
     * @inheritDoc
40
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
41
    public function getName(): string
42
    {
43
        return self::NAME;
44
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
45
46
    /**
47
     * @param mixed $xData
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
48
     *
49
     * @return array
50
     */
51
    private function readData($xData): array
52
    {
53
        // Todo: clean input data.
54
        if(is_string($xData))
55
        {
56
            return json_decode($xData, true) ?: [];
57
        }
58
        return is_array($xData) ? $xData : [];
59
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
60
61
    /**
62
     * @inheritDoc
63
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
64
    public function getHash(): string
65
    {
66
        // Use the version number as hash
67
        return '4.0.0';
68
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
69
70
    /**
71
     * @inheritDoc
72
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
73
    public function getReadyScript(): string
74
    {
75
        return '
76
    jaxon.command.handler.register("bags.set", function(args) {
77
        for (const bag in args.data) {
78
            jaxon.ajax.parameters.bags[bag] = args.data[bag];
79
        }
80
    });
81
';
82
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
83
84
    /**
85
     * @return void
86
     */
87
    public function writeCommand()
88
    {
89
        if($this->xDataBag->touched())
90
        {
91
            $this->addCommand(['cmd' => 'bags.set'], $this->xDataBag->getAll());
92
        }
93
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
94
95
    /**
96
     * @param string $sName
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
97
     *
98
     * @return DataBagContext
99
     */
100
    public function bag(string $sName): DataBagContext
101
    {
102
        return new DataBagContext($this->xDataBag, $sName);
103
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 0 found
Loading history...
104
}
105