Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 29
0.00% covered (danger)
0.00%
0 / 8
CRAP
0.00% covered (danger)
0.00%
0 / 1
common_ext_ExtensionUpdater
0.00% covered (danger)
0.00%
0 / 29
0.00% covered (danger)
0.00%
0 / 8
156
0.00% covered (danger)
0.00%
0 / 1
 update
n/a
0 / 0
n/a
0 / 0
0
 postUpdate
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 setVersion
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 isVersion
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 isBetween
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
6
 skip
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
12
 getReports
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 addReport
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 safeLoadService
0.00% covered (danger)
0.00%
0 / 18
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2
3/**
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; under version 2
7 * of the License (non-upgradable).
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
17 *
18 * Copyright (c) 2008-2010 (original work) Deutsche Institut für Internationale Pädagogische Forschung
19 *                         (under the project TAO-TRANSFER);
20 *               2009-2012 (update and modification) Public Research Centre Henri Tudor
21 *                         (under the project TAO-SUSTAIN & TAO-DEV);
22 *
23 */
24
25declare(strict_types=1);
26
27use oat\oatbox\service\ServiceManagerAwareTrait;
28use oat\oatbox\service\ServiceManagerAwareInterface;
29use common_report_Report as Report;
30
31/**
32 * Short description of class common_ext_ExtensionInstaller
33 *
34 * @access public
35 * @author lionel.lecaque@tudor.lu
36 * @package generis
37 * @see @license  GNU General Public (GPL) Version 2 http://www.opensource.org/licenses/gpl-2.0.php
38
39 */
40abstract class common_ext_ExtensionUpdater extends common_ext_ExtensionHandler implements ServiceManagerAwareInterface
41{
42    use ServiceManagerAwareTrait;
43
44    /** @var Report[] */
45    private $reports = [];
46
47    /**
48     *
49     * @param string $currentVersion
50     * @return string $versionUpdatedTo
51     */
52    abstract public function update($initialVersion);
53
54    /**
55     * @return Report|null
56     */
57    public function postUpdate(): ?Report
58    {
59        //to be replaced in child class if needed
60        return null;
61    }
62
63    /**
64     * Update the current version of the extension to the provided version
65     * Ensures that a successful update doesn't get executed twice
66     *
67     * @param string $version
68     */
69    public function setVersion($version)
70    {
71        common_ext_ExtensionsManager::singleton()->updateVersion($this->getExtension(), $version);
72    }
73
74    /**
75     * Test if $version is the current version
76     *
77     * @param string $version
78     * @return boolean
79     */
80    public function isVersion($version)
81    {
82        $extensionsManager = $this->getServiceManager()->get(common_ext_ExtensionsManager::SERVICE_ID);
83        return $version == $extensionsManager->getInstalledVersion($this->getExtension()->getId());
84    }
85
86    /**
87     * Please use "skip" instead of inBetween.
88     *
89     * @param string $minVersion
90     * @param string $maxVersion
91     * @return boolean
92     */
93    public function isBetween($minVersion, $maxVersion)
94    {
95        $current = common_ext_ExtensionsManager::singleton()->getInstalledVersion($this->getExtension()->getId());
96        return version_compare($minVersion, $current, '<=') && version_compare($current, $maxVersion, '<=');
97    }
98
99    /**
100     * Skip from version FROM to version TO without additional required actions
101     *
102     * @param string $from
103     * @param string $to
104     */
105    public function skip($from, $to)
106    {
107        $current = common_ext_ExtensionsManager::singleton()->getInstalledVersion($this->getExtension()->getId());
108        if (version_compare($from, $current, '<=') && version_compare($current, $to, '<')) {
109            $this->setVersion($to);
110        }
111    }
112
113    /**
114     * @return Report[]
115     */
116    public function getReports()
117    {
118        return $this->reports;
119    }
120
121    /**
122     * @param Report $report
123     */
124    public function addReport(Report $report)
125    {
126        $this->reports[] = $report;
127    }
128
129    /**
130     * Loads a service in a "safe" way, trying to convert
131     * unknown classes to abstract services
132     *
133     * @param string $configId
134     * @return
135     */
136    public function safeLoadService($configId)
137    {
138        /**
139         * Inline autoloader that will construct a new class based on ConfigurableService
140         * @param string $class_name
141         */
142        $missingClasses = [];
143
144        $fallbackAutoload = function ($class_name) use (&$missingClasses) {
145            $missingClasses[] = $class_name;
146            $split = strrpos($class_name, '\\');
147            if ($split == false) {
148                $result = eval('class ' . $class_name . ' extends oat\\oatbox\\service\\ConfigurableService {}');
149            } else {
150                $namespace = substr($class_name, 0, $split);
151                $class = substr($class_name, $split + 1);
152                eval(
153                    'namespace ' . $namespace . '; ' . 'class ' . $class
154                        . ' extends \\oat\\oatbox\\service\\ConfigurableService {}'
155                );
156            }
157        };
158        $serviceManager = $this->getServiceManager();
159        spl_autoload_register($fallbackAutoload);
160        $service = $serviceManager->get($configId);
161        spl_autoload_unregister($fallbackAutoload);
162
163        return $service;
164    }
165}