Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
5 / 5
CRAP
100.00% covered (success)
100.00%
1 / 1
AbstractRecordWriterStrategy
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
5 / 5
5
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
 getBasePath
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getFilename
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 saveDeletedRecords
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 saveFile
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3/**
4 * Abstract record writer strategy (shared base for standard vs. combined modes
5 * of saving records).
6 *
7 * PHP version 7
8 *
9 * Copyright (c) Demian Katz 2016.
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2,
13 * as published by the Free Software Foundation.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
23 *
24 * @category VuFind
25 * @package  Harvest_Tools
26 * @author   Demian Katz <demian.katz@villanova.edu>
27 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
28 * @link     https://vufind.org/wiki/indexing:oai-pmh Wiki
29 */
30
31namespace VuFindHarvest\RecordWriterStrategy;
32
33/**
34 * Abstract record writer strategy (shared base for standard vs. combined modes
35 * of saving records).
36 *
37 * @category VuFind
38 * @package  Harvest_Tools
39 * @author   Demian Katz <demian.katz@villanova.edu>
40 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
41 * @link     https://vufind.org/wiki/indexing:oai-pmh Wiki
42 */
43abstract class AbstractRecordWriterStrategy implements RecordWriterStrategyInterface
44{
45    /**
46     * Directory for storing harvested files
47     *
48     * @var string
49     */
50    protected $basePath;
51
52    /**
53     * Constructor
54     *
55     * @param string $basePath Target directory for harvested files
56     */
57    public function __construct($basePath)
58    {
59        $this->basePath = $basePath;
60    }
61
62    /**
63     * Get base path for writes.
64     *
65     * @return string
66     */
67    public function getBasePath()
68    {
69        return $this->basePath;
70    }
71
72    /**
73     * Get the filename for a specific record ID.
74     *
75     * @param string $id  ID of record to save.
76     * @param string $ext File extension to use.
77     *
78     * @return string     Full path + filename.
79     */
80    protected function getFilename($id, $ext)
81    {
82        return $this->basePath . time() . '_' .
83            preg_replace('/[^\w]/', '_', $id) . '.' . $ext;
84    }
85
86    /**
87     * Create a tracking file to record the deletion of a record.
88     *
89     * @param string|array $ids ID(s) of deleted record(s).
90     *
91     * @return void
92     */
93    protected function saveDeletedRecords($ids)
94    {
95        $ids = (array)$ids; // make sure input is array format
96        $filename = $this->getFilename($ids[0], 'delete');
97        file_put_contents($filename, implode("\n", $ids));
98    }
99
100    /**
101     * Save a record to disk.
102     *
103     * @param string $id  Record ID to use for filename generation.
104     * @param string $xml XML to save.
105     *
106     * @return void
107     */
108    protected function saveFile($id, $xml)
109    {
110        // Save our XML:
111        file_put_contents($this->getFilename($id, 'xml'), trim($xml));
112    }
113}