Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
80.00% covered (warning)
80.00%
20 / 25
66.67% covered (warning)
66.67%
2 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
SetLoader
80.00% covered (warning)
80.00%
20 / 25
66.67% covered (warning)
66.67%
2 / 3
8.51
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 sendRequest
37.50% covered (danger)
37.50%
3 / 8
0.00% covered (danger)
0.00%
0 / 1
2.98
 getNames
100.00% covered (success)
100.00%
16 / 16
100.00% covered (success)
100.00%
1 / 1
5
1<?php
2
3/**
4 * OAI-PMH Harvest Tool
5 *
6 * PHP version 7
7 *
8 * Copyright (c) Demian Katz 2010.
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\OaiPmh;
31
32use VuFindHarvest\ConsoleOutput\WriterAwareTrait;
33
34use function count;
35
36/**
37 * OAI-PMH Harvest Tool
38 *
39 * @category VuFind
40 * @package  Harvest_Tools
41 * @author   Demian Katz <demian.katz@villanova.edu>
42 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
43 * @link     https://vufind.org/wiki/indexing:oai-pmh Wiki
44 */
45class SetLoader
46{
47    use WriterAwareTrait;
48
49    /**
50     * Low-level OAI-PMH communicator
51     *
52     * @var Communicator
53     */
54    protected $communicator;
55
56    /**
57     * Constructor.
58     *
59     * @param Communicator $communicator Low-level API client
60     */
61    public function __construct(Communicator $communicator)
62    {
63        $this->communicator = $communicator;
64    }
65
66    /**
67     * Make an OAI-PMH request.  Die if there is an error; return a SimpleXML object
68     * on success.
69     *
70     * @param string $verb   OAI-PMH verb to execute.
71     * @param array  $params GET parameters for ListRecords method.
72     *
73     * @return object        SimpleXML-formatted response.
74     */
75    protected function sendRequest($verb, $params = [])
76    {
77        $result = $this->communicator->request($verb, $params);
78        // Detect errors and die if one is found:
79        if ($result->error) {
80            $attribs = $result->error->attributes();
81            throw new \Exception(
82                "OAI-PMH error -- code: {$attribs['code']}" .
83                "value: {$result->error}"
84            );
85        }
86        return $result;
87    }
88
89    /**
90     * Load set list from the server.
91     *
92     * @return array
93     */
94    public function getNames()
95    {
96        $this->write('Loading set list... ');
97
98        // On the first pass through the following loop, we want to get the
99        // first page of sets without using a resumption token:
100        $params = [];
101
102        $setNames = [];
103
104        // Grab set information until we have it all (at which point we will
105        // break out of this otherwise-infinite loop):
106        do {
107            // Process current page of results:
108            $response = $this->sendRequest('ListSets', $params);
109            if (isset($response->ListSets->set)) {
110                foreach ($response->ListSets->set as $current) {
111                    $spec = (string)$current->setSpec;
112                    $name = (string)$current->setName;
113                    if (!empty($spec)) {
114                        $setNames[$spec] = $name;
115                    }
116                }
117            }
118
119            // Is there a resumption token?  If so, continue looping; if not,
120            // we're done!
121            $params['resumptionToken']
122                = !empty($response->ListSets->resumptionToken)
123                ? (string)$response->ListSets->resumptionToken : '';
124        } while (!empty($params['resumptionToken']));
125        $this->writeLine('found ' . count($setNames));
126        return $setNames;
127    }
128}