IndexController::indexAction()   B
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 34
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 3 Features 0
Metric Value
c 3
b 3
f 0
dl 0
loc 34
rs 8.5806
cc 4
eloc 20
nc 4
nop 0
1
<?php
2
3
/**
4
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
5
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
6
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
7
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
8
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
9
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
10
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
11
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
12
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
13
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
14
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
15
 *
16
 * This software consists of voluntary contributions made by many individuals
17
 * and is licensed under the MIT license.
18
 */
19
namespace LearnZF2Log\Controller;
20
21
use Zend\Form\FormInterface;
22
use Zend\Log\Logger;
23
use Zend\Mvc\Controller\AbstractActionController;
24
use Zend\View\Model\ViewModel;
25
26
/**
27
 * @author Abdul Malik Ikhsan <[email protected]>
28
 */
29
class IndexController extends AbstractActionController
30
{
31
    /**
32
     * @var array
33
     */
34
    protected $loggerConfig = [
35
        'writers' => [
36
            [
37
                'name' => 'stream',
38
                'options' => [
39
                    'stream' => 'php://output',
40
                    'formatter' => [
41
                        'name' => 'simple',
42
                        'options' => [
43
                            'format' => '%timestamp% %priorityName% (%priority%): %message%',
44
                        ],
45
                    ],
46
                ],
47
            ],
48
        ],
49
    ];
50
51
    /**
52
     * @var int
53
     */
54
    protected $loggerPriority = Logger::EMERG;
55
56
    /**
57
     * @var FormInterface
58
     */
59
    protected $form;
60
61
    public function __construct(FormInterface $form)
62
    {
63
        $this->form = $form;
64
    }
65
66
    public function indexAction()
67
    {
68
        $request = $this->getRequest();
69
70
        $logContent = '';
0 ignored issues
show
Unused Code introduced by
$logContent is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
71
72
        // initialize when no submit anymore
73
        $data = [];
74
        $data['logmessage'] = $this->form->get('logmessage')->getValue();
75
        if ($request->isPost()) {
76
            $this->form->setData($request->getPost());
77
            if ($this->form->isValid()) {
78
                $data = $this->form->getData();
79
80
                $this->loggerPriority = $data['logpriority'];
81
                if ($data['logformat'] !== 'simple') {
82
                    $this->loggerConfig['writers'][0]['options']['formatter']['name'] = $data['logformat'];
83
                    unset($this->loggerConfig['writers'][0]['options']['formatter']['options']);
84
                }
85
            }
86
        }
87
88
        $logger = new Logger($this->loggerConfig);
89
90
        // save log data to buffer and make it variable
91
        ob_start();
92
        $logger->log((int) $this->loggerPriority, $data['logmessage']);
93
        $logContent = ob_get_clean();
94
95
        return new ViewModel([
96
            'form' => $this->form,
97
            'logContent' => $logContent,
98
        ]);
99
    }
100
}
101