Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
21 / 21
100.00% covered (success)
100.00%
6 / 6
CRAP
100.00% covered (success)
100.00%
1 / 1
CombinedRecordWriterStrategy
100.00% covered (success)
100.00%
21 / 21
100.00% covered (success)
100.00%
6 / 6
10
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 getCombinedXML
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 beginWrite
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 addDeletedRecord
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 addRecord
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 endWrite
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
3
1<?php
2
3/**
4 * Strategy for writing records to disk as a combined file.
5 *
6 * PHP version 7
7 *
8 * Copyright (c) Demian Katz 2016.
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  Harvest_Tools
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/indexing:oai-pmh Wiki
28 */
29
30namespace VuFindHarvest\RecordWriterStrategy;
31
32/**
33 * Strategy for writing records to disk as a combined file.
34 *
35 * @category VuFind
36 * @package  Harvest_Tools
37 * @author   Demian Katz <demian.katz@villanova.edu>
38 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
39 * @link     https://vufind.org/wiki/indexing:oai-pmh Wiki
40 */
41class CombinedRecordWriterStrategy extends AbstractRecordWriterStrategy
42{
43    /**
44     * The wrapping XML tag to be used if combinedRecords is set to true
45     *
46     * @var string
47     */
48    protected $wrappingTag = '<collection>';
49
50    /**
51     * Collection of deleted IDs.
52     *
53     * @var array
54     */
55    protected $deletedIds = [];
56
57    /**
58     * Collection of XML to include inside final output tag.
59     *
60     * @var array
61     */
62    protected $innerXML = '';
63
64    /**
65     * The ID of the first successfully harvested record.
66     *
67     * @var string
68     */
69    protected $firstHarvestedId = false;
70
71    /**
72     * Constructor
73     *
74     * @param string $basePath Target directory for harvested files
75     * @param string $tag      Wrapping tag to contain collection (null for default
76     * of <collection>)
77     */
78    public function __construct($basePath, $tag = null)
79    {
80        parent::__construct($basePath);
81        if (null !== $tag) {
82            $this->wrappingTag = $tag;
83        }
84    }
85
86    /**
87     * Support method for building combined XML document.
88     *
89     * @param string $innerXML XML for inside of document.
90     *
91     * @return string
92     */
93    protected function getCombinedXML($innerXML)
94    {
95        // Determine start and end tags from configuration:
96        $start = $this->wrappingTag;
97        $tmp = explode(' ', $start);
98        $end = '</' . str_replace(['<', '>'], '', $tmp[0]) . '>';
99
100        // Assemble the document:
101        return $start . $innerXML . $end;
102    }
103
104    /**
105     * Called before the writing process begins.
106     *
107     * @return void
108     */
109    public function beginWrite()
110    {
111        $this->deletedIds = [];
112        $this->innerXML = '';
113        $this->firstHarvestedId = false;
114    }
115
116    /**
117     * Add the ID of a deleted record.
118     *
119     * @param string $id ID
120     *
121     * @return void
122     */
123    public function addDeletedRecord($id)
124    {
125        $this->deletedIds[] = $id;
126    }
127
128    /**
129     * Add a non-deleted record.
130     *
131     * @param string $id     ID
132     * @param string $record Record XML
133     *
134     * @return void
135     */
136    public function addRecord($id, $record)
137    {
138        $this->innerXML .= $record;
139        if (false === $this->firstHarvestedId) {
140            $this->firstHarvestedId = $id;
141        }
142    }
143
144    /**
145     * Close out the writing process.
146     *
147     * @return void
148     */
149    public function endWrite()
150    {
151        if (false !== $this->firstHarvestedId) {
152            $this->saveFile(
153                $this->firstHarvestedId,
154                $this->getCombinedXML($this->innerXML)
155            );
156        }
157
158        if (!empty($this->deletedIds)) {
159            $this->saveDeletedRecords($this->deletedIds);
160        }
161    }
162}