Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 16 |
|
0.00% |
0 / 2 |
CRAP | |
0.00% |
0 / 1 |
| MicrotimeRandUriProvider | |
0.00% |
0 / 16 |
|
0.00% |
0 / 2 |
12 | |
0.00% |
0 / 1 |
| getPersistence | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
| provide | |
0.00% |
0 / 12 |
|
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) 2002-2008 (original work) Public Research Centre Henri Tudor & University of Luxembourg |
| 19 | * (under the project TAO & TAO2); |
| 20 | * 2008-2010 (update and modification) Deutsche Institut für Internationale Pädagogische Forschung |
| 21 | * (under the project TAO-TRANSFER); |
| 22 | * 2009-2012 (update and modification) Public Research Centre Henri Tudor |
| 23 | * (under the project TAO-SUSTAIN & TAO-DEV); |
| 24 | * 2013 (update and modification) Open Assessment Technologies SA; |
| 25 | */ |
| 26 | |
| 27 | namespace oat\generis\model\kernel\uri; |
| 28 | |
| 29 | use oat\oatbox\service\ConfigurableService; |
| 30 | use oat\generis\persistence\PersistenceManager; |
| 31 | use common_persistence_SqlPersistence; |
| 32 | |
| 33 | /** |
| 34 | * UriProvider implementation based on PHP microtime and rand(). |
| 35 | * |
| 36 | * @access public |
| 37 | * @author Jerome Bogaerts, <jerome.bogaerts@tudor.lu> |
| 38 | * @package generis |
| 39 | |
| 40 | */ |
| 41 | class MicrotimeRandUriProvider extends ConfigurableService implements UriProvider |
| 42 | { |
| 43 | public const OPTION_PERSISTENCE = 'persistence'; |
| 44 | |
| 45 | public const OPTION_NAMESPACE = 'namespace'; |
| 46 | // --- ASSOCIATIONS --- |
| 47 | |
| 48 | // --- ATTRIBUTES --- |
| 49 | |
| 50 | // --- OPERATIONS --- |
| 51 | |
| 52 | /** |
| 53 | * @return common_persistence_SqlPersistence |
| 54 | */ |
| 55 | public function getPersistence() |
| 56 | { |
| 57 | return $this |
| 58 | ->getServiceLocator() |
| 59 | ->get(PersistenceManager::SERVICE_ID) |
| 60 | ->getPersistenceById($this->getOption(self::OPTION_PERSISTENCE)); |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Generates a URI based on the value of PHP microtime() and rand(). |
| 65 | * |
| 66 | * @access public |
| 67 | * @author Jerome Bogaerts, <jerome.bogaerts@tudor.lu> |
| 68 | * @return string |
| 69 | */ |
| 70 | public function provide() |
| 71 | { |
| 72 | |
| 73 | $uriExist = false; |
| 74 | do { |
| 75 | list($usec, $sec) = explode(" ", microtime()); |
| 76 | $uri = $this->getOption(self::OPTION_NAMESPACE) . 'i' . (str_replace(".", "", $sec |
| 77 | . "" . $usec)) . rand(0, 1000); |
| 78 | $sqlResult = $this->getPersistence()->query( |
| 79 | "SELECT COUNT(subject) AS num FROM statements WHERE subject = '" . $uri . "'" |
| 80 | ); |
| 81 | if ($row = $sqlResult->fetch()) { |
| 82 | $uriExist = $row['num'] > 0; |
| 83 | $sqlResult->closeCursor(); |
| 84 | } |
| 85 | } while ($uriExist); |
| 86 | |
| 87 | return (string) $uri; |
| 88 | } |
| 89 | } |