Completed
Push — 0.4.27 ( d0497d )
by Peter
17:13 queued 14:52
created

RefillController::getForm()   C

Complexity

Conditions 18
Paths 57

Size

Total Lines 80
Code Lines 69

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 342

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 80
ccs 0
cts 75
cp 0
rs 5.026
cc 18
eloc 69
nc 57
nop 3
crap 342

How to fix   Long Method    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
/**
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 AnimeDb\Bundle\CatalogBundle\Entity\Item;
14
use AnimeDb\Bundle\CatalogBundle\Entity\Source;
15
use AnimeDb\Bundle\CatalogBundle\Plugin\Fill\Refiller\RefillerInterface;
16
use AnimeDb\Bundle\CatalogBundle\Form\Type\Plugin\Refiller\DateEnd as DateEndForm;
17
use AnimeDb\Bundle\CatalogBundle\Form\Type\Plugin\Refiller\DatePremiere as DatePremiereForm;
18
use AnimeDb\Bundle\CatalogBundle\Form\Type\Plugin\Refiller\Duration as DurationForm;
19
use AnimeDb\Bundle\CatalogBundle\Form\Type\Plugin\Refiller\Episodes as EpisodesForm;
20
use AnimeDb\Bundle\CatalogBundle\Form\Type\Plugin\Refiller\EpisodesNumber as EpisodesNumberForm;
21
use AnimeDb\Bundle\CatalogBundle\Form\Type\Plugin\Refiller\FileInfo as FileInfoForm;
22
use AnimeDb\Bundle\CatalogBundle\Form\Type\Plugin\Refiller\Images as ImagesForm;
23
use AnimeDb\Bundle\CatalogBundle\Form\Type\Plugin\Refiller\Names as NamesForm;
24
use AnimeDb\Bundle\CatalogBundle\Form\Type\Plugin\Refiller\Sources as SourcesForm;
25
use AnimeDb\Bundle\CatalogBundle\Form\Type\Plugin\Refiller\Summary as SummaryForm;
26
use AnimeDb\Bundle\CatalogBundle\Form\Type\Plugin\Refiller\Translate as TranslateForm;
27
use AnimeDb\Bundle\CatalogBundle\Plugin\Fill\Refiller\Item as ItemRefiller;
28
use Symfony\Component\Form\Form;
29
use Symfony\Component\HttpFoundation\Request;
30
use Symfony\Component\HttpFoundation\Response;
31
32
/**
33
 * Refill
34
 *
35
 * @package AnimeDb\Bundle\CatalogBundle\Controller
36
 * @author  Peter Gribanov <[email protected]>
37
 */
38
class RefillController extends BaseController
39
{
40
    /**
41
     * Refill item
42
     *
43
     * @param string $plugin
44
     * @param string $field
45
     * @param Request $request
46
     *
47
     * @return Response
48
     */
49 View Code Duplication
    public function refillAction($plugin, $field, 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...
50
    {
51
        /* @var $refiller RefillerInterface */
52
        if (!($refiller = $this->get('anime_db.plugin.refiller')->getPlugin($plugin))) {
53
            throw $this->createNotFoundException('Plugin \''.$plugin.'\' is not found');
54
        }
55
        $item = $this->createForm('anime_db_catalog_entity_item', new Item())
56
            ->handleRequest($request)
57
            ->getData();
58
59
        $form = $this->getForm($field, clone $item, $refiller->refill($item, $field));
60
61
        return $this->render('AnimeDbCatalogBundle:Refill:refill.html.twig', [
62
            'field' => $field,
63
            'form' => $form->createView(),
64
        ]);
65
    }
66
67
    /**
68
     * Search for refill
69
     *
70
     * @param string $plugin
71
     * @param string $field
72
     * @param Request $request
73
     *
74
     * @return Response
75
     */
76
    public function searchAction($plugin, $field, Request $request)
77
    {
78
        /* @var $refiller RefillerInterface */
79
        if (!($refiller = $this->get('anime_db.plugin.refiller')->getPlugin($plugin))) {
80
            throw $this->createNotFoundException('Plugin \''.$plugin.'\' is not found');
81
        }
82
        $item = $this->createForm('anime_db_catalog_entity_item', new Item())
83
            ->handleRequest($request)
84
            ->getData();
85
86
        $result = [];
87
        if ($refiller->isCanSearch($item, $field)) {
88
            $result = $refiller->search($item, $field);
89
            /* @var $search_item ItemRefiller */
90
            foreach ($result as $key => $search_item) {
91
                $result[$key] = [
92
                    'name' => $search_item->getName(),
93
                    'image' => $search_item->getImage(),
94
                    'description' => $search_item->getDescription(),
95
                    'source' => $search_item->getSource(),
96
                    'link' => $this->generateUrl('refiller_search_fill', [
97
                        'plugin' => $plugin,
98
                        'field' => $field,
99
                        'id' => $item->getId(),
100
                        'data' => $search_item->getData(),
101
                        'source' => $search_item->getSource(),
102
                    ])
103
                ];
104
            }
105
        }
106
107
        return $this->render('AnimeDbCatalogBundle:Refill:search.html.twig', [
108
            'result' => $result
109
        ]);
110
    }
111
112
    /**
113
     * Refill item from search result
114
     *
115
     * @param string $plugin
116
     * @param string $field
117
     * @param Request $request
118
     *
119
     * @return Response
120
     */
121 View Code Duplication
    public function fillFromSearchAction($plugin, $field, 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...
122
    {
123
        /* @var $refiller RefillerInterface */
124
        if (!($refiller = $this->get('anime_db.plugin.refiller')->getPlugin($plugin))) {
125
            throw $this->createNotFoundException('Plugin \''.$plugin.'\' is not found');
126
        }
127
        $item = $this->createForm('anime_db_catalog_entity_item', new Item())
128
            ->handleRequest($request)
129
            ->getData();
130
131
        $form = $this->getForm($field, clone $item, $refiller->refillFromSearchResult($item, $field, $request->get('data')));
132
133
        return $this->render('AnimeDbCatalogBundle:Refill:refill.html.twig', [
134
            'field' => $field,
135
            'form' => $form->createView(),
136
        ]);
137
    }
138
139
    /**
140
     * Get form for field
141
     *
142
     * @param string $field
143
     * @param Item $item_origin
144
     * @param Item $item_fill
145
     *
146
     * @return Form
147
     */
148
    protected function getForm($field, Item $item_origin, Item $item_fill)
149
    {
150
        switch ($field) {
151
            case RefillerInterface::FIELD_DATE_END:
152
                $form = new DateEndForm();
153
                $data = ['date_end' => $item_fill->getDateEnd()];
154
                break;
155
            case RefillerInterface::FIELD_DATE_PREMIERE:
156
                $form = new DatePremiereForm();
157
                $data = ['date_premiere' => $item_fill->getDatePremiere()];
158
                break;
159
            case RefillerInterface::FIELD_DURATION:
160
                $form = new DurationForm();
161
                $data = ['duration' => $item_fill->getDuration()];
162
                break;
163
            case RefillerInterface::FIELD_EPISODES:
164
                $form = new EpisodesForm();
165
                $data = ['episodes' => $item_fill->getEpisodes()];
166
                break;
167
            case RefillerInterface::FIELD_EPISODES_NUMBER:
168
                $form = new EpisodesNumberForm();
169
                $data = ['episodes_number' => $item_fill->getEpisodesNumber()];
170
                break;
171
            case RefillerInterface::FIELD_FILE_INFO:
172
                $form = new FileInfoForm();
173
                $data = ['file_info' => $item_fill->getFileInfo()];
174
                break;
175
            case RefillerInterface::FIELD_GENRES:
176
                $form = $this->get('anime_db.form.type.refill.gengres');
177
                $data = ['genres' => $item_fill->getGenres()];
178
                break;
179
            case RefillerInterface::FIELD_IMAGES:
180
                $form = new ImagesForm();
181
                $data = ['images' => $item_fill->getImages()];
182
                break;
183
            case RefillerInterface::FIELD_COUNTRY:
184
                $form = $this->get('anime_db.form.type.refill.country');
185
                $data = ['country' => $item_fill->getCountry()];
186
                break;
187
            case RefillerInterface::FIELD_NAMES:
188
                $form = new NamesForm();
189
                $data = ['names' => $item_fill->getNames()];
190
                break;
191
            case RefillerInterface::FIELD_SOURCES:
192
                $form = new SourcesForm();
193
                $data = ['sources' => $item_fill->getSources()];
194
                break;
195
            case RefillerInterface::FIELD_SUMMARY:
196
                $form = new SummaryForm();
197
                $data = ['summary' => $item_fill->getSummary()];
198
                break;
199
            case RefillerInterface::FIELD_TRANSLATE:
200
                $form = new TranslateForm();
201
                $data = ['translate' => $item_fill->getTranslate()];
202
                break;
203
            case RefillerInterface::FIELD_STUDIO:
204
                $form = $this->get('anime_db.form.type.refill.studio');
205
                $data = ['studio' => $item_fill->getStudio()];
206
                break;
207
            default:
208
                throw $this->createNotFoundException('Field \''.$field.'\' is not supported');
209
        }
210
        // search new source link
211
        /* @var $sources_origin Source[] */
212
        $sources_origin = array_reverse($item_origin->getSources()->toArray());
213
        /* @var $sources_fill Source[] */
214
        $sources_fill = array_reverse($item_fill->getSources()->toArray());
215
        foreach ($sources_fill as $source_fill) {
216
            // sources is already added
217
            foreach ($sources_origin as $source_origin) {
218
                if ($source_fill->getUrl() == $source_origin->getUrl()) {
219
                    continue 2;
220
                }
221
            }
222
            $data['source'] = $source_fill->getUrl();
223
            break;
224
        }
225
226
        return $this->createForm($form, $data);
227
    }
228
}
229