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

LogController::get_log_introduction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 14

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 17
rs 9.4285
cc 1
eloc 14
nc 1
nop 1
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', array(
4
    'image/AlbumCollector',
5
    'waterfall/LogCollector'));
6
Loader::load('controller', 'waterfalls/DefaultPageController');
7
8
final class LogController extends DefaultPageController
9
{
10
11
    private static $AUTHOR = 'Jacob Emerick';
12
    private static $AUTHOR_URL = 'http://home.jacobemerick.com/';
13
    private static $JOURNAL_DIRECTORY = 'journal';
14
15
    private static $FULL_IMAGE_LINK = '/photo/%s/%s-size-standard.jpg';
16
    private static $THUMB_IMAGE_NODE = '<img src="/photo/%s/%s-size-thumb.jpg" alt="%s" height="%s" width="%s" />';
17
    private static $MEDIUM_IMAGE_NODE = '<img src="/photo/%s/%s-size-small.jpg" alt="%s" height="375" width="500" />';
18
19
    private $log;
20
21
    public function __construct()
22
    {
23
        parent::__construct();
24
        
25
        $log_path = URLDecode::getPiece(2);
26
        
27
        $this->log = LogCollector::getByAlias($log_path);
28
        if(!$this->log)
29
            $this->eject();
30
31
        $this->parent_navigation_item = 'journal';
0 ignored issues
show
Bug introduced by
The property parent_navigation_item does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
32
        
33
        $this->handle_comment_submit(
34
            self::$WATERFALL_SITE_ID,
35
            $this->log->alias,
36
            Loader::getRootUrl('waterfalls') . self::$JOURNAL_DIRECTORY . '/' . $this->log->alias . '/',
37
            $this->log->title);
38
        
39
        $this->add_waterfall_js();
0 ignored issues
show
Bug introduced by
The method add_waterfall_js() does not seem to exist on object<LogController>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
40
    }
41
42
    protected function set_head_data()
43
    {
44
        parent::set_head_data();
45
        
46
        $this->set_title("{$this->log->title} | " . self::$WEBSITE_TITLE);
47
        $this->set_description($this->log->introduction);
48
        
49
        $keyword_array = array();
50
        $tag_result = LogCollector::getTagListForLog($this->log->id);
51
        foreach($tag_result as $tag)
0 ignored issues
show
Bug introduced by
The expression $tag_result of type array|false is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
52
        {
53
            $keyword_array[] = $tag->name;
54
        }
55
        
56
        $this->set_keywords($keyword_array);
57
    }
58
59
    protected function set_body_data($page_type = 'normal')
60
    {
61
        parent::set_body_data($page_type);
0 ignored issues
show
Unused Code introduced by
The call to DefaultPageController::set_body_data() has too many arguments starting with $page_type.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
62
        
63
        $this->set_body('view', 'Log');
64
65
        $body_data = $this->get_formatted_log();
66
        $body_data['comment_array'] = $this->get_comment_array(self::$WATERFALL_SITE_ID, $this->log->alias);
67
        $body_data['sidebar'] = $this->get_sidebar();
68
        $body_data['series'] = $this->get_series();
69
        $body_data['album'] = $this->get_album();
70
        
71
        $this->set_body('data', $body_data);
72
    }
73
74
    private function get_formatted_log()
75
    {
76
        $formatted_log = array();
77
        
78
        $formatted_log['introduction'] = $this->get_log_introduction($this->log);
79
        $formatted_log['title'] = $this->log->title;
80
        $formatted_log['url'] = Loader::getRootURL('waterfalls') . self::$JOURNAL_DIRECTORY . '/' . $this->log->alias . '/';
81
82
        $formatted_log['body'] = $this->get_formatted_log_body();
83
        
84
        return $formatted_log;
85
    }
86
87
    private function get_log_introduction($log)
88
    {
89
        return array(
90
            'title' => $log->title,
91
            'url' => Loader::getRootURL('waterfalls') . self::$JOURNAL_DIRECTORY . '/' . $log->alias . '/',
92
            'date' => $this->get_parsed_date($log->date),
93
            'publish_date' => $this->get_parsed_date($log->publish_date),
94
            'introduction' => $log->introduction,
95
            'image' => sprintf(
96
                self::$MEDIUM_IMAGE_NODE,
97
                $log->image_category,
98
                $log->image_name,
99
                $log->image_description),
100
            'author' => self::$AUTHOR,
101
            'author_url' => self::$AUTHOR_URL,
102
        );
103
    }
104
105
    // @todo - link things up
106
    private function get_formatted_log_body()
107
    {
108
        $body = $this->log->body;
109
        
110
        $body = Content::instance('FixPhoto', $body)->activate(false, 'standard');
111
        $body = Content::instance('FixInternalLink', $body)->activate();
112
        
113
        return $body;
114
    }
115
116
    private function get_series()
117
    {
118
        $series = array(
119
            'previous' => array(),
120
            'next' => array());
121
        
122
        $previous_log = LogCollector::getPreviousLog($this->log->id);
123 View Code Duplication
        if($previous_log != false)
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...
124
        {
125
            $series['previous'] = (object) array(
126
                'path' => '/' . self::$JOURNAL_DIRECTORY . '/' . $previous_log->alias . '/',
127
                'title' => $previous_log->title,
128
                'date' => $previous_log->date);
129
        }
130
        
131
        $next_log = LogCollector::getNextLog($this->log->id);
132 View Code Duplication
        if($next_log != false)
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...
133
        {
134
            $series['next'] = (object) array(
135
                'path' => '/' . self::$JOURNAL_DIRECTORY . '/' . $next_log->alias . '/',
136
                'title' => $next_log->title,
137
                'date' => $next_log->date);
138
        }
139
        
140
        return $series;
141
    }
142
143
    private function get_sidebar()
144
    {
145
        $sidebar = array();
146
        
147
        $sidebar['companion_list'] = array();
148
        $companions = LogCollector::getCompanionListForLog($this->log->id);
149
        foreach($companions as $companion)
0 ignored issues
show
Bug introduced by
The expression $companions of type array|false is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
150
        {
151
            $sidebar['companion_list'][] = (object) array(
152
                'title' => $companion->name,
153
                'path' => "/companion/{$companion->alias}/");
154
        }
155
        
156
        $sidebar['tag_list'] = array();
157
        $tags = LogCollector::getTagListForLog($this->log->id);
158
        foreach($tags as $tag)
0 ignored issues
show
Bug introduced by
The expression $tags of type array|false is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
159
        {
160
            $sidebar['tag_list'][] = (object) array(
161
                'title' => $tag->name,
162
                'path' => "/journal/tag/{$tag->alias}/");
163
        }
164
        
165
        $sidebar['waterfall_list'] = array();
166
        $waterfalls = LogCollector::getWaterfallListForLog($this->log->id);
167
        foreach($waterfalls as $waterfall)
0 ignored issues
show
Bug introduced by
The expression $waterfalls of type array|false is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
168
        {
169
            $sidebar['waterfall_list'][] = (object) array(
170
                'title' => $waterfall->name,
171
                'path' => "/{$waterfall->watercourse_alias}/{$waterfall->alias}/");
172
        }
173
        
174
        return $sidebar;
175
    }
176
177 View Code Duplication
    private function get_album()
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...
178
    {
179
        $album = array();
180
        
181
        if($this->log->album == 0)
182
            return $album;
183
        
184
        $photo_list = AlbumCollector::getPhotoListForAlbum($this->log->album);
185
        foreach($photo_list as $photo)
0 ignored issues
show
Bug introduced by
The expression $photo_list of type array|false is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
186
        {
187
            $photo_array = array();
188
            $photo_array['full_link'] = sprintf(self::$FULL_IMAGE_LINK, $photo->category, $photo->name);
189
            $photo_array['description'] = $photo->description;
190
            
191
            if($photo->height < $photo->width)
192
                list($height, $width) = array(75, 100);
193
            else
194
                list($height, $width) = array(100, 75);
195
            
196
            $photo_array['image_node'] = sprintf(
197
                self::$THUMB_IMAGE_NODE,
198
                $photo->category,
199
                $photo->name,
200
                $photo->description,
201
                $height,
202
                $width);
203
            
204
            $album[] = (object) $photo_array;
205
        }
206
        
207
        return $album;
208
    }
209
210
}
211