Completed
Push — master ( 6336a8...2eca18 )
by Jacob
07:36
created

LogController::get_formatted_log()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.8666
c 0
b 0
f 0
cc 1
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', 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 = 'https://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 = array_merge(
66
            $this->get_formatted_log(),
67
            [
68
                'comment_array' => $this->get_comment_array(
69
                    'waterfallsofthekeweenaw.com',
70
                    "journal/{$this->log->alias}"
71
                ),
72
                'sidebar' => $this->get_sidebar(),
73
                'series' => $this->get_series(),
74
                'album' => $this->get_album(),
75
            ]
76
        );
77
78
        $this->set_body('data', $body_data);
79
    }
80
81
    private function get_formatted_log()
82
    {
83
        $formatted_log = array();
84
        
85
        $formatted_log['introduction'] = $this->get_log_introduction($this->log);
86
        $formatted_log['title'] = $this->log->title;
87
        $formatted_log['url'] = Loader::getRootURL('waterfalls') . self::$JOURNAL_DIRECTORY . '/' . $this->log->alias . '/';
88
89
        $formatted_log['body'] = $this->get_formatted_log_body();
90
        
91
        return $formatted_log;
92
    }
93
94
    private function get_log_introduction($log)
95
    {
96
        return array(
97
            'title' => $log->title,
98
            'url' => Loader::getRootURL('waterfalls') . self::$JOURNAL_DIRECTORY . '/' . $log->alias . '/',
99
            'date' => $this->get_parsed_date($log->date),
100
            'publish_date' => $this->get_parsed_date($log->publish_date),
101
            'introduction' => $log->introduction,
102
            'image' => sprintf(
103
                self::$MEDIUM_IMAGE_NODE,
104
                $log->image_category,
105
                $log->image_name,
106
                $log->image_description),
107
            'author' => self::$AUTHOR,
108
            'author_url' => self::$AUTHOR_URL,
109
        );
110
    }
111
112
    // @todo - link things up
113
    private function get_formatted_log_body()
114
    {
115
        $body = $this->log->body;
116
        
117
        $body = Content::instance('FixPhoto', $body)->activate(false, 'standard');
118
        $body = Content::instance('FixInternalLink', $body)->activate();
119
        
120
        return $body;
121
    }
122
123
    private function get_series()
124
    {
125
        $series = array(
126
            'previous' => array(),
127
            'next' => array());
128
        
129
        $previous_log = LogCollector::getPreviousLog($this->log->id);
130 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...
131
        {
132
            $series['previous'] = (object) array(
133
                'path' => '/' . self::$JOURNAL_DIRECTORY . '/' . $previous_log->alias . '/',
134
                'title' => $previous_log->title,
135
                'date' => $previous_log->date);
136
        }
137
        
138
        $next_log = LogCollector::getNextLog($this->log->id);
139 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...
140
        {
141
            $series['next'] = (object) array(
142
                'path' => '/' . self::$JOURNAL_DIRECTORY . '/' . $next_log->alias . '/',
143
                'title' => $next_log->title,
144
                'date' => $next_log->date);
145
        }
146
        
147
        return $series;
148
    }
149
150
    private function get_sidebar()
151
    {
152
        $sidebar = array();
153
        
154
        $sidebar['companion_list'] = array();
155
        $companions = LogCollector::getCompanionListForLog($this->log->id);
156
        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...
157
        {
158
            $sidebar['companion_list'][] = (object) array(
159
                'title' => $companion->name,
160
                'path' => "/companion/{$companion->alias}/");
161
        }
162
        
163
        $sidebar['tag_list'] = array();
164
        $tags = LogCollector::getTagListForLog($this->log->id);
165
        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...
166
        {
167
            $sidebar['tag_list'][] = (object) array(
168
                'title' => $tag->name,
169
                'path' => "/journal/tag/{$tag->alias}/");
170
        }
171
        
172
        $sidebar['waterfall_list'] = array();
173
        $waterfalls = LogCollector::getWaterfallListForLog($this->log->id);
174
        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...
175
        {
176
            $sidebar['waterfall_list'][] = (object) array(
177
                'title' => $waterfall->name,
178
                'path' => "/{$waterfall->watercourse_alias}/{$waterfall->alias}/");
179
        }
180
        
181
        return $sidebar;
182
    }
183
184 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...
185
    {
186
        $album = array();
187
        
188
        if($this->log->album == 0)
189
            return $album;
190
        
191
        $photo_list = AlbumCollector::getPhotoListForAlbum($this->log->album);
192
        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...
193
        {
194
            $photo_array = array();
195
            $photo_array['full_link'] = sprintf(self::$FULL_IMAGE_LINK, $photo->category, $photo->name);
196
            $photo_array['description'] = $photo->description;
197
            
198
            if($photo->height < $photo->width)
199
                list($height, $width) = array(75, 100);
200
            else
201
                list($height, $width) = array(100, 75);
202
            
203
            $photo_array['image_node'] = sprintf(
204
                self::$THUMB_IMAGE_NODE,
205
                $photo->category,
206
                $photo->name,
207
                $photo->description,
208
                $height,
209
                $width);
210
            
211
            $album[] = (object) $photo_array;
212
        }
213
        
214
        return $album;
215
    }
216
217
}
218