Completed
Push — develop ( d594fb...df75c8 )
by Peter
04:13
created

InstallController::getRepository()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
/**
3
 * AnimeDb package
4
 *
5
 * @package   AnimeDb
6
 * @author    Peter Gribanov <[email protected]>
7
 * @copyright Copyright (c) 2011, Peter Gribanov
8
 * @license   http://opensource.org/licenses/GPL-3.0 GPL v3
9
 */
10
11
namespace AnimeDb\Bundle\CatalogBundle\Controller;
12
13
use Symfony\Component\Form\Form;
14
use Symfony\Component\HttpFoundation\Request;
15
use AnimeDb\Bundle\CatalogBundle\Entity\Storage;
16
use AnimeDb\Bundle\CatalogBundle\Repository\Storage as StorageRepository;
17
use AnimeDb\Bundle\CatalogBundle\Form\Type\Entity\Storage as StorageForm;
18
use AnimeDb\Bundle\AppBundle\Util\Filesystem;
19
use AnimeDb\Bundle\CatalogBundle\Event\Install\App as AppInstall;
20
use AnimeDb\Bundle\CatalogBundle\Event\Install\Samples as SamplesInstall;
21
use AnimeDb\Bundle\CatalogBundle\Event\Install\StoreEvents;
22
use Symfony\Component\HttpFoundation\Response;
23
24
/**
25
 * Installation controller
26
 *
27
 * @package AnimeDb\Bundle\CatalogBundle\Controller
28
 * @author  Peter Gribanov <[email protected]>
29
 */
30
class InstallController extends BaseController
31
{
32
    /**
33
     * Link to guide, how scan the storage
34
     *
35
     * @var string
36
     */
37
    const GUIDE_LINK_SCAN = '/guide/storage/scan.html';
38
39
    /**
40
     * Link to guide, how to start work
41
     *
42
     * @var string
43
     */
44
    const GUIDE_LINK_START = '/guide/start.html';
45
46
    /**
47
     * Home (Stap #1)
48
     *
49
     * @param Request $request
50
     *
51
     * @return Response
52
     */
53
    public function indexAction(Request $request)
54
    {
55
        // app already installed
56
        if ($this->container->getParameter('anime_db.catalog.installed')) {
57
            return $this->redirect($this->generateUrl('home'));
58
        }
59
60
        $response = $this->getCacheTimeKeeper()->getResponse();
61
        // response was not modified for this request
62
        if ($response->isNotModified($request)) {
63
            return $response;
64
        }
65
        $form = $this->createForm('anime_db_catalog_install_settings')->handleRequest($request);
66
67
        if ($form->isValid()) {
68
            // update params
69
            $this->get('anime_db.manipulator.parameters')
70
                ->set('locale', $form->getData()['locale']);
71
            $this->get('anime_db.manipulator.parameters')
72
                ->set('anime_db.catalog.default_search', $form->getData()['default_search']);
73
            // clear cache
74
            $this->get('anime_db.cache_clearer')->clear();
75
            // redirect to step 2
76
            return $this->redirect($this->generateUrl('install_add_storage'));
77
        }
78
79
        return $this->render('AnimeDbCatalogBundle:Install:index.html.twig', [
80
            'form' => $form->createView(),
81
        ], $response);
82
    }
83
84
    /**
85
     * Add storage (Stap #2)
86
     *
87
     * @param Request $request
88
     *
89
     * @return Response
90
     */
91
    public function addStorageAction(Request $request)
92
    {
93
        // app already installed
94
        if ($this->container->getParameter('anime_db.catalog.installed')) {
95
            return $this->redirect($this->generateUrl('home'));
96
        }
97
98
        $response = $this->getCacheTimeKeeper()->getResponse('AnimeDbCatalogBundle:Storage');
99
        // response was not modified for this request
100
        if ($response->isNotModified($request)) {
101
            return $response;
102
        }
103
        // get last storage
104
        $storage = $this->getRepository()->getLast();
105
        if (!$storage) {
106
            $storage = new Storage();
107
            $storage->setPath(Filesystem::getUserHomeDir());
108
        }
109
110
        /* @var $form Form */
111
        $form = $this->createForm(new StorageForm(), $storage)->handleRequest($request);
112
113
        if ($form->isValid()) {
114
            $em = $this->getDoctrine()->getManager();
115
            $em->persist($storage);
116
            $em->flush();
117
            // redirect to step 3
118
            return $this->redirect($this->generateUrl('install_what_you_want'));
119
        }
120
121
        return $this->render('AnimeDbCatalogBundle:Install:add_storage.html.twig', [
122
            'form' => $form->createView(),
123
            'is_new' => !$storage->getId(),
124
            'guide' => $this->get('anime_db.api.client')->getSiteUrl(StorageController::GUIDE_LINK)
125
        ], $response);
126
    }
127
128
    /**
129
     * What you want
130
     *
131
     * @param Request $request
132
     *
133
     * @return Response
134
     */
135 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...
136
    {
137
        // app already installed
138
        if ($this->container->getParameter('anime_db.catalog.installed')) {
139
            return $this->redirect($this->generateUrl('home'));
140
        }
141
142
        $response = $this->getCacheTimeKeeper()->getResponse();
143
        // response was not modified for this request
144
        if ($response->isNotModified($request)) {
145
            return $response;
146
        }
147
148
        if ($request->isMethod('POST')) {
149
            $storage = $this->getRepository()->getLast();
150
            $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 149 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...
151
            return $this->redirect($this->generateUrl('install_end_skip', ['from' => 'install_sample']));
152
        }
153
154
        return $this->render('AnimeDbCatalogBundle:Install:what_you_want.html.twig', [
155
            'guide' => $this->get('anime_db.api.client')->getSiteUrl(self::GUIDE_LINK_SCAN)
156
        ], $response);
157
    }
158
159
    /**
160
     * Scan storage (Stap #4)
161
     *
162
     * @param Request $request
163
     *
164
     * @return Response
165
     */
166
    public function scanAction(Request $request)
167
    {
168
        // app already installed
169
        if ($this->container->getParameter('anime_db.catalog.installed')) {
170
            return $this->redirect($this->generateUrl('home'));
171
        }
172
173
        $storage = $this->getRepository()->getLast();
174
        if (!$storage) {
175
            return $this->redirect('install_add_storage');
176
        }
177
178
        $response = $this->getCacheTimeKeeper()->getResponse($storage->getDateUpdate());
179
180
        // scan storage in background
181
        $this->get('anime_db.storage_scanner')->export($storage);
182
183
        // response was not modified for this request
184
        if ($response->isNotModified($request)) {
185
            return $response;
186
        }
187
188
        return $this->render('AnimeDbCatalogBundle:Install:scan.html.twig', [
189
            'storage' => $storage
190
        ], $response);
191
    }
192
193
    /**
194
     * End install (Stap #5)
195
     *
196
     * @param Request $request
197
     * @param string $from
198
     *
199
     * @return Response
200
     */
201 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...
202
    {
203
        // app already installed
204
        if ($this->container->getParameter('anime_db.catalog.installed')) {
205
            return $this->redirect($this->generateUrl('home'));
206
        }
207
208
        $response = $this->getCacheTimeKeeper()->getResponse();
209
        // response was not modified for this request
210
        if ($response->isNotModified($request)) {
211
            return $response;
212
        }
213
214
        if ($request->isMethod('POST')) {
215
            $this->get('event_dispatcher')->dispatch(StoreEvents::INSTALL_APP, new AppInstall());
216
            return $this->redirect($this->generateUrl('home'));
217
        }
218
219
        return $this->render('AnimeDbCatalogBundle:Install:end.html.twig', [
220
            'guide' => $this->get('anime_db.api.client')->getSiteUrl(self::GUIDE_LINK_START),
221
            'from' => $from
222
        ], $response);
223
    }
224
225
    /**
226
     * @return StorageRepository
227
     */
228
    protected function getRepository()
229
    {
230
        return $this->getDoctrine()->getRepository('AnimeDbCatalogBundle:Storage');
231
    }
232
}
233