Search::getNameForLocale()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 10

Duplication

Lines 13
Ratio 92.86 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 13
loc 14
c 0
b 0
f 0
ccs 0
cts 13
cp 0
rs 9.4285
cc 3
eloc 10
nc 3
nop 2
crap 12
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\Service;
10
11
use AnimeDb\Bundle\CatalogBundle\Plugin\Fill\Search\Search as SearchPlugin;
12
use AnimeDb\Bundle\CatalogBundle\Plugin\Fill\Search\Item as ItemSearch;
13
use AnimeDb\Bundle\AniDbBrowserBundle\Service\Browser;
14
use Knp\Menu\ItemInterface;
15
16
/**
17
 * Search from site AniDB.net.
18
 */
19
class Search extends SearchPlugin
20
{
21
    /**
22
     * @var string
23
     */
24
    const NAME = 'anidb';
25
26
    /**
27
     * @var string
28
     */
29
    const TITLE = 'AniDB.net';
30
31
    /**
32
     * @var string
33
     */
34
    const ITEM_LINK = '/perl-bin/animedb.pl?show=anime&aid=#ID#';
35
36
    /**
37
     * @var Browser
38
     */
39
    protected $browser;
40
41
    /**
42
     * @var string
43
     */
44
    protected $titles_db;
45
46
    /**
47
     * @var string
48
     */
49
    protected $locale;
50
51
    /**
52
     * @param Browser $browser
53
     * @param string $titles_db
54
     * @param string $cache_dir
55
     * @param string $locale
56
     */
57
    public function __construct(Browser $browser, $titles_db, $cache_dir, $locale)
58
    {
59
        $this->browser = $browser;
60
        $this->locale = $locale;
61
        $this->titles_db = $cache_dir.'/'.$titles_db;
62
    }
63
64
    /**
65
     * @return string
66
     */
67
    public function getName()
68
    {
69
        return self::NAME;
70
    }
71
72
    /**
73
     * @return string
74
     */
75
    public function getTitle()
76
    {
77
        return self::TITLE;
78
    }
79
80
    /**
81
     * Build menu for plugin.
82
     *
83
     * @param ItemInterface $item
84
     *
85
     * @return ItemInterface
86
     */
87
    public function buildMenu(ItemInterface $item)
88
    {
89
        return parent::buildMenu($item)
90
            ->setLinkAttribute('class', 'icon-label icon-label-plugin-anidb');
91
    }
92
93
    /**
94
     * Search source by name.
95
     *
96
     * @param array $data
97
     *
98
     * @return ItemSearch[]
99
     */
100
    public function search(array $data)
101
    {
102
        // if the db does not exists, send a request to download
103
        if (!file_exists($this->titles_db)) {
104
            return [];
105
        }
106
107
        $search = $this->getUnifiedTitle($data['name']);
108
109
        // search by name
110
        $aids = [];
111
        $fp = gzopen($this->titles_db, 'r');
112
        while (!gzeof($fp)) {
113
            $line = trim(gzgets($fp, 4096));
114
            list($aid, $type, $lang, $unified) = explode('|', $line);
115
            if (mb_strpos($unified, $search, 0, 'utf8') === 0) {
116
                if ($type == 1 || ($type == 4 && $lang == $this->locale) || empty($titles[$aid])) {
117
                    $aids[] = $aid;
118
                }
119
            }
120
        }
121
        gzclose($fp);
122
        $aids = array_unique($aids);
123
124
        // get all names for aid
125
        $items = [];
126
        $fp = gzopen($this->titles_db, 'r');
127
        while (!gzeof($fp)) {
128
            $line = trim(gzgets($fp, 4096));
129
            list($aid, $type, $lang, , $title) = explode('|', $line);
130
            if (in_array($aid, $aids)) {
131
                $items[$aid][$lang][$type] = $title;
132
            }
133
        }
134
        gzclose($fp);
135
136
        // build result
137
        foreach ($items as $aid => $item) {
138
            if (!empty($item[$this->locale])) {
139
                $main_name = $this->getNameForLocale($this->locale, $item);
140 View Code Duplication
            } elseif ($this->locale != 'en' && !empty($item['en'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
141
                $main_name = $this->getNameForLocale('en', $item);
142
            } else {
143
                $main_name = $this->getNameForLocale(array_keys($item)[0], $item);
144
            }
145
            $description = [];
146
            foreach ($item as $names) {
147
                foreach ($names as $name) {
148
                    $description[] = $name;
149
                }
150
            }
151
            sort($description);
152
            $items[$aid] = new ItemSearch(
153
                $main_name,
154
                $this->getLinkForFill($this->browser->getHost().str_replace('#ID#', $aid, self::ITEM_LINK)),
155
                $this->router->generate('ani_db_media_cover', ['id' => $aid]),
156
                implode("\n", array_unique($description)),
157
                $this->browser->getHost().str_replace('#ID#', $aid, self::ITEM_LINK)
158
            );
159
        }
160
161
        return $items;
162
    }
163
164
    /**
165
     * @param string $title
166
     *
167
     * @return string
168
     */
169 View Code Duplication
    protected function getUnifiedTitle($title)
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...
170
    {
171
        $title = mb_strtolower($title, 'utf8');
172
        $title = preg_replace('/\W+/u', ' ', $title);
173
174
        return trim($title);
175
    }
176
177
    /**
178
     * @param string $locale
179
     * @param array $names
180
     *
181
     * @return string
182
     */
183 View Code Duplication
    protected function getNameForLocale($locale, &$names)
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...
184
    {
185
        if (isset($names[$locale][1])) {
186
            $name = $names[$locale][1];
187
            unset($names[$locale][1]);
188
        } elseif (isset($names[$locale][4])) {
189
            $name = $names[$locale][4];
190
            unset($names[$locale][4]);
191
        } else {
192
            $name = array_shift($names[$locale]);
193
        }
194
195
        return $name;
196
    }
197
}
198