Completed
Push — master ( 70260c...fc1b09 )
by Jacob
03:23
created

PostController::get_post_description()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 8
rs 9.4285
cc 1
eloc 5
nc 1
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('collector', 'waterfall/LogCollector');
4
Loader::load('controller', 'blog/DefaultPageController');
5
6
final class PostController extends DefaultPageController
7
{
8
9
	private static $PAGE_DESCRIPTION_LIMIT = 250;
10
11
	private static $TITLE = "%s | Jacob Emerick's Blog";
12
	private static $AUTHOR = 'Jacob Emerick';
13
	private static $AUTHOR_URL = 'https://home.jacobemerick.com/';
14
15
	private static $POST_LENGTH_SHORT = 100;
16
	private static $POST_LENGTH_LONG = 140;
17
18
	private $post;
19
	private $tags;
20
	private $comment_errors = array();
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
Unused Code introduced by
The property $comment_errors is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
21
22
	public function __construct()
23
	{
24
		parent::__construct();
25
		
26
        global $container;
27
        $repository = new Jacobemerick\Web\Domain\Blog\Post\MysqlPostRepository($container['db_connection_locator']);
28
        $this->post = $repository->findPostByPath(URLDecode::getPiece(2));
29
30
		if($this->post == null)
31
			$this->eject();
32
		
33
		$this->handle_comment_submit(
34
			self::$BLOG_SITE_ID,
35
			$this->post['path'],
36
			Loader::getRootUrl('blog') . $this->post['category'] . '/' . $this->post['path'] . '/',
37
			$this->post['title']);
38
39
        global $container;
40
        $repository = new Jacobemerick\Web\Domain\Blog\Tag\MysqlTagRepository($container['db_connection_locator']);
41
        $this->tags = $repository->getTagsForPost($this->post['id']);
42
	}
43
44
	protected function set_head_data()
45
	{
46
		parent::set_head_data();
47
		
48
		$this->set_title(sprintf(self::$TITLE, $this->post['title']));
49
		$this->set_description($this->get_post_description());
50
		$this->set_keywords($this->get_post_keywords());
51
		$this->set_author(self::$AUTHOR);
52
53
    $photo = Content::instance('FetchFirstPhoto', $this->post['body'])->activate(true);
54
    $photo = preg_match('/^<img src="([a-z-:\.\/]+)" [^>]+>$/', $photo, $matches);
0 ignored issues
show
Unused Code introduced by
$photo is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
55
    $this->set_head('thumbnail', $matches[1]);
56
57
		if (array_key_exists($this->post['id'], self::$DEPRECATED_BLOGS)) {
58
			$log_id = self::$DEPRECATED_BLOGS[$this->post['id']];
59
			$log = LogCollector::getById($log_id);
60
			if (!empty($log)) {
61
				$log_url = Loader::getRootUrl('waterfalls') . "journal/{$log->alias}/";
62
				$this->set_canonical($log_url);
63
			}
64
		}
65
	}
66
67
	protected function get_introduction() {}
68
69
	protected function set_body_data()
70
	{
71
		parent::set_body_data();
72
		
73
		$this->set_body('title', $this->post['title']);
74
		$this->set_body('view', 'Post');
75
		$this->set_body('data', array(
76
			'post' => $this->format_post($this->post, false),
77
			'series_posts' => $this->get_series_posts(),
78
			'related_posts' => $this->get_related_posts(),
79
			'author' => self::$AUTHOR,
80
			'author_url' => self::$AUTHOR_URL,
81
			'comment_array' => $this->get_comment_array(self::$BLOG_SITE_ID, $this->post['path'])));
82
	}
83
84
	protected function get_post_description()
85
	{
86
		$description = $this->post['body'];
87
		$description = strip_tags($description);
88
		$description = Content::instance('SmartTrim', $description)->activate(self::$PAGE_DESCRIPTION_LIMIT);
89
		
90
		return $description;
91
	}
92
93
	protected function get_post_keywords()
94
	{
95
		$keyword_array = array();
96
		$keywords = $this->tags;
97
		
98
		foreach($keywords as $keyword)
99
		{
100
			$keyword_array[] = $keyword['tag'];
101
		}
102
		
103
		$keyword_array[] = 'blog';
104
		$keyword_array[] = 'Jacob Emerick';
105
		
106
		return $keyword_array;
107
	}
108
109
	private function get_series_posts()
110
	{
111
		$series_posts = $this->fetch_series_posts();
112
		if(count($series_posts) < 1)
113
			return array();
114
		
115
		$previous_post = new stdclass();
116
		$next_post = new stdclass();
117
		
118
		$found_current_post = false;
119
		foreach($series_posts as $post_row)
120
		{
121
			if($post_row['post'] == $this->post['id'])
122
			{
123
				$found_current_post = true;
124
				continue;
125
			}
126
			
127
			$post = new stdclass();
128
129
      if (
130
        strpos($post_row['title'], 'Rainy Supe Loop') === 0 ||
131
        strpos($post_row['title'], 'Malapais Loop') === 0 ||
132
        strpos($post_row['title'], 'Mazatzal Peak Loop') === 0 ||
133
        strpos($post_row['title'], 'Dripping Springs Loop') === 0
134
      ) {
135
        $title = $post_row['title'];
136
        $title = explode(':', $title);
137
        $title = array_pop($title);
138
        $title = trim($title);
139
        $post->title = $title;
140
      } else if (strpos($post_row['title'], 'Isle Royale') === 0) {
141
				$title = $post_row['title'];
142
				$title = explode(',', $title);
143
				$title = array_pop($title);
144
				$title = trim($title);
145
				$post->title = $title;
146
			} else {
147
				$post->title = $post_row['title'];
148
			}
149
150
			$post->url = Loader::getRootUrl('blog') . "{$post_row['category']}/{$post_row['path']}/";
151
			
152
			if(!$found_current_post)
153
				$previous_post = $post;
154
			else
155
			{
156
				$next_post = $post;
157
				break;
158
			}
159
		}
160
		
161
		return array(
162
			'title' => $post_row['series_title'],
0 ignored issues
show
Bug introduced by
The variable $post_row seems to be defined by a foreach iteration on line 119. Are you sure the iterator is never empty, otherwise this variable is not defined?

It seems like you are relying on a variable being defined by an iteration:

foreach ($a as $b) {
}

// $b is defined here only if $a has elements, for example if $a is array()
// then $b would not be defined here. To avoid that, we recommend to set a
// default value for $b.


// Better
$b = 0; // or whatever default makes sense in your context
foreach ($a as $b) {
}

// $b is now guaranteed to be defined here.
Loading history...
163
			'description' => Content::instance('FixInternalLink', $post_row['series_description'])->activate(),
164
			'previous' => $previous_post,
165
			'next' => $next_post);
166
	}
167
168
	private $series_posts;
169
	private function fetch_series_posts()
170
	{
171
      if(!isset($this->series_posts)) {
172
          global $container;
173
          $repository = new Jacobemerick\Web\Domain\Blog\Series\MysqlSeriesRepository($container['db_connection_locator']);
174
          $this->series_posts = $repository->getSeriesForPost($this->post['id']);
175
      }
176
      return $this->series_posts;
177
	}
178
179
	private function get_related_posts()
180
	{
181
		$tag_array = array();
182
		foreach($this->tags as $tag)
183
		{
184
			$tag_array[] = $tag['id'];
185
		}
186
		
187
		$series_posts = $this->fetch_series_posts();
188
		$exclude_post_array = array();
189
		foreach($series_posts as $series_post)
190
		{
191
			$exclude_post_array[] = $series_post['post'];
192
		}
193
194
        global $container;
195
        $repository = new Jacobemerick\Web\Domain\Blog\Post\MysqlPostRepository($container['db_connection_locator']);
196
        $post_result = $repository->getActivePostsByRelatedTags($this->post['id']);
197
198
        $post_array = array();
199
		
200 View Code Duplication
		foreach($post_result as $post_row)
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
201
		{
202
			$post = new stdclass();
203
			$post->title = $post_row['title'];
204
			$post->url = Loader::getRootUrl('blog') . "{$post_row['category']}/{$post_row['path']}/";
205
			$post->category = ucwords(str_replace('-', ' ', $post_row['category']));
206
			$post->thumb = Content::instance('FetchFirstPhoto', $post_row['body'])->activate();
207
			$post->body = Content::instance('SmartTrim', $post_row['body'])->activate(($post->thumb !== '') ? self::$POST_LENGTH_SHORT : self::$POST_LENGTH_LONG);
208
			
209
			$post_array[] = $post;
210
		}
211
		
212
		return $post_array;
213
	}
214
215
}
216