Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
| Total | |
0.00% |
0 / 1 |
|
0.00% |
0 / 10 |
CRAP | |
0.00% |
0 / 91 |
| Banner | |
0.00% |
0 / 1 |
|
0.00% |
0 / 10 |
342.00 | |
0.00% |
0 / 91 |
| getPathPattern | |
0.00% |
0 / 1 |
2.00 | |
0.00% |
0 / 5 |
|||
| getPath | |
0.00% |
0 / 1 |
2.00 | |
0.00% |
0 / 6 |
|||
| getImageData | |
0.00% |
0 / 1 |
6.00 | |
0.00% |
0 / 19 |
|||
| getImageUrl | |
0.00% |
0 / 1 |
12.00 | |
0.00% |
0 / 19 |
|||
| delete | |
0.00% |
0 / 1 |
6.00 | |
0.00% |
0 / 10 |
|||
| findFile | |
0.00% |
0 / 1 |
6.00 | |
0.00% |
0 / 8 |
|||
| findFiles | |
0.00% |
0 / 1 |
6.00 | |
0.00% |
0 / 8 |
|||
| mimeContentType | |
0.00% |
0 / 1 |
6.00 | |
0.00% |
0 / 6 |
|||
| findOne | |
0.00% |
0 / 1 |
2.00 | |
0.00% |
0 / 3 |
|||
| findAll | |
0.00% |
0 / 1 |
6.00 | |
0.00% |
0 / 7 |
|||
| <?php | |
| namespace common\models; | |
| use Yii; | |
| use yii\helpers\Url; | |
| /** | |
| * This is the model class for Banner files. | |
| * | |
| * @property string $key | |
| * @property string $path | |
| * @property string $url | |
| */ | |
| class Banner extends \yii\base\Model | |
| { | |
| private static $definitions = [ | |
| 'home' => [ | |
| 'app-frontend', | |
| '@frontend/web/home-banner.', | |
| '@web' | |
| ], | |
| 'admin-home' => [ | |
| 'app-backend', | |
| '@backend/web/home-banner.', | |
| '@web' | |
| ] | |
| ]; | |
| private static $mimeDictionary = [ | |
| 'gif' => 'image/gif', | |
| 'jpeg' => 'image/jpeg', | |
| 'jpg' => 'image/jpeg', | |
| 'png' => 'image/png', | |
| 'svg' => 'image/svg+xml', | |
| ]; | |
| public $key; | |
| /** | |
| * Gets configured path to the image | |
| * @return string | |
| */ | |
| public function getPathPattern() | |
| { | |
| $path = self::$definitions[$this->key][1]; | |
| $realPath = Yii::getAlias($path); | |
| return $realPath; | |
| } | |
| /** | |
| * Gets real path to the image | |
| * null if image not found | |
| * @return string|null | |
| */ | |
| public function getPath() | |
| { | |
| $path = self::$definitions[$this->key][1]; | |
| $realPath = Yii::getAlias($path); | |
| $foundPath = $this->findFile($realPath); | |
| return $foundPath; | |
| } | |
| /** | |
| * Gets image represented in base64 encoding | |
| * placeholder if image not found | |
| * @return string | |
| */ | |
| public function getImageData() | |
| { | |
| $path = self::$definitions[$this->key][1]; | |
| $realPath = Yii::getAlias($path); | |
| $foundPath = $this->findFile($realPath); | |
| if (empty($foundPath)) { | |
| $height = 140; | |
| $width = 700; | |
| $text = 'No banner image'; | |
| $data = "<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 $width $height' preserveAspectRatio='none meet' width='$width' height='$height'><defs /><rect fill='#eeeeee' width='$width' height='$height' /><g><text style='font-family: Arial, Helvetica, Open Sans, sans-serif, monospace; font-size: 12pt; font-weight: bold; dominant-baseline: central; fill: #aaaaaa; text-align: center;' x='50%' y='70' text-anchor='middle'>$text</text></g></svg>"; | |
| $mime = 'image/svg+xml'; | |
| } else { | |
| $pathInfo = pathinfo($foundPath); | |
| $extension = $pathInfo['extension']; | |
| $data = file_get_contents($foundPath); | |
| $mime = $this->mimeContentType($extension); | |
| } | |
| $encodedImage = base64_encode($data); | |
| return "data:$mime;base64,$encodedImage"; | |
| } | |
| /** | |
| * Gets image url for running application | |
| * @return string|null | |
| */ | |
| public function getImageUrl() | |
| { | |
| $currentAppId = Yii::$app->id; | |
| $appId = self::$definitions[$this->key][0]; | |
| if (stripos($currentAppId, $appId) !== false) { | |
| $path = self::$definitions[$this->key][1]; | |
| $realPath = Yii::getAlias($path); | |
| $foundPath = $this->findFile($realPath); | |
| if (empty($foundPath)) { | |
| return null; | |
| } else { | |
| $pathInfo = pathinfo($foundPath); | |
| $filename = $pathInfo['basename']; | |
| $urlRoot = self::$definitions[$this->key][2]; | |
| return Url::to("$urlRoot/$filename"); | |
| } | |
| } else { | |
| throw new \yii\base\InvalidConfigException("Given banner key '$this->key' is not valid for application '$currentAppId'."); | |
| } | |
| } | |
| /** | |
| * Deletes all images that could be found for given key | |
| * @return bool | |
| */ | |
| public function delete() | |
| { | |
| $result = true; | |
| $path = self::$definitions[$this->key][1]; | |
| $realPath = Yii::getAlias($path); | |
| $foundFiles = $this->findFiles($realPath); | |
| foreach ($foundFiles as $file) { | |
| $result &= unlink($file); | |
| } | |
| return $result; | |
| } | |
| /** | |
| * Finds first file that meet file location and name (extension is gussed) | |
| * @return string|null | |
| */ | |
| private function findFile($path) | |
| { | |
| $files = glob("$path*"); | |
| if (count($files) > 0) { | |
| return $files[0]; | |
| } else { | |
| return null; | |
| } | |
| } | |
| /** | |
| * Finds all files that meet file location and name (extension is gussed) | |
| * @return string[] | |
| */ | |
| private function findFiles($path) | |
| { | |
| $files = glob("$path*"); | |
| if (count($files) > 0) { | |
| return $files; | |
| } else { | |
| return []; | |
| } | |
| } | |
| /** | |
| * Finds all files that meet file location and name (extension is gussed) | |
| * @return string|null | |
| */ | |
| private function mimeContentType($fileExtension) | |
| { | |
| if (array_key_exists($fileExtension, self::$mimeDictionary)) { | |
| return self::$mimeDictionary[$fileExtension]; | |
| } | |
| return null; | |
| } | |
| /** | |
| * Finds Banner model with given key | |
| * @return Banner | |
| */ | |
| public static function findOne($key) | |
| { | |
| return new Banner(['key' => $key]); | |
| } | |
| /** | |
| * Finds all Banner models | |
| * @return Banner[] | |
| */ | |
| public static function findAll() | |
| { | |
| $banners = []; | |
| foreach (self::$definitions as $key => $definition) { | |
| $banners[] = self::findOne($key); | |
| } | |
| return $banners; | |
| } | |
| } |