Completed
Push — 2.1 ( c23fe5...78dbd5 )
by David
14s
created

PatchController::displayNotificationMessage()   D

Complexity

Conditions 10
Paths 8

Size

Total Lines 28
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 28
rs 4.8196
c 0
b 0
f 0
cc 10
eloc 18
nc 8
nop 2

How to fix   Complexity   

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
namespace Mouf\Utils\Patcher\Controllers;
3
4
use Mouf\Controllers\AbstractMoufInstanceController;
5
6
use Mouf\Database\TDBM\Utils\TDBMDaoGenerator;
7
8
use Mouf\Html\Widgets\MessageService\Service\UserMessageInterface;
9
use Mouf\MoufManager;
10
11
use Mouf\Mvc\Splash\Controllers\Controller;
12
13
use Mouf\Reflection\MoufReflectionProxy;
14
15
use Mouf\Html\HtmlElement\HtmlBlock;
16
use Mouf\InstanceProxy;
17
use Mouf\Utils\Patcher\PatchException;
18
use Mouf\Utils\Patcher\PatchInterface;
19
use Mouf\Utils\Patcher\PatchService;
20
21
/**
22
 * The controller to track which patchs have been applied.
23
24
 */
25
class PatchController extends AbstractMoufInstanceController {
26
	
27
	/**
28
	 *
29
	 * @var HtmlBlock
30
	 */
31
	public $content;
32
	
33
	/**
34
	 * A list of patches returned by the getView method of the PatchService. 
35
	 * @var array
36
	 */
37
	protected $patchesArray;
38
	
39
	protected $nbAwaiting = 0;
40
	protected $nbError = 0;
41
42
	protected $nbPatchesByType = [];
43
	protected $action;
44
	
45
	/**
46
	 * Page listing the patches to be applied.
47
	 *
48
	 * @Action
49
	 * @Logged
50
	 */
51
	public function defaultAction($name, $selfedit="false") {
52
		$this->initController($name, $selfedit);
53
		
54
		$patchService = new InstanceProxy($name, $selfedit == "true");
55
		$this->patchesArray = $patchService->getView();
56
		
57
		foreach ($this->patchesArray as $patch) {
58
			if ($patch['status'] == PatchInterface::STATUS_AWAITING) {
59
				$this->nbAwaiting++;
60
			} elseif ($patch['status'] == PatchInterface::STATUS_ERROR) {
61
				$this->nbError++;
62
			}
63
		}
64
		
65
		$this->content->addFile(__DIR__."/../../../../views/patchesList.php", $this);
66
		$this->template->toHtml();
67
	}
68
	
69
70
	/**
71
	 * Runs a patch.
72
	 * 
73
	 * @Action
74
	 * @Logged
75
	 * @param string $name
76
	 * @param string $uniqueName
77
	 * @param string $action
78
	 */
79
	public function runPatch($name, $uniqueName, $action, $selfedit) {
80
		
81
		$patchService = new InstanceProxy($name, $selfedit == "true");
82
		
83
		try {
84
		
85
			if ($action == 'apply') {
86
				$this->patchesArray = $patchService->apply($uniqueName);
87
			} else if ($action == 'revert') {
88
				$this->patchesArray = $patchService->revert($uniqueName);
89
			} else if ($action == 'skip') {
90
				$this->patchesArray = $patchService->skip($uniqueName);
91
			} else {
92
				throw new PatchException("Unknown action: '".$action."'");
93
			}
94
		} catch (\Exception $e) {
95
			$htmlMessage = "An error occured while applying the patch: ".$e->getMessage();
96
			set_user_message($htmlMessage);
97
		}
98
99
		header('Location: .?name='.urlencode($name));
100
	}
101
102
    /**
103
     * Displays the page to select the patch types to be applied.
104
     *
105
     * @Action
106
     * @Logged
107
     * @param string $name
108
     * @param string $selfedit
109
     * @param string $action One of "reset" or "apply"
110
     */
111
    public function runAllPatches($name, $selfedit, $action) {
112
        $this->initController($name, $selfedit);
113
114
        $patchService = new InstanceProxy($name, $selfedit == "true");
115
        $this->patchesArray = $patchService->getView();
116
117
        $types = $patchService->_getSerializedTypes();
118
119
        foreach ($types as $type) {
120
            $this->nbPatchesByType[$type['name']] = 0;
121
        }
122
123
        $nbNoneDefaultPatches = 0;
124
125
        foreach ($this->patchesArray as $patch) {
126
            if ($action === 'reset' || $patch['status'] === PatchInterface::STATUS_AWAITING || $patch['status'] === PatchInterface::STATUS_ERROR) {
127
                $type = $patch['patch_type'];
128
                if ($type !== '') {
129
                    $nbNoneDefaultPatches++;
130
                }
131
                $this->nbPatchesByType[$type]++;
132
            }
133
        }
134
135
        // If all patches to be applied are default patches, let's do this right now.
136
        if ($nbNoneDefaultPatches === 0) {
137
            $this->applyAllPatches($name, [''], $action, $selfedit);
138
            return;
139
        }
140
141
        ksort($this->nbPatchesByType);
142
143
        $this->action = $action;
144
145
        // Otherwise, let's display a screen to select the patch types to be applied.
146
        $this->content->addFile(__DIR__."/../../../../views/applyPatches.php", $this);
147
        $this->template->toHtml();
148
    }
149
150
151
    /**
152
     * Runs all patches in a row.
153
     *
154
     * @Action
155
     * @Logged
156
     * @param string $name
157
     * @param string[] $types
158
     * @param string $action One of "reset" or "apply"
159
     * @param string $selfedit
160
     */
161
	public function applyAllPatches($name, array $types, $action, $selfedit) {
162
		$patchService = new InstanceProxy($name, $selfedit == "true");
163
		/* @var $patchService PatchService */
164
165
        if ($action === 'reset') {
166
            $patchService->reset();
167
        }
168
        try {
169
170
            [
171
                'applied' => $appliedPatchArray,
0 ignored issues
show
Bug introduced by
The variable $appliedPatchArray does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
172
                'skipped' => $skippedPatchArray
0 ignored issues
show
Bug introduced by
The variable $skippedPatchArray does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
173
            ] = $patchService->applyAll($types);
174
175
		} catch (\Exception $e) {
176
			$htmlMessage = "An error occured while applying the patch: ".$e->getMessage();
177
			set_user_message($htmlMessage);
178
		}
179
180
        $this->displayNotificationMessage($appliedPatchArray, $skippedPatchArray);
181
182
        header('Location: .?name='.urlencode($name));
183
	}
184
185
	private function displayNotificationMessage(array $appliedPatchArray, array $skippedPatchArray)
186
    {
187
        $nbPatchesApplied = array_sum($appliedPatchArray);
188
        $nbPatchesSkipped = array_sum($skippedPatchArray);
189
        $msg = '';
190
        if ($nbPatchesApplied !== 0) {
191
            $patchArr = [];
192
            foreach ($appliedPatchArray as $name => $number) {
193
                $name = $name ?: 'default';
194
                $patchArr[] = plainstring_to_htmlprotected($name).': '.$number;
195
            }
196
197
            $msg .= sprintf('%d patch%s applied (%s)', $nbPatchesApplied, ($nbPatchesApplied > 1)?'es':'', implode(', ', $patchArr));
198
        }
199
        if ($nbPatchesSkipped !== 0) {
200
            $patchArr = [];
201
            foreach ($skippedPatchArray as $name => $number) {
202
                $name = $name ?: 'default';
203
                $patchArr[] = plainstring_to_htmlprotected($name).': '.$number;
204
            }
205
206
            $msg .= sprintf('%d patch%s skipped (%s)', $nbPatchesSkipped, ($nbPatchesSkipped > 1)?'es':'', implode(', ', $patchArr));
207
        }
208
209
        if ($msg !== '') {
210
            set_user_message($msg, UserMessageInterface::SUCCESS);
211
        }
212
    }
213
}