jacobemerick /
web
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php |
||
| 2 | |||
| 3 | Loader::load('utility', 'Content'); |
||
| 4 | |||
| 5 | final class FetchFirstPhotoContent extends Content |
||
| 6 | { |
||
| 7 | |||
| 8 | private static $PHOTO_PLACEHOLDER_MATCH = '@{{photo="(.*)"}}@'; |
||
| 9 | private static $IMAGE_NODE = '<img src="%sphoto/%s/%s-size-%s.%s" height="%d" width="%d" alt="%s" />'; |
||
| 10 | private static $DEFAULT_RETURN = ''; |
||
| 11 | |||
| 12 | protected function execute($is_absolute = false, $size = 'thumb') |
||
| 13 | { |
||
| 14 | if(preg_match(self::$PHOTO_PLACEHOLDER_MATCH, $this->content, $match) === 1) |
||
| 15 | $this->content = $this->get_thumb($match[1], $is_absolute, $size); |
||
| 16 | else |
||
| 17 | $this->content = self::$DEFAULT_RETURN; |
||
| 18 | return; |
||
| 19 | } |
||
| 20 | |||
| 21 | private function get_file_path($category, $photo, $size, $extension) |
||
| 22 | { |
||
| 23 | $path = "{$category}/{$photo}-size-{$size}.{$extension}"; |
||
| 24 | return Loader::getImagePath('photo', $path); |
||
| 25 | } |
||
| 26 | |||
| 27 | View Code Duplication | private function get_thumb($string, $is_absolute, $size) |
|
|
0 ignored issues
–
show
|
|||
| 28 | { |
||
| 29 | list($category, $file_name) = explode('/', $string); |
||
| 30 | list($photo, $extension) = explode('.', $file_name); |
||
| 31 | |||
| 32 | $file_path = $this->get_file_path($category, $photo, $size, $extension); |
||
| 33 | $file_size = getimagesize($file_path); |
||
| 34 | |||
| 35 | Loader::load('collector', 'image/PhotoCollector'); |
||
| 36 | $photo_result = PhotoCollector::fetchRow($category, $photo); |
||
| 37 | if($photo_result == false) |
||
| 38 | return ''; |
||
| 39 | |||
| 40 | $height = $file_size[1]; |
||
| 41 | $width = $file_size[0]; |
||
| 42 | $description = $photo_result->description; |
||
| 43 | |||
| 44 | $domain = '/'; |
||
| 45 | if($is_absolute) |
||
| 46 | $domain = Loader::getRootUrl(); |
||
| 47 | |||
| 48 | return sprintf(self::$IMAGE_NODE, $domain, $category, $photo, $size, $extension, $height, $width, $description); |
||
| 49 | } |
||
| 50 | |||
| 51 | } |
||
| 52 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.