Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 21 |
|
0.00% |
0 / 2 |
CRAP | |
0.00% |
0 / 1 |
| ServiceDelegator | |
0.00% |
0 / 21 |
|
0.00% |
0 / 2 |
90 | |
0.00% |
0 / 1 |
| getResponsibleService | |
0.00% |
0 / 11 |
|
0.00% |
0 / 1 |
30 | |||
| registerHandler | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
20 | |||
| 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) 2017 (original work) Open Assessment Technologies SA; |
| 19 | * |
| 20 | * @author Alexander Zagovorichev <zagovorichev@1pt.com> |
| 21 | */ |
| 22 | |
| 23 | namespace oat\taoProctoring\model; |
| 24 | |
| 25 | use oat\oatbox\service\ConfigurableService; |
| 26 | use oat\oatbox\user\User; |
| 27 | |
| 28 | abstract class ServiceDelegator extends ConfigurableService implements ServiceDelegatorInterface |
| 29 | { |
| 30 | /** |
| 31 | * @var ConfigurableService |
| 32 | */ |
| 33 | private $service; |
| 34 | |
| 35 | /** |
| 36 | * Returns applicable service |
| 37 | * |
| 38 | * @throws \common_exception_NoImplementation |
| 39 | * @param $user |
| 40 | * @param $deliveryId |
| 41 | * @return DelegatedServiceHandler |
| 42 | */ |
| 43 | public function getResponsibleService(User $user, $deliveryId = null) |
| 44 | { |
| 45 | if (!isset($this->service)) { |
| 46 | /** @var DelegatedServiceHandler $handler */ |
| 47 | foreach ($this->getOption(self::SERVICE_HANDLERS) as $handler) { |
| 48 | if (!is_a($handler, DelegatedServiceHandler::class)) { |
| 49 | throw new \common_exception_NoImplementation( |
| 50 | 'Handler should be instance of DelegatorServiceHandler.' |
| 51 | ); |
| 52 | } |
| 53 | $handler->setServiceLocator($this->getServiceLocator()); |
| 54 | if ($handler->isSuitable($user, $deliveryId)) { |
| 55 | $this->service = $handler; |
| 56 | break; |
| 57 | } |
| 58 | } |
| 59 | } |
| 60 | return $this->service; |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * @param $handler |
| 65 | */ |
| 66 | public function registerHandler(DelegatedServiceHandler $handler) |
| 67 | { |
| 68 | $handlers = $this->getOption(self::SERVICE_HANDLERS); |
| 69 | $exists = false; |
| 70 | |
| 71 | // change options on the existing handlers |
| 72 | foreach ($handlers as $key => $_handler) { |
| 73 | if ($_handler instanceof $handler) { |
| 74 | $handlers[$key] = $handler; |
| 75 | $exists = true; |
| 76 | break; |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | // new handler should be added to the top of the list |
| 81 | if (!$exists) { |
| 82 | $handlers = array_merge([$handler], $handlers); |
| 83 | } |
| 84 | |
| 85 | $this->setOption(self::SERVICE_HANDLERS, $handlers); |
| 86 | } |
| 87 | } |