RefillController   B
last analyzed

Complexity

Total Complexity 26

Size/Duplication

Total Lines 191
Duplicated Lines 17.8 %

Coupling/Cohesion

Components 1
Dependencies 18

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 26
lcom 1
cbo 18
dl 34
loc 191
ccs 0
cts 134
cp 0
rs 7.3333
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A refillAction() 17 17 2
B searchAction() 0 35 4
A fillFromSearchAction() 17 17 2
C getForm() 0 80 18

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