Passed
Push — master ( 6f7a23...2cf817 )
by Thierry
02:29
created

DataBagPlugin::setDataBag()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 9
dl 0
loc 14
rs 9.9666
c 0
b 0
f 0
cc 3
nc 3
nop 0
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 Container
21
     */
22
    protected $di;
23
24
    /**
25
     * @var DataBag
26
     */
27
    protected $xDataBag = null;
28
29
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $di should have a doc-comment as per coding-style.
Loading history...
30
     * The constructor
31
     */
32
    public function __construct(Container $di)
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines before function; 1 found
Loading history...
33
    {
34
        $this->di = $di;
35
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
36
37
    /**
38
     * @return void
39
     */
40
    public function setDataBag()
41
    {
42
        if($this->xDataBag !== null)
43
        {
44
            return;
45
        }
46
47
        $xRequest = $this->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...
48
        $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...
49
        $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...
50
        $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...
51
            $this->readData($aBody['jxnbags'] ?? []) :
0 ignored issues
show
Coding Style introduced by
Expected 1 space after ":"; newline found
Loading history...
52
            $this->readData($aParams['jxnbags'] ?? []);
53
        $this->xDataBag = new DataBag($aData);
54
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
55
56
    /**
57
     * @inheritDoc
58
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
59
    public function getName(): string
60
    {
61
        return self::NAME;
62
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
63
64
    /**
65
     * @param mixed $xData
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
66
     *
67
     * @return array
68
     */
69
    private function readData($xData): array
70
    {
71
        // Todo: clean input data.
72
        if(is_string($xData))
73
        {
74
            return json_decode($xData, true) ?: [];
75
        }
76
        return is_array($xData) ? $xData : [];
77
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
78
79
    /**
80
     * @inheritDoc
81
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
82
    public function getHash(): string
83
    {
84
        // Use the version number as hash
85
        return '4.0.0';
86
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
87
88
    /**
89
     * @inheritDoc
90
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
91
    public function getReadyScript(): string
92
    {
93
        return '
94
    jaxon.command.handler.register("bags.set", function(args) {
95
        for (const bag in args.data) {
96
            jaxon.ajax.parameters.bags[bag] = args.data[bag];
97
        }
98
    });
99
';
100
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
101
102
    /**
103
     * @return void
104
     */
105
    public function writeCommand()
106
    {
107
        $this->setDataBag();
108
        if($this->xDataBag->touched())
109
        {
110
            $this->addCommand(['cmd' => 'bags.set'], $this->xDataBag->getAll());
111
        }
112
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
113
114
    /**
115
     * @param string $sName
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
116
     *
117
     * @return DataBagContext
118
     */
119
    public function bag(string $sName): DataBagContext
120
    {
121
        $this->setDataBag();
122
        return new DataBagContext($this->xDataBag, $sName);
123
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 0 found
Loading history...
124
}
125