Search   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 125
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 4
dl 0
loc 125
ccs 0
cts 49
cp 0
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getForm() 0 4 1
A __construct() 0 6 1
A getName() 0 4 1
A getTitle() 0 4 1
A buildMenu() 0 5 1
C search() 0 31 7
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\ShikimoriFillerBundle\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\ShikimoriBrowserBundle\Service\Browser;
14
use AnimeDb\Bundle\ShikimoriFillerBundle\Form\Type\Search as SearchForm;
15
use Knp\Menu\ItemInterface;
16
17
class Search extends SearchPlugin
18
{
19
    /**
20
     * @var string
21
     */
22
    const NAME = 'shikimori';
23
24
    /**
25
     * @var string
26
     */
27
    const TITLE = 'Shikimori.org';
28
29
    /**
30
     * Path for search.
31
     *
32
     * @var string
33
     */
34
    const SEARH_URL = '/animes?limit=#LIMIT#&search=#NAME#&genre=#GENRE#&type=#TYPE#&season=#SEASON#';
35
36
    /**
37
     * Limit the search results list.
38
     *
39
     * @var int
40
     */
41
    const DEFAULT_LIMIT = 30;
42
43
    /**
44
     * @var Browser
45
     */
46
    private $browser;
47
48
    /**
49
     * @var string
50
     */
51
    protected $locale;
52
53
    /**
54
     * @var SearchForm
55
     */
56
    protected $form;
57
58
    /**
59
     * @param Browser $browser
60
     * @param SearchForm $form
61
     * @param string $locale
62
     */
63
    public function __construct(Browser $browser, SearchForm $form, $locale)
64
    {
65
        $this->browser = $browser;
66
        $this->locale = $locale;
67
        $this->form = $form;
68
    }
69
70
    /**
71
     * @return string
72
     */
73
    public function getName()
74
    {
75
        return self::NAME;
76
    }
77
78
    /**
79
     * @return string
80
     */
81
    public function getTitle()
82
    {
83
        return self::TITLE;
84
    }
85
86
    /**
87
     * @param ItemInterface $item
88
     *
89
     * @return ItemInterface
90
     */
91
    public function buildMenu(ItemInterface $item)
92
    {
93
        return parent::buildMenu($item)
94
            ->setLinkAttribute('class', 'icon-label icon-label-plugin-shikimori');
95
    }
96
97
    /**
98
     * @param array $data
99
     *
100
     * @return ItemSearch[]
101
     */
102
    public function search(array $data)
103
    {
104
        $path = str_replace('#NAME#', urlencode($data['name']), self::SEARH_URL);
105
        $path = str_replace('#LIMIT#', self::DEFAULT_LIMIT, $path);
106
        $path = str_replace('#GENRE#', (isset($data['genre']) ? $data['genre'] : ''), $path);
107
        $path = str_replace('#TYPE#', (isset($data['type']) ? $data['type'] : ''), $path);
108
        $path = str_replace('#SEASON#', (isset($data['season']) ? str_replace('-', '_', $data['season']) : ''), $path);
109
        $body = (array) $this->browser->get($path);
110
111
        // build list
112
        foreach ($body as $key => $item) {
113
            // set a name based on the locale
114
            if ($this->locale == 'ru' && $item['russian']) {
115
                $name = $item['russian'];
116
                $description = $item['name'];
117
            } else {
118
                $name = $item['name'];
119
                $description = $item['russian'];
120
            }
121
122
            $body[$key] = new ItemSearch(
123
                $name,
124
                $this->getLinkForFill($this->browser->getHost().$item['url']),
125
                $this->browser->getHost().$item['image']['original'],
126
                $description,
127
                $this->browser->getHost().$item['url']
128
            );
129
        }
130
131
        return $body;
132
    }
133
134
    /**
135
     * @return SearchForm
136
     */
137
    public function getForm()
138
    {
139
        return $this->form;
140
    }
141
}
142