Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
22 / 22
100.00% covered (success)
100.00%
4 / 4
CRAP
100.00% covered (success)
100.00%
1 / 1
AbstractThemeCommand
100.00% covered (success)
100.00%
22 / 22
100.00% covered (success)
100.00%
4 / 4
6
100.00% covered (success)
100.00%
1 / 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%
9 / 9
100.00% covered (success)
100.00%
1 / 1
1
 generate
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 execute
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
3
1<?php
2
3/**
4 * Abstract base class for theme resource generator commands.
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\Generate;
31
32use Symfony\Component\Console\Command\Command;
33use Symfony\Component\Console\Input\InputArgument;
34use Symfony\Component\Console\Input\InputInterface;
35use Symfony\Component\Console\Output\OutputInterface;
36use VuFindTheme\GeneratorInterface;
37
38/**
39 * Abstract base class for theme resource generator commands.
40 *
41 * @category VuFind
42 * @package  Console
43 * @author   Demian Katz <demian.katz@villanova.edu>
44 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
45 * @link     https://vufind.org/wiki/development Wiki
46 */
47abstract class AbstractThemeCommand extends Command
48{
49    /**
50     * Theme resource generator
51     *
52     * @var GeneratorInterface
53     */
54    protected $generator;
55
56    /**
57     * Type of resource being generated (used in help messages)
58     *
59     * @var string
60     */
61    protected $type;
62
63    /**
64     * Extra text to append to the output when generation is successful.
65     *
66     * @var string
67     */
68    protected $extraSuccessMessage = '';
69
70    /**
71     * Constructor
72     *
73     * @param GeneratorInterface $generator Generator to call
74     * @param string|null        $name      The name of the command; passing null
75     * means it must be set in configure()
76     */
77    public function __construct(GeneratorInterface $generator, $name = null)
78    {
79        $this->generator = $generator;
80        parent::__construct($name);
81    }
82
83    /**
84     * Configure the command.
85     *
86     * @return void
87     */
88    protected function configure()
89    {
90        $this
91            ->setDescription(ucwords($this->type) . ' generator')
92            ->setHelp('Creates and configures a new ' . $this->type . '.')
93            ->addArgument(
94                'name',
95                InputArgument::OPTIONAL,
96                'name of ' . $this->type
97                . ' to generate. Defaults to custom  if unspecified.'
98            );
99    }
100
101    /**
102     * Run the generator.
103     *
104     * @param string $name Name of resource to generate
105     *
106     * @return bool
107     */
108    protected function generate($name)
109    {
110        return $this->generator->generate($name);
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        $name = $input->getArgument('name');
124        if (empty($name)) {
125            $output->writeln("\tNo {$this->type} name provided, using \"custom\"");
126            $name = 'custom';
127        }
128
129        $this->generator->setOutputInterface($output);
130
131        if (!$this->generate($name)) {
132            $output->writeln($this->generator->getLastError());
133            return 1;
134        }
135        $output->writeln(rtrim("\tFinished. {$this->extraSuccessMessage}"));
136        return 0;
137    }
138}