CerbereHackedListener::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Drush Cerbere command line tools.
5
 * Copyright (C) 2015 - Sebastien Malot <[email protected]>
6
 *
7
 * This program is free software; you can redistribute it and/or modify
8
 * it under the terms of the GNU General Public License as published by
9
 * the Free Software Foundation; either version 2 of the License, or
10
 * (at your option) any later version.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU General Public License along
18
 * with this program; if not, write to the Free Software Foundation, Inc.,
19
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20
 */
21
22
namespace Cerbere\Event;
23
24
use Cerbere\Model\Hacked\HackedProject;
25
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
26
27
/**
28
 * Class CerbereHackedListener
29
 *
30
 * @package Cerbere\Event
31
 */
32
class CerbereHackedListener implements EventSubscriberInterface
33
{
34
    /**
35
     */
36
    public function __construct()
37
    {
38
    }
39
40
    /**
41
     * Returns an array of event names this subscriber wants to listen to.
42
     *
43
     * The array keys are event names and the value can be:
44
     *
45
     *  * The method name to call (priority defaults to 0)
46
     *  * An array composed of the method name to call and the priority
47
     *  * An array of arrays composed of the method names to call and respective
48
     *    priorities, or 0 if unset
49
     *
50
     * For instance:
51
     *
52
     *  * array('eventName' => 'methodName')
53
     *  * array('eventName' => array('methodName', $priority))
54
     *  * array('eventName' => array(array('methodName1', $priority), array('methodName2'))
55
     *
56
     * @return array The event names to listen to
57
     */
58
    public static function getSubscribedEvents()
59
    {
60
        return array(
61
          CerbereEvents::CERBERE_REPORT_ACTION => array('onReportAction', 0),
62
        );
63
    }
64
65
    /**
66
     * @param CerbereReportActionEvent $event
67
     */
68
    public function onReportAction(CerbereReportActionEvent $event)
69
    {
70
        $current_dir = getcwd();
71
        
72
        try {
73
            // Change current directory to the module directory.
74
            $workingDirectory = $event->getProject()->getWorkingDirectory();
75
            if (!is_dir($workingDirectory)) {
76
                throw new \Exception('Missing folder');
77
            }
78
            @chdir($workingDirectory);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
79
80
            $hacked = new HackedProject($event->getProject());
81
            $result = $hacked->computeReport();
82
        } catch (\Exception $e) {
83
            // Todo: log error.
84
            $result = array();
85
        }
86
87
        $result += array(
88
          'status' => HackedProject::STATUS_UNCHECKED,
89
          'counts' => array(),
90
        );
91
92
        // Restore previous directory.
93
        chdir($current_dir);
94
95
        $report  = $event->getReport();
96
        $options = $event->getOptions();
97
98
        // Alter report.
99
        if (!empty($options['flat'])) {
100
            $report['hacked_status'] = $result['status'];
101
            $report['hacked_label']  = HackedProject::getStatusLabel($result['status']);
102
        } else {
103
            $report['hacked'] = array(
104
              'status' => $result['status'],
105
              'status_label' => HackedProject::getStatusLabel($result['status']),
106
              'counts' => $result['counts'],
107
            );
108
        }
109
110
        $event->setReport($report);
111
    }
112
}
113