Passed
Push — v5.x ( 8eee51...080647 )
by Thierry
10:22
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
    private function initDataBag()
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
        return is_string($xData) ?
0 ignored issues
show
Coding Style introduced by
Expected 1 space after "?"; newline found
Loading history...
73
            (json_decode($xData, true) ?: []) :
0 ignored issues
show
Coding Style introduced by
Expected 1 space after ":"; newline found
Loading history...
74
            (is_array($xData) ? $xData : []);
75
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
76
77
    /**
78
     * @inheritDoc
79
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
80
    public function getHash(): string
81
    {
82
        // Use the version number as hash
83
        return '4.0.0';
84
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
85
86
    /**
87
     * @return void
88
     */
89
    public function writeCommand()
90
    {
91
        $this->initDataBag();
92
        if($this->xDataBag->touched())
93
        {
94
            $this->addCommand('bags.set', ['bags' => $this->xDataBag]);
95
        }
96
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
97
98
    /**
99
     * @param string $sName
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
100
     *
101
     * @return DataBagContext
102
     */
103
    public function bag(string $sName): DataBagContext
104
    {
105
        $this->initDataBag();
106
        return new DataBagContext($this->xDataBag, $sName);
107
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 0 found
Loading history...
108
}
109