InstallController   A
last analyzed

Complexity

Total Complexity 22

Size/Duplication

Total Lines 204
Duplicated Lines 22.55 %

Coupling/Cohesion

Components 1
Dependencies 12

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 22
lcom 1
cbo 12
dl 46
loc 204
ccs 0
cts 107
cp 0
rs 10
c 1
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
B indexAction() 0 30 4
B addStorageAction() 0 35 5
B whatYouWantAction() 23 24 4
B scanAction() 0 26 4
B endAction() 23 24 4
A getRepository() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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 AnimeDb\Bundle\CatalogBundle\Entity\Storage;
14
use AnimeDb\Bundle\CatalogBundle\Repository\Storage as StorageRepository;
15
use AnimeDb\Bundle\CatalogBundle\Form\Type\Entity\Storage as StorageForm;
16
use AnimeDb\Bundle\AppBundle\Util\Filesystem;
17
use AnimeDb\Bundle\CatalogBundle\Event\Install\App as AppInstall;
18
use AnimeDb\Bundle\CatalogBundle\Event\Install\Samples as SamplesInstall;
19
use AnimeDb\Bundle\CatalogBundle\Event\Install\StoreEvents;
20
use Symfony\Component\HttpFoundation\Response;
21
22
/**
23
 * Installation controller.
24
 *
25
 * @author  Peter Gribanov <[email protected]>
26
 */
27
class InstallController extends BaseController
28
{
29
    /**
30
     * Link to guide, how scan the storage.
31
     *
32
     * @var string
33
     */
34
    const GUIDE_LINK_SCAN = '/guide/storage/scan.html';
35
36
    /**
37
     * Link to guide, how to start work.
38
     *
39
     * @var string
40
     */
41
    const GUIDE_LINK_START = '/guide/start.html';
42
43
    /**
44
     * Home (Stap #1).
45
     *
46
     * @param Request $request
47
     *
48
     * @return Response
49
     */
50
    public function indexAction(Request $request)
51
    {
52
        // app already installed
53
        if ($this->container->getParameter('anime_db.catalog.installed')) {
54
            return $this->redirect($this->generateUrl('home'));
55
        }
56
57
        $response = $this->getCacheTimeKeeper()->getResponse();
58
        // response was not modified for this request
59
        if ($response->isNotModified($request)) {
60
            return $response;
61
        }
62
        $form = $this->createForm('install_settings')->handleRequest($request);
63
64
        if ($form->isValid()) {
65
            // update params
66
            $this->get('anime_db.manipulator.parameters')
67
                ->set('locale', $form->getData()['locale']);
68
            $this->get('anime_db.manipulator.parameters')
69
                ->set('anime_db.catalog.default_search', $form->getData()['default_search']);
70
            // clear cache
71
            $this->get('anime_db.cache_clearer')->clear();
72
            // redirect to step 2
73
            return $this->redirect($this->generateUrl('install_add_storage'));
74
        }
75
76
        return $this->render('AnimeDbCatalogBundle:Install:index.html.twig', [
77
            'form' => $form->createView(),
78
        ], $response);
79
    }
80
81
    /**
82
     * Add storage (Stap #2).
83
     *
84
     * @param Request $request
85
     *
86
     * @return Response
87
     */
88
    public function addStorageAction(Request $request)
89
    {
90
        // app already installed
91
        if ($this->container->getParameter('anime_db.catalog.installed')) {
92
            return $this->redirect($this->generateUrl('home'));
93
        }
94
95
        $response = $this->getCacheTimeKeeper()->getResponse('AnimeDbCatalogBundle:Storage');
96
        // response was not modified for this request
97
        if ($response->isNotModified($request)) {
98
            return $response;
99
        }
100
        // get last storage
101
        $storage = $this->getRepository()->getLast();
102
        if (!$storage) {
103
            $storage = new Storage();
104
            $storage->setPath(Filesystem::getUserHomeDir());
105
        }
106
107
        $form = $this->createForm(new StorageForm(), $storage)->handleRequest($request);
108
109
        if ($form->isValid()) {
110
            $em = $this->getDoctrine()->getManager();
111
            $em->persist($storage);
112
            $em->flush();
113
            // redirect to step 3
114
            return $this->redirect($this->generateUrl('install_what_you_want'));
115
        }
116
117
        return $this->render('AnimeDbCatalogBundle:Install:add_storage.html.twig', [
118
            'form' => $form->createView(),
119
            'is_new' => !$storage->getId(),
120
            'guide' => $this->get('anime_db.api.client')->getSiteUrl(StorageController::GUIDE_LINK),
121
        ], $response);
122
    }
123
124
    /**
125
     * What you want.
126
     *
127
     * @param Request $request
128
     *
129
     * @return Response
130
     */
131 View Code Duplication
    public function whatYouWantAction(Request $request)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
132
    {
133
        // app already installed
134
        if ($this->container->getParameter('anime_db.catalog.installed')) {
135
            return $this->redirect($this->generateUrl('home'));
136
        }
137
138
        $response = $this->getCacheTimeKeeper()->getResponse();
139
        // response was not modified for this request
140
        if ($response->isNotModified($request)) {
141
            return $response;
142
        }
143
144
        if ($request->isMethod('POST')) {
145
            $storage = $this->getRepository()->getLast();
146
            $this->get('event_dispatcher')->dispatch(StoreEvents::INSTALL_SAMPLES, new SamplesInstall($storage));
0 ignored issues
show
Bug introduced by
It seems like $storage defined by $this->getRepository()->getLast() on line 145 can be null; however, AnimeDb\Bundle\CatalogBu...\Samples::__construct() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
147
148
            return $this->redirect($this->generateUrl('install_end_skip', ['from' => 'install_sample']));
149
        }
150
151
        return $this->render('AnimeDbCatalogBundle:Install:what_you_want.html.twig', [
152
            'guide' => $this->get('anime_db.api.client')->getSiteUrl(self::GUIDE_LINK_SCAN),
153
        ], $response);
154
    }
155
156
    /**
157
     * Scan storage (Stap #4).
158
     *
159
     * @param Request $request
160
     *
161
     * @return Response
162
     */
163
    public function scanAction(Request $request)
164
    {
165
        // app already installed
166
        if ($this->container->getParameter('anime_db.catalog.installed')) {
167
            return $this->redirect($this->generateUrl('home'));
168
        }
169
170
        $storage = $this->getRepository()->getLast();
171
        if (!$storage) {
172
            return $this->redirect('install_add_storage');
173
        }
174
175
        $response = $this->getCacheTimeKeeper()->getResponse($storage->getDateUpdate());
176
177
        // scan storage in background
178
        $this->get('anime_db.storage.scan_executor')->export($storage);
179
180
        // response was not modified for this request
181
        if ($response->isNotModified($request)) {
182
            return $response;
183
        }
184
185
        return $this->render('AnimeDbCatalogBundle:Install:scan.html.twig', [
186
            'storage' => $storage,
187
        ], $response);
188
    }
189
190
    /**
191
     * End install (Stap #5).
192
     *
193
     * @param Request $request
194
     * @param string $from
195
     *
196
     * @return Response
197
     */
198 View Code Duplication
    public function endAction(Request $request, $from = '')
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
199
    {
200
        // app already installed
201
        if ($this->container->getParameter('anime_db.catalog.installed')) {
202
            return $this->redirect($this->generateUrl('home'));
203
        }
204
205
        $response = $this->getCacheTimeKeeper()->getResponse();
206
        // response was not modified for this request
207
        if ($response->isNotModified($request)) {
208
            return $response;
209
        }
210
211
        if ($request->isMethod('POST')) {
212
            $this->get('event_dispatcher')->dispatch(StoreEvents::INSTALL_APP, new AppInstall());
213
214
            return $this->redirect($this->generateUrl('home'));
215
        }
216
217
        return $this->render('AnimeDbCatalogBundle:Install:end.html.twig', [
218
            'guide' => $this->get('anime_db.api.client')->getSiteUrl(self::GUIDE_LINK_START),
219
            'from' => $from,
220
        ], $response);
221
    }
222
223
    /**
224
     * @return StorageRepository
225
     */
226
    protected function getRepository()
227
    {
228
        return $this->getDoctrine()->getRepository('AnimeDbCatalogBundle:Storage');
229
    }
230
}
231