Refiller::onAddNewItem()   D
last analyzed

Complexity

Conditions 13
Paths 258

Size

Total Lines 54
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 182

Importance

Changes 0
Metric Value
dl 0
loc 54
c 0
b 0
f 0
ccs 0
cts 45
cp 0
rs 4.9261
cc 13
eloc 32
nc 258
nop 1
crap 182

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
 * @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
namespace AnimeDb\Bundle\AniDbFillerBundle\Event\Listener;
10
11
use AnimeDb\Bundle\AniDbFillerBundle\Service\SummaryCleaner;
12
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
13
use AnimeDb\Bundle\AniDbFillerBundle\Service\Refiller as RefillerService;
14
use AnimeDb\Bundle\AniDbFillerBundle\Service\Filler;
15
use AnimeDb\Bundle\AniDbBrowserBundle\Service\Browser;
16
use AnimeDb\Bundle\CatalogBundle\Event\Storage\AddNewItem;
17
use AnimeDb\Bundle\CatalogBundle\Event\Storage\StoreEvents;
18
use AnimeDb\Bundle\CatalogBundle\Entity\Item;
19
use AnimeDb\Bundle\CatalogBundle\Entity\Name;
20
21
/**
22
 * Refiller for new item.
23
 */
24
class Refiller
25
{
26
    /**
27
     * @var RefillerService
28
     */
29
    protected $refiller;
30
31
    /**
32
     * @var Filler
33
     */
34
    protected $filler;
35
36
    /**
37
     * @var Browser
38
     */
39
    protected $browser;
40
41
    /**
42
     * @var EventDispatcherInterface
43
     */
44
    protected $dispatcher;
45
46
    /**
47
     * @var SummaryCleaner
48
     */
49
    protected $cleaner;
50
51
    /**
52
     * @param EventDispatcherInterface $dispatcher
53
     * @param RefillerService $refiller
54
     * @param Filler $filler
55
     * @param Browser $browser
56
     * @param SummaryCleaner $cleaner
57
     */
58
    public function __construct(
59
        EventDispatcherInterface $dispatcher,
60
        RefillerService $refiller,
61
        Filler $filler,
62
        Browser $browser,
63
        SummaryCleaner $cleaner
64
    ) {
65
        $this->dispatcher = $dispatcher;
66
        $this->refiller = $refiller;
67
        $this->filler = $filler;
68
        $this->browser = $browser;
69
        $this->cleaner = $cleaner;
70
    }
71
72
    /**
73
     * @param AddNewItem $event
74
     */
75
    public function onAddNewItem(AddNewItem $event)
76
    {
77
        $item = $event->getItem();
78
        if (!$event->getFillers()->contains($this->filler) &&
79
            ($url = $this->refiller->getSourceForFill($item)) &&
80
            preg_match(Filler::REG_ITEM_ID, $url, $match)
81
        ) {
82
            try {
83
                // get data
84
                $body = $this->browser->get('anime', ['aid' => $match['id']]);
85
            } catch (\Exception $e) {
86
                return;
87
            }
88
89
            // fill item
90
            if (!$item->getDateEnd()) {
91
                $item->setDateEnd(new \DateTime($body->filter('enddate')->text()));
92
            }
93
            if (!$item->getDatePremiere()) {
94
                $item->setDatePremiere(new \DateTime($body->filter('startdate')->text()));
95
            }
96
            if (!$item->getEpisodes()) {
97
                $this->filler->setEpisodes($item, $body);
98
            }
99
            if (!$item->getEpisodesNumber()) {
100
                $item->setEpisodesNumber($body->filter('episodecount')->text());
101
            }
102
            if (!$item->getSummary()) {
103
                $item->setSummary($this->cleaner->clean($body->filter('description')->text()));
104
            }
105
            if (!$item->getType()) {
106
                $this->filler->setType($item, $body);
107
            }
108
            if (!$item->getCover()) {
109
                $this->filler->setCover($item, $body, $match['id']);
110
            }
111
            $this->filler->setGenres($item, $body);
112
113
            // copy main and other names
114
            $new_item = $this->filler->setNames(new Item(), $body);
115
            // set main name in top of names list
116
            $new_names = $new_item->getNames()->toArray();
117
            array_unshift($new_names, (new Name())->setName($new_item->getName()));
118
            /* @var $new_name Name */
119
            foreach ($new_names as $new_name) {
120
                $item->addName($new_name->setItem(null));
121
            }
122
123
            $event->addFiller($this->filler);
124
            // resend event
125
            $this->dispatcher->dispatch(StoreEvents::ADD_NEW_ITEM, clone $event);
126
            $event->stopPropagation();
127
        }
128
    }
129
}
130