Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
97.83% covered (success)
97.83%
45 / 46
66.67% covered (warning)
66.67%
2 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
ImportCsvCommand
97.83% covered (success)
97.83%
45 / 46
66.67% covered (warning)
66.67%
2 / 3
10
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 configure
100.00% covered (success)
100.00%
27 / 27
100.00% covered (success)
100.00%
1 / 1
1
 execute
94.12% covered (success)
94.12%
16 / 17
0.00% covered (danger)
0.00%
0 / 1
8.01
1<?php
2
3/**
4 * Console command: CSV importer
5 *
6 * PHP version 8
7 *
8 * Copyright (C) Villanova University 2020.
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2,
12 * as published by the Free Software Foundation.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
22 *
23 * @category VuFind
24 * @package  Console
25 * @author   Demian Katz <demian.katz@villanova.edu>
26 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
27 * @link     https://vufind.org/wiki/development Wiki
28 */
29
30namespace VuFindConsole\Command\Import;
31
32use Symfony\Component\Console\Attribute\AsCommand;
33use Symfony\Component\Console\Command\Command;
34use Symfony\Component\Console\Input\InputArgument;
35use Symfony\Component\Console\Input\InputInterface;
36use Symfony\Component\Console\Input\InputOption;
37use Symfony\Component\Console\Output\OutputInterface;
38use VuFind\CSV\Importer;
39
40use function is_callable;
41
42/**
43 * Console command: CSV importer
44 *
45 * @category VuFind
46 * @package  Console
47 * @author   Demian Katz <demian.katz@villanova.edu>
48 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
49 * @link     https://vufind.org/wiki/development Wiki
50 */
51#[AsCommand(
52    name: 'import/import-csv',
53    description: 'CSV importer'
54)]
55class ImportCsvCommand extends Command
56{
57    /**
58     * CSV importer
59     *
60     * @var Importer
61     */
62    protected $importer;
63
64    /**
65     * Constructor
66     *
67     * @param Importer    $importer CSV importer
68     * @param string|null $name     The name of the command; passing null means it
69     * must be set in configure()
70     */
71    public function __construct(Importer $importer, $name = null)
72    {
73        $this->importer = $importer;
74        parent::__construct($name);
75    }
76
77    /**
78     * Configure the command.
79     *
80     * @return void
81     */
82    protected function configure()
83    {
84        $this
85            ->setHelp('Indexes CSV files into Solr.')
86            ->addArgument(
87                'CSV_file',
88                InputArgument::REQUIRED,
89                'source file to index'
90            )->addArgument(
91                'ini_file',
92                InputArgument::REQUIRED,
93                'import configuration file ($VUFIND_LOCAL_DIR/import and '
94                . ' $VUFIND_HOME/import will'
95                . "\nbe searched for this filename; see csv.ini "
96                . 'for configuration examples)'
97            )->addOption(
98                'test-only',
99                null,
100                InputOption::VALUE_NONE,
101                'activates test mode, which displays transformed output without '
102                . 'updating Solr'
103            )->addOption(
104                'index',
105                null,
106                InputOption::VALUE_OPTIONAL,
107                'name of search backend to index content into (could be overridden '
108                . "with,\nfor example, SolrAuth to index authority records)",
109                'Solr'
110            );
111    }
112
113    /**
114     * Run the command.
115     *
116     * @param InputInterface  $input  Input object
117     * @param OutputInterface $output Output object
118     *
119     * @return int 0 for success
120     */
121    protected function execute(InputInterface $input, OutputInterface $output)
122    {
123        $testMode = $input->getOption('test-only') ? true : false;
124        $index = $input->getOption('index');
125        $csv = $input->getArgument('CSV_file');
126        $ini = $input->getArgument('ini_file');
127        // Try to import the document if successful:
128        try {
129            $result = $this->importer->save($csv, $ini, $index, $testMode);
130            if ($testMode) {
131                $output->writeln($result);
132            }
133        } catch (\Exception $e) {
134            $output->writeln('Fatal error: ' . $e->getMessage());
135            if (is_callable([$e, 'getPrevious']) && $e = $e->getPrevious()) {
136                while ($e) {
137                    $output->writeln('Previous exception: ' . $e->getMessage());
138                    $e = $e->getPrevious();
139                }
140            }
141            return 1;
142        }
143        if (!$testMode) {
144            $output->writeln("Successfully imported $csv...");
145        }
146        return 0;
147    }
148}