Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
78.12% covered (warning)
78.12%
25 / 32
60.00% covered (warning)
60.00%
3 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
AbstractThemeUtility
78.12% covered (warning)
78.12%
25 / 32
60.00% covered (warning)
60.00%
3 / 5
24.19
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
 getLastError
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 copyDir
70.59% covered (warning)
70.59%
12 / 17
0.00% covered (danger)
0.00%
0 / 1
12.54
 deleteDir
81.82% covered (warning)
81.82%
9 / 11
0.00% covered (danger)
0.00%
0 / 1
7.29
 setLastError
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3/**
4 * Abstract base class to hold shared logic for theme utilities.
5 *
6 * PHP version 8
7 *
8 * Copyright (C) Villanova University 2017.
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  Theme
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 Main Site
28 */
29
30namespace VuFindTheme;
31
32/**
33 * Abstract base class to hold shared logic for theme utilities.
34 *
35 * @category VuFind
36 * @package  Theme
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 Main Site
40 */
41abstract class AbstractThemeUtility
42{
43    /**
44     * Theme info object
45     *
46     * @var ThemeInfo
47     */
48    protected $info;
49
50    /**
51     * Last error message
52     *
53     * @var string
54     */
55    protected $lastError = null;
56
57    /**
58     * Constructor
59     *
60     * @param ThemeInfo $info Theme info object
61     */
62    public function __construct(ThemeInfo $info)
63    {
64        $this->info = $info;
65    }
66
67    /**
68     * Get last error message.
69     *
70     * @return string
71     */
72    public function getLastError()
73    {
74        return $this->lastError;
75    }
76
77    /**
78     * Copy the contents of $src into $dest if no matching files already exist.
79     *
80     * @param string $src  Source directory
81     * @param string $dest Target directory
82     *
83     * @return bool
84     */
85    protected function copyDir($src, $dest)
86    {
87        if (!is_dir($dest)) {
88            if (!mkdir($dest)) {
89                return $this->setLastError("Cannot create $dest");
90            }
91        }
92        $dir = opendir($src);
93        while ($current = readdir($dir)) {
94            if ($current === '.' || $current === '..') {
95                continue;
96            }
97            if (is_dir("$src/$current")) {
98                if (!$this->copyDir("$src/$current", "$dest/$current")) {
99                    return false;
100                }
101            } elseif (
102                !file_exists("$dest/$current")
103                && !copy("$src/$current", "$dest/$current")
104            ) {
105                return $this->setLastError(
106                    "Cannot copy $src/$current to $dest/$current."
107                );
108            }
109        }
110        closedir($dir);
111        return true;
112    }
113
114    /**
115     * Recursively delete a directory and its contents.
116     *
117     * @param string $path Directory to delete.
118     *
119     * @return bool
120     */
121    protected function deleteDir($path)
122    {
123        $dir = opendir($path);
124        while ($current = readdir($dir)) {
125            if ($current === '.' || $current === '..') {
126                continue;
127            }
128            if (is_dir("$path/$current")) {
129                if (!$this->deleteDir("$path/$current")) {
130                    return false;
131                }
132            } elseif (!unlink("$path/$current")) {
133                return $this->setLastError("Cannot delete $path/$current");
134            }
135        }
136        closedir($dir);
137        return rmdir($path);
138    }
139
140    /**
141     * Set last error message and return a boolean false.
142     *
143     * @param string $error Error message.
144     *
145     * @return bool
146     */
147    protected function setLastError($error)
148    {
149        $this->lastError = $error;
150        return false;
151    }
152}