Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
20 / 20
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
PurgeCachedRecordCommand
100.00% covered (success)
100.00%
20 / 20
100.00% covered (success)
100.00%
3 / 3
6
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
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
 execute
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
4
1<?php
2
3/**
4 * Console command: purge a record from cache
5 *
6 * PHP version 8
7 *
8 * Copyright (C) The National Library of Finland 2023.
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   Ere Maijala <ere.maijala@helsinki.fi>
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\Util;
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\Db\Service\RecordServiceInterface;
39use VuFind\Db\Service\ResourceServiceInterface;
40
41/**
42 * Console command: purge a record from cache
43 *
44 * @category VuFind
45 * @package  Console
46 * @author   Demian Katz <demian.katz@villanova.edu>
47 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
48 * @link     https://vufind.org/wiki/development Wiki
49 */
50#[AsCommand(
51    name: 'util/purge_cached_record',
52    description: 'Purge a cached record and optionally a resource'
53)]
54class PurgeCachedRecordCommand extends Command
55{
56    /**
57     * Constructor
58     *
59     * @param RecordServiceInterface   $recordService   Record table object
60     * @param ResourceServiceInterface $resourceService Resource table object
61     * @param string|null              $name            The name of the command; passing null means it
62     * must be set in configure()
63     */
64    public function __construct(
65        protected RecordServiceInterface $recordService,
66        protected ResourceServiceInterface $resourceService,
67        string $name = null
68    ) {
69        parent::__construct($name);
70    }
71
72    /**
73     * Configure the command.
74     *
75     * @return void
76     */
77    protected function configure()
78    {
79        $this
80            ->setHelp('Removes a record and optionally a resource from the database.')
81            ->addOption(
82                'purge-resource',
83                null,
84                InputOption::VALUE_NONE,
85                'Purge the resource entry as well (deletes the record from favorites)'
86            )->addArgument('source', InputArgument::REQUIRED, 'Record source (e.g. Solr)')
87            ->addArgument('id', InputArgument::REQUIRED, 'Record ID');
88    }
89
90    /**
91     * Run the command.
92     *
93     * @param InputInterface  $input  Input object
94     * @param OutputInterface $output Output object
95     *
96     * @return int 0 for success
97     *
98     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
99     */
100    protected function execute(InputInterface $input, OutputInterface $output)
101    {
102        $source = $input->getArgument('source');
103        $id = $input->getArgument('id');
104        if ($this->recordService->deleteRecord($id, $source)) {
105            $output->writeln('Cached record deleted');
106        } else {
107            $output->writeln('No cached record found');
108        }
109        if ($input->getOption('purge-resource')) {
110            if ($this->resourceService->deleteResourceByRecordId($id, $source)) {
111                $output->writeln('Resource deleted');
112            } else {
113                $output->writeln('No resource found');
114            }
115        }
116        return 0;
117    }
118}