Completed
Push — master ( acb423...2a302c )
by Jacob
03:45
created

DefaultPageController::get_comments_from_service()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 36
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 36
rs 8.8571
cc 3
eloc 27
nc 3
nop 0
1
<?
0 ignored issues
show
Security Best Practice introduced by
It is not recommend to use PHP's short opening tag <?, better use <?php, or <?= in case of outputting.

Short opening tags are disabled in PHP’s default configuration. In such a case, all content of this file is output verbatim to the browser without being parsed, or executed.

As a precaution to avoid these problems better use the long opening tag <?php.

Loading history...
2
3
Loader::load('controller', '/PageController');
4
Loader::load('utility', 'Content');
5
6
abstract class DefaultPageController extends PageController
7
{
8
9
	private static $RECENT_COMMENT_COUNT = 10;
10
	private static $MINIMUM_TAG_COUNT = 10;
11
	protected static $LENGTH_OF_TRIMMED_POST = 300;
12
13
	protected static $BLOG_SITE_ID = 2;
14
15
	private static $INTRODUCTION_IMAGE_PATTERN = '<img src="/photo/%s/%s-size-%s.jpg" height="%d" width="%d" alt="%s" />';
16
17
	protected function set_head_data()
18
	{
19
    $this->set_head('rss_link', [
20
      'title' => 'Jacob Emerick Blog Feed',
21
      'url' => '/rss.xml'
22
    ]);
23
    $this->set_head('rss_comment_link', [
24
      'title' => 'Jacob Emerick Blog Comment Feed',
25
      'url' => '/rss-comments.xml'
26
    ]);
27
		
28
		$this->add_css('normalize');
29
		$this->add_css('blog');
30
	}
31
32
	protected function get_introduction()
33
	{
34
		return;
35
	}
36
37
	protected function get_introduction_image($id)
38
	{
39
		Loader::load('collector', 'image/PhotoCollector');
40
		$photo_result = PhotoCollector::getRow($id);
41
		
42
		if($photo_result == null)
43
			return;
44
		
45
		$name = $photo_result->name;
46
		$category = $photo_result->category;
47
		$size = 'medium';
48
		$height = 375;
49
		$width = 500;
50
		$description = $photo_result->description;
51
		
52
		return sprintf(self::$INTRODUCTION_IMAGE_PATTERN, $category, $name, $size, $height, $width, $description);
53
	}
54
55
	protected function set_body_data()
56
	{
57
		$this->set_body('introduction', $this->get_introduction());
58
		$this->set_body('right_side', $this->get_right_side());
59
		$this->set_body('activity_array', $this->get_recent_activity());
60
		
61
		$this->set_body_view('Page');
62
	}
63
64
	final protected function format_post($post, $trim = false)
65
	{
66
		$post_object = new stdclass();
67
		
68
		$post_object->title = $post['title'];
69
		$post_object->path = "/{$post['category']}/{$post['path']}/";
70
		$post_object->category = ucwords(str_replace('-', ' ', $post['category']));
71
		$post_object->category_link = "/{$post['category']}/";
72
		$post_object->tags = $this->get_tags_for_post($post);
73
		$post_object->image = Content::instance('FetchFirstPhoto', $post['body'])->activate(false, 'small');
74
		$post_object->body = $this->get_body_for_post($post, $trim);
75
		$post_object->date = $this->get_parsed_date($post['date']);
76
77
		return $post_object;
78
	}
79
80
	final private function get_tags_for_post($post)
81
	{
82
        global $container;
83
        $repository = new Jacobemerick\Web\Domain\Blog\Tag\MysqlTagRepository($container['db_connection_locator']);
84
        $tag_result = $repository->getTagsForPost($post['id']);
85
86
        $tag_array = array();
87
		foreach($tag_result as $tag)
88
		{
89
			$tag_object = new stdclass();
90
			$tag_object->name = $tag['tag'];
91
			$tag_object->link = Content::instance('URLSafe', "/tag/{$tag['tag']}/")->activate();
92
			$tag_array[] = $tag_object;
93
		}
94
		return $tag_array;
95
	}
96
97
	final private function get_body_for_post($post, $trim)
98
	{
99
		$body = $post['body'];
100
		
101
		if($trim)
102
			$body = Content::instance('SmartTrim', $body)->activate(self::$LENGTH_OF_TRIMMED_POST);
103
		
104
		$body = Content::instance('FixPhoto', $body)->activate(false, 'standard');
105
		$body = Content::instance('MarkupCode', $body)->activate();
106
		
107
		return $body;
108
	}
109
110
	final protected function get_right_side()
111
	{
112
		$side_array = array();
113
		$side_array['tags'] = $this->get_tag_cloud();
114
		$side_array['comments'] = $this->get_comments();
115
		return $side_array;
116
	}
117
118
	final private function get_tag_cloud()
119
	{
120
        global $container;
121
        $repository = new Jacobemerick\Web\Domain\Blog\Tag\MysqlTagRepository($container['db_connection_locator']);
122
        $tag_result = $repository->getTagCloud();
123
		
124
		$maximum_tag_count = $this->get_maximum_tag_count($tag_result);
125
		
126
		$cloud_array = array();
127
		foreach($tag_result as $tag)
128
		{
129
			if($tag['count'] < self::$MINIMUM_TAG_COUNT)
130
				continue;
131
			
132
			$tag_object = new stdclass();
133
			$tag_object->name = $tag['tag'];
134
			$tag_object->link = Content::instance('URLSafe', "/tag/{$tag['tag']}/")->activate();
135
			$tag_object->scalar = floor(($tag['count'] - 1) * (9 / ($maximum_tag_count - self::$MINIMUM_TAG_COUNT)));
136
			$cloud_array[] = $tag_object;
137
		}
138
		
139
		return $cloud_array;
140
	}
141
142
	final private function get_maximum_tag_count($tag_result)
143
	{
144
		$maximum = 1;
145
		
146
		foreach($tag_result as $tag)
147
		{
148
			if($tag['count'] > $maximum)
149
				$maximum = $tag['count'];
150
		}
151
		return $maximum;
152
	}
153
154 View Code Duplication
    final private function get_comments()
0 ignored issues
show
Duplication introduced by
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...
155
    {
156
        global $container;
157
        $repository = new Jacobemerick\Web\Domain\Comment\Comment\ServiceCommentRepository($container['comment_service_api']);
158
        $start = microtime(true);
159
        try {
160
            $comment_response = $repository->getComments(
161
                'blog.jacobemerick.com',
162
                null,
163
                1,
164
                self::$RECENT_COMMENT_COUNT,
165
                '-date'
166
            );
167
        } catch (Exception $e) {
168
            $container['logger']->warning("CommentService | Sidebar | {$e->getMessage()}");
169
            return;
170
        }
171
 
172
        $elapsed = microtime(true) - $start;
173
        global $container;
174
        $container['logger']->info("CommentService | Sidebar | {$elapsed}");
175
176
        $array = array();
177
        foreach($comment_response as $comment)
178
        {
179
            $body = Content::instance('CleanComment', $comment['body'])->activate();
180
            $body = strip_tags($body);
181
182
            $comment_obj = new stdclass();
183
            $comment_obj->description = Content::instance('SmartTrim', $body)->activate(30);
184
            $comment_obj->commenter = $comment['commenter']['name'];
185
            $comment_obj->link = $comment['url'];
186
            $array[] = $comment_obj;
187
        }
188
        return $array;
189
    }
190
}
191