Completed
Pull Request — master (#4)
by Jacob
05:15
created

utility/content/FixPhotoContent.class.inc.php (1 issue)

Upgrade to new PHP Analysis Engine

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 FixPhotoContent extends Content
6
{
7
8
	private static $PHOTO_PLACEHOLDER_MATCH = '@{{photo="(.*)"}}@';
9
	private static $ERROR_CONTENT = '<div class="photo-holder"><p class="photo-caption">Image (%s) could not be found!</p></div>';
10
	private static $PHOTO_CONTENT = '<div class="photo-holder"><img src="%sphoto/%s/%s-size-%s.jpg" height="%d" width="%d" alt="%s" /><p class="photo-caption">%s</p></div>';
11
12
	protected function execute($is_absolute = false, $size = 'medium')
13
	{
14
		preg_match_all(self::$PHOTO_PLACEHOLDER_MATCH, $this->content, $matches);
15
		foreach($matches[1] as $key => $match)
16
		{
17
			$photo_content = $this->get_photo_content($match, $size, $is_absolute);
18
			$this->content = str_replace($matches[0][$key], $photo_content, $this->content);
19
		}
20
		return;
21
	}
22
23
	private function get_file_path($category, $photo, $size, $extension)
24
	{
25
		$path = "{$category}/{$photo}-size-{$size}.{$extension}";
26
		return Loader::getImagePath('photo', $path);
27
	}
28
29 View Code Duplication
	private function get_photo_content($string, $size, $is_absolute)
0 ignored issues
show
This method seems to be duplicated in your project.

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.

Loading history...
30
	{
31
		list($category, $file_name) = explode('/', $string);
32
		list($photo, $extension) = explode('.', $file_name);
33
		
34
		$file_path = $this->get_file_path($category, $photo, $size, $extension);
35
		
36
		$file_size = getimagesize($file_path);
37
		
38
		Loader::load('collector', 'image/PhotoCollector');
39
		$photo_result = PhotoCollector::fetchRow($category, $photo);
40
		if($photo_result == false)
41
			return '';
42
		
43
		$height = $file_size[1];
44
		$width = $file_size[0];
45
		$description = $photo_result->description;
46
        
47
		$domain = '/';
48
		if($is_absolute)
49
			$domain = Loader::getRootUrl('blog');
50
		
51
		return sprintf(self::$PHOTO_CONTENT, $domain, $category, $photo, $size, $height, $width, $description, $description);
52
	}
53
54
}
55