Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 13
0.00% covered (danger)
0.00%
0 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
StateManager
0.00% covered (danger)
0.00%
0 / 13
0.00% covered (danger)
0.00%
0 / 6
90
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 clearState
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
6
 loadDate
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
6
 loadState
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
6
 saveDate
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 saveState
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3/**
4 * OAI-PMH State Manager (for persisting harvest state)
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
32/**
33 * OAI-PMH State Manager (for persisting harvest state)
34 *
35 * This class actually serves two distinct functions:
36 *
37 * Long-term state management: remembering/retrieving the end date of the most
38 * recent harvest through saveDate()/loadDate().
39 *
40 * Short-term state management: remembering resumption tokens to allow for
41 * continuation of an interrupted harvest through saveState()/loadState().
42 *
43 * @category VuFind
44 * @package  Harvest_Tools
45 * @author   Demian Katz <demian.katz@villanova.edu>
46 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
47 * @link     https://vufind.org/wiki/indexing:oai-pmh Wiki
48 */
49class StateManager
50{
51    /**
52     * File for tracking last harvest date
53     *
54     * @var string
55     */
56    protected $lastHarvestFile;
57
58    /**
59     * File for tracking last harvest state (for continuing interrupted
60     * connection).
61     *
62     * @var string
63     */
64    protected $lastStateFile;
65
66    /**
67     * Constructor.
68     *
69     * @param string $basePath Directory to contain state files
70     */
71    public function __construct($basePath)
72    {
73        // Check if there is a file containing a start date:
74        $this->lastHarvestFile = $basePath . 'last_harvest.txt';
75        $this->lastStateFile = $basePath . 'last_state.txt';
76    }
77
78    /**
79     * Clear the state most recently saved to saveState().
80     *
81     * @return void
82     */
83    public function clearState()
84    {
85        if (file_exists($this->lastStateFile)) {
86            unlink($this->lastStateFile);
87        }
88    }
89
90    /**
91     * Retrieve the date from the "last harvested" file and use it as our start
92     * date if it is available.
93     *
94     * @return string
95     */
96    public function loadDate()
97    {
98        return (file_exists($this->lastHarvestFile))
99            ? trim(current(file($this->lastHarvestFile))) : null;
100    }
101
102    /**
103     * Load the last saved harvest state. Returns an array of
104     * [set, resumption token, start date] if found; false otherwise.
105     *
106     * @return array|bool
107     */
108    public function loadState()
109    {
110        return file_exists($this->lastStateFile)
111            ? explode("\t", file_get_contents($this->lastStateFile)) : false;
112    }
113
114    /**
115     * Save a date to the "last harvested" file.
116     *
117     * @param string $date Date to save.
118     *
119     * @return void
120     */
121    public function saveDate($date)
122    {
123        file_put_contents($this->lastHarvestFile, $date);
124    }
125
126    /**
127     * Save a harvest state.
128     *
129     * @param string $set            Set being harvested
130     * @param string $token          Current resumption token
131     * @param string $startDate      Start date of harvest
132     * @param string $harvestEndDate End date of harvest
133     *
134     * @return void
135     */
136    public function saveState($set, $token, $startDate, $harvestEndDate)
137    {
138        file_put_contents(
139            $this->lastStateFile,
140            "$set\t$token\t$startDate\t$harvestEndDate"
141        );
142    }
143}