UpdateController   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 120
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 14
lcom 1
cbo 7
dl 0
loc 120
ccs 0
cts 61
cp 0
rs 10
c 2
b 0
f 1

4 Methods

Rating   Name   Duplication   Size   Complexity  
B indexAction() 0 35 6
A getAction() 0 12 4
A getPlugin() 0 10 2
A executeAction() 0 17 2
1
<?php
2
/**
3
 * AnimeDb package.
4
 *
5
 * @author    Peter Gribanov <[email protected]>
6
 * @copyright Copyright (c) 2011, Peter Gribanov
7
 * @license   http://opensource.org/licenses/GPL-3.0 GPL v3
8
 */
9
10
namespace AnimeDb\Bundle\CatalogBundle\Controller;
11
12
use Symfony\Component\HttpFoundation\Request;
13
use Symfony\Component\HttpFoundation\Response;
14
15
/**
16
 * System update.
17
 *
18
 * @author  Peter Gribanov <[email protected]>
19
 */
20
class UpdateController extends BaseController
21
{
22
    /**
23
     * Message identifies the end of the update.
24
     *
25
     * @var string
26
     */
27
    const END_MESSAGE = 'Updating the application has been completed';
28
29
    /**
30
     * Link to guide by update the application on Windows XP.
31
     *
32
     * @var string
33
     */
34
    const GUIDE_LINK = '/guide/general/update.html#update-win-xp';
35
36
    /**
37
     * Update page.
38
     *
39
     * @param Request $request
40
     *
41
     * @return Response
42
     */
43
    public function indexAction(Request $request)
44
    {
45
        $response = $this->getCacheTimeKeeper()->getResponse();
46
        // response was not modified for this request
47
        if ($response->isNotModified($request)) {
48
            return $response;
49
        }
50
51
        // update for Windows XP does not work
52
        $can_update = strpos(php_uname('v'), 'Windows XP') === false;
53
54
        $plugin = $request->request->get('plugin');
55
        $action = $this->getAction($plugin);
56
57
        // delete or install package
58
        switch ($action) {
59
            case 'delete':
60
                $this->get('anime_db.manipulator.composer')->removePackage($plugin['delete']);
61
                $plugin = $this->getPlugin('plugin/'.$plugin['delete'].'/');
62
                break;
63
            case 'install':
64
                $this->get('anime_db.manipulator.composer')
65
                    ->addPackage($plugin['install']['package'], $plugin['install']['version']);
66
                $plugin = $this->getPlugin('plugin/'.$plugin['install']['package'].'/');
67
                break;
68
        }
69
70
        return $this->render('AnimeDbCatalogBundle:Update:index.html.twig', [
71
            'can_update' => $can_update,
72
            'doc' => !$can_update ? $this->get('anime_db.api.client')->getSiteUrl(self::GUIDE_LINK) : '',
73
            'referer' => $request->headers->get('referer'),
74
            'plugin' => $action ? $plugin : [],
75
            'action' => $action,
76
        ], $response);
77
    }
78
79
    /**
80
     * Get action.
81
     *
82
     * @param array $plugin
83
     *
84
     * @return string
85
     */
86
    protected function getAction($plugin)
87
    {
88
        if (!$plugin) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $plugin of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
89
            return '';
90
        } elseif (!empty($plugin['delete'])) {
91
            return 'delete';
92
        } elseif (!empty($plugin['install'])) {
93
            return 'install';
94
        }
95
96
        return '';
97
    }
98
99
    /**
100
     * Get plugin.
101
     *
102
     * @param string $plugin
103
     *
104
     * @return array|null
105
     */
106
    protected function getPlugin($plugin)
107
    {
108
        try {
109
            list($vendor, $package) = explode('/', $plugin);
110
111
            return $this->get('anime_db.api.client')->getPlugin($vendor, $package);
112
        } catch (\RuntimeException $e) {
113
            return;
114
        }
115
    }
116
117
    /**
118
     * Execute update application.
119
     *
120
     * @return Response
121
     */
122
    public function executeAction()
123
    {
124
        // update for Windows XP does not work
125
        if (strpos(php_uname('v'), 'Windows XP') !== false) {
126
            $this->redirect($this->generateUrl('update'));
127
        }
128
129
        // execute update
130
        file_put_contents($this->container->getParameter('kernel.root_dir').'/../web/update.log', '');
131
        $this->get('anime_db.command')
132
            ->send('php app/console animedb:update --env=prod --no-ansi >web/update.log 2>&1');
133
134
        return $this->render('AnimeDbCatalogBundle:Update:execute.html.twig', [
135
            'log_file' => '/update.log',
136
            'end_message' => self::END_MESSAGE,
137
        ]);
138
    }
139
}
140