Completed
Push — master ( c60cf3...36f595 )
by Sebastien
07:49
created

Hacked::process()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 51
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 6
Bugs 0 Features 1
Metric Value
c 6
b 0
f 1
dl 0
loc 51
rs 8.8983
cc 4
eloc 31
nc 4
nop 2

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\Action;
23
24
use Cerbere\Event\CerbereDoActionEvent;
25
use Cerbere\Event\CerbereDoneActionEvent;
26
use Cerbere\Event\CerbereEvents;
27
use Cerbere\Event\CerbereReportActionEvent;
28
use Cerbere\Model\Hacked\HackedProject;
29
use Cerbere\Model\Project;
30
use Cerbere\Model\ReleaseHistory;
31
use Symfony\Component\EventDispatcher\EventDispatcher;
32
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
33
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
34
35
/**
36
 * Class Hacked
37
 *
38
 * @package Cerbere\Action
39
 */
40
class Hacked implements ActionInterface
41
{
42
    /**
43
     * @inheritDoc
44
     */
45
    public function getCode()
46
    {
47
        return 'hacked';
48
    }
49
50
    /**
51
     * @param EventSubscriberInterface $listener
52
     */
53
    public function addLoggerListener(EventSubscriberInterface $listener)
54
    {
55
        $this->getDispatcher()->addSubscriber($listener);
56
    }
57
58
    /**
59
     * @inheritDoc
60
     */
61
    public function getDispatcher()
62
    {
63
        if (!isset($this->dispatcher)) {
64
            $this->dispatcher = new EventDispatcher();
0 ignored issues
show
Bug introduced by
The property dispatcher does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
65
        }
66
67
        return $this->dispatcher;
68
    }
69
70
    /**
71
     * @inheritDoc
72
     */
73
    public function setDispatcher(EventDispatcherInterface $dispatcher)
74
    {
75
        $this->dispatcher = $dispatcher;
76
    }
77
78
    /**
79
     * @inheritDoc
80
     */
81
    public function prepare()
82
    {
83
84
    }
85
86
    /**
87
     * @inheritDoc
88
     */
89
    public function process(array $projects, $options = array())
90
    {
91
        if (empty($projects)) {
92
            return array();
93
        }
94
95
        $reports = array();
96
        $release_history = new ReleaseHistory();
97
98
        /** @var Project $project */
99
        foreach ($projects as $project) {
100
            $release_history->prepare($project);
101
102
            $event = new CerbereDoActionEvent($this, $project);
103
            $this->getDispatcher()->dispatch(CerbereEvents::CERBERE_DO_ACTION, $event);
104
105
            if ($filename = $project->getFilename()) {
106
                $current_dir = getcwd();
107
                // Change current directory to the module directory.
108
                chdir(dirname($filename));
109
110
                $hacked = new HackedProject($project);
111
                $result = $hacked->computeReport();
112
113
                $report = array(
114
                  'project' => $project->getProject(),
115
                  'type' => $project->getProjectType(),
116
                  'version' => $project->getVersion(),
117
                  'version_date' => $project->getDatestamp(),
118
                  'status' => $result['status'],
119
                  'status_label' => HackedProject::getStatusLabel($result['status']),
120
                  'modified' => $result['counts']['different'],
121
                  'deleted' => $result['counts']['missing'],
122
                );
123
124
                $event = new CerbereReportActionEvent($this, $project, $report);
125
                $this->getDispatcher()->dispatch(CerbereEvents::CERBERE_REPORT_ACTION, $event);
126
                $report = $event->getReport();
127
128
                $reports[] = $report;
129
130
                // Restore current directory.
131
                chdir($current_dir);
132
            }
133
134
            $event = new CerbereDoneActionEvent($this, $project);
135
            $this->getDispatcher()->dispatch(CerbereEvents::CERBERE_DONE_ACTION, $event);
136
        }
137
138
        return $reports;
139
    }
140
}
141