Completed
Push — master ( 1a5a6e...47a910 )
by Peter
01:56
created

UpdateTitlesCommand::execute()   C

Complexity

Conditions 7
Paths 3

Size

Total Lines 45
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 5
Bugs 0 Features 0
Metric Value
c 5
b 0
f 0
dl 0
loc 45
ccs 0
cts 37
cp 0
rs 6.7272
cc 7
eloc 30
nc 3
nop 2
crap 56
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\Command;
10
11
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
12
use Symfony\Component\Console\Input\InputInterface;
13
use Symfony\Component\Console\Output\OutputInterface;
14
15
/**
16
 * Update list of titles from AniDB.net.
17
 */
18
class UpdateTitlesCommand extends ContainerAwareCommand
19
{
20
    /**
21
     * @var int
22
     */
23
    const CACHE_LIFE_TIME = 86400;
24
25
    protected function configure()
26
    {
27
        $this->setName('animedb:update-titles')
28
            ->setDescription('Update list of titles from AniDB.net');
29
    }
30
31
    /**
32
     * @param InputInterface $input
33
     * @param OutputInterface $output
34
     *
35
     * @return int
36
     */
37
    protected function execute(InputInterface $input, OutputInterface $output)
38
    {
39
        $now = time();
40
        $file_csv = $this->getContainer()->getParameter('kernel.cache_dir').'/'.
41
            $this->getContainer()->getParameter('anime_db.ani_db.titles_db');
42
43
        if (!file_exists($file_csv) || filemtime($file_csv) + self::CACHE_LIFE_TIME < $now) {
44
            try {
45
                $file = $this->getOriginDb($output, $now);
46
            } catch (\Exception $e) {
47
                $output->writeln(sprintf('<error>Failed download origin DB: %s</error>', $e->getMessage()));
48
                return 1;
49
            }
50
51
            $output->writeln('Start assembling database');
52
53
            // clear list titles and add unified title
54
            $fp = gzopen($file, 'r');
55
            $fp_csv = gzopen($file_csv, 'w');
56
            while (!gzeof($fp)) {
57
                $line = trim(gzgets($fp, 4096));
58
                // ignore comments
59
                if ($line[0] == '#') {
60
                    continue;
61
                }
62
                list($aid, $type, $lang, $title) = explode('|', $line);
63
                $lang = substr($lang, 0, 2);
64
                // ignore not supported locales
65
                if ($lang == 'x-') {
66
                    continue;
67
                }
68
                gzwrite($fp_csv, $aid.'|'.$type.'|'.$lang.'|'.$this->getUnifiedTitle($title).'|'.$title."\n");
69
            }
70
            gzclose($fp);
71
            gzclose($fp_csv);
72
            touch($file, $now);
73
            touch($file_csv, $now);
74
75
            $output->writeln('The titles database is updated');
76
        } else {
77
            $output->writeln('Update is not needed');
78
        }
79
80
        return 0;
81
    }
82
83
    /**
84
     * Get original db file.
85
     *
86
     * Download the original db if need and cache it in a system temp dir
87
     *
88
     * @throws \InvalidArgumentException
89
     * @throws \RuntimeException
90
     *
91
     * @param OutputInterface $output
92
     * @param int $now
93
     *
94
     * @return string
95
     */
96
    protected function getOriginDb(OutputInterface $output, $now)
97
    {
98
        $url = $this->getContainer()->getParameter('anime_db.ani_db.import_titles');
99
100
        if (($path = parse_url($url, PHP_URL_PATH)) === false) {
101
            throw new \InvalidArgumentException('Failed parse URL: '.$url);
102
        }
103
104
        $file = sys_get_temp_dir().'/'.pathinfo($path, PATHINFO_BASENAME);
105
106
        if (!file_exists($file) || filemtime($file) + self::CACHE_LIFE_TIME < $now) {
107
            /* @var $downloader \AnimeDb\Bundle\AppBundle\Service\Downloader */
108
            $downloader = $this->getContainer()->get('anime_db.downloader');
109
            if (!$downloader->download($url, $file)) {
110
                throw new \RuntimeException('Failed to download the titles database');
111
            }
112
            $output->writeln('The titles database is loaded');
113
        }
114
115
        return $file;
116
    }
117
118
    /**
119
     * @param string $title
120
     *
121
     * @return string
122
     */
123 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...
124
    {
125
        $title = mb_strtolower($title, 'utf8');
126
        $title = preg_replace('/\W+/u', ' ', $title);
127
128
        return trim($title);
129
    }
130
}
131