Code Coverage  | 
      ||||||||||
Lines  | 
       Functions and Methods  | 
       Classes and Traits  | 
      ||||||||
| Total |         | 
       0.00%  | 
       0 / 31  | 
               | 
       0.00%  | 
       0 / 4  | 
       CRAP |         | 
       0.00%  | 
       0 / 1  | 
      
| SaveStylesheetClassesService |         | 
       0.00%  | 
       0 / 31  | 
               | 
       0.00%  | 
       0 / 4  | 
       182 |         | 
       0.00%  | 
       0 / 1  | 
      
| save |         | 
       0.00%  | 
       0 / 12  | 
               | 
       0.00%  | 
       0 / 1  | 
       12 | |||
| removeStoredStylesheet |         | 
       0.00%  | 
       0 / 3  | 
               | 
       0.00%  | 
       0 / 1  | 
       6 | |||
| getCssContentFromArray |         | 
       0.00%  | 
       0 / 13  | 
               | 
       0.00%  | 
       0 / 1  | 
       42 | |||
| getStylesheetRepository |         | 
       0.00%  | 
       0 / 3  | 
               | 
       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) 2021 (original work) Open Assessment Technologies SA; | 
| 19 | */ | 
| 20 | |
| 21 | declare(strict_types=1); | 
| 22 | |
| 23 | namespace oat\taoMediaManager\model\sharedStimulus\css\service; | 
| 24 | |
| 25 | use Exception; | 
| 26 | use oat\oatbox\filesystem\FilesystemException; | 
| 27 | use oat\oatbox\service\ConfigurableService; | 
| 28 | use oat\taoMediaManager\model\sharedStimulus\css\dto\SaveStylesheetClasses; | 
| 29 | use oat\taoMediaManager\model\sharedStimulus\css\repository\StylesheetRepository; | 
| 30 | |
| 31 | class SaveStylesheetClassesService extends ConfigurableService | 
| 32 | { | 
| 33 | public const STYLESHEET_WARNING_HEADER = " /* Do not edit */" . "\n"; | 
| 34 | |
| 35 | /** @var StylesheetRepository */ | 
| 36 | private $stylesheetRepository; | 
| 37 | |
| 38 | public function save(SaveStylesheetClasses $saveStylesheetClassesDTO): void | 
| 39 | { | 
| 40 | $path = $this->getStylesheetRepository()->getPath($saveStylesheetClassesDTO->getUri()); | 
| 41 | |
| 42 | if ($path === '.') { | 
| 43 | throw new Exception('Shared stimulus stored as single file'); | 
| 44 | } | 
| 45 | |
| 46 | $cssClassesArray = $saveStylesheetClassesDTO->getCssClassesArray(); | 
| 47 | |
| 48 | if (empty($cssClassesArray)) { | 
| 49 | $this->removeStoredStylesheet($path . DIRECTORY_SEPARATOR . $saveStylesheetClassesDTO->getStylesheetUri()); | 
| 50 | |
| 51 | return; | 
| 52 | } | 
| 53 | |
| 54 | $content = $this->getCssContentFromArray($cssClassesArray); | 
| 55 | $this->getStylesheetRepository()->write( | 
| 56 | $path . DIRECTORY_SEPARATOR . $saveStylesheetClassesDTO->getStylesheetUri(), | 
| 57 | $content | 
| 58 | ); | 
| 59 | } | 
| 60 | |
| 61 | private function removeStoredStylesheet(string $path): void | 
| 62 | { | 
| 63 | try { | 
| 64 | $this->getStylesheetRepository()->delete($path); | 
| 65 | } catch (FilesystemException $exception) { | 
| 66 | $this->logDebug(sprintf('Stylesheet %s to delete was not found when trying to clear styles', $path)); | 
| 67 | } | 
| 68 | } | 
| 69 | |
| 70 | private function getCssContentFromArray(array $array): string | 
| 71 | { | 
| 72 | // Todo clarify if can we use taoQtiItem/helpers/CssHelper.php here? For now duplicating the code | 
| 73 | $css = self::STYLESHEET_WARNING_HEADER; | 
| 74 | |
| 75 | // rebuild CSS | 
| 76 | foreach ($array as $key1 => $value1) { | 
| 77 | $css .= $key1 . '{'; | 
| 78 | |
| 79 | foreach ($value1 as $key2 => $value2) { | 
| 80 | // in the case that the code is embedded in a media query | 
| 81 | if (is_array($value2)) { | 
| 82 | foreach ($value2 as $value3) { | 
| 83 | $css .= $key2 . '{'; | 
| 84 | foreach ($value3 as $mProp) { | 
| 85 | $css .= $mProp . ':' . $value3 . ';'; | 
| 86 | } | 
| 87 | $css .= '}'; | 
| 88 | } | 
| 89 | } else { // regular selectors | 
| 90 | $css .= $key2 . ':' . $value2 . ';'; | 
| 91 | } | 
| 92 | } | 
| 93 | $css .= "}\n"; | 
| 94 | } | 
| 95 | return $css; | 
| 96 | } | 
| 97 | |
| 98 | private function getStylesheetRepository(): StylesheetRepository | 
| 99 | { | 
| 100 | if (!isset($this->stylesheetRepository)) { | 
| 101 | $this->stylesheetRepository = $this->getServiceLocator()->get(StylesheetRepository::class); | 
| 102 | } | 
| 103 | |
| 104 | return $this->stylesheetRepository; | 
| 105 | } | 
| 106 | } |