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

WaterfallController::get_map_piece()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 15

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 21
rs 9.3142
cc 1
eloc 15
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/WatercourseCollector',
6
    'waterfall/WaterfallCollector',
7
    'waterfall/LogCollector'));
8
Loader::load('controller', 'waterfalls/DefaultPageController');
9
10
final class WaterfallController extends DefaultPageController
11
{
12
13
    private static $STANDARD_IMAGE_NODE = '<img src="/photo/%s/%s-size-standard.jpg" alt="%s" height="600" width="800" />';
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-medium.jpg" alt="%s" height="375" width="500" />';
0 ignored issues
show
Unused Code introduced by
The property $MEDIUM_IMAGE_NODE 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...
18
19
    private $waterfall;
20
21
    public function __construct()
22
    {
23
        parent::__construct();
24
25
        $path_watercourse = URLDecode::getPiece(1);
26
        $path_fall = URLDecode::getPiece(2);
27
28
        $this->waterfall = WaterfallCollector::getByAlias($path_watercourse, $path_fall);
29
        if (!$this->waterfall) {
30
              $this->eject();
31
        }
32
        
33
        $this->handle_comment_submit(
34
            self::$WATERFALL_SITE_ID,
35
            "{$this->waterfall->watercourse_alias}/{$this->waterfall->alias}",
36
            Loader::getRootUrl('waterfalls') . "{$this->waterfall->watercourse_alias}/{$this->waterfall->alias}/",
37
            $this->waterfall->name);
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<WaterfallController>.

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->waterfall->name} on {$this->waterfall->watercourse} | " . self::$WEBSITE_TITLE);
47
        $this->set_description($this->waterfall->description);
48
        $this->set_keywords((array) $this->waterfall->name);
49
    }
50
51
    protected function set_body_data($page_type = 'normal')
52
    {
53
        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...
54
55
        $body_data = $this->format_waterfall_data($this->waterfall);
56
        $body_data['comment_array'] = $this->get_comment_array(self::$WATERFALL_SITE_ID, "{$this->waterfall->watercourse_alias}/{$this->waterfall->alias}");
57
58
        $this->set_body('data', $body_data);
59
60
        $this->set_body('view', 'Fall');
61
    }
62
63
    private function format_waterfall_data($waterfall)
64
    {
65
        $waterfall_data = array();
66
        $waterfall_data['introduction'] = $this->get_waterfall_introduction($waterfall);
67
        $waterfall_data['name'] = $waterfall->name;
68
        $waterfall_data['watercourse'] = $waterfall->watercourse;
69
        $waterfall_data['path'] = "/{$waterfall->watercourse_alias}/{$waterfall->alias}/";
70
        $waterfall_data['tagline'] = $waterfall->introduction;
71
        $waterfall_data['main_photo'] = sprintf(self::$STANDARD_IMAGE_NODE, $waterfall->photo_category, $waterfall->photo, $waterfall->photo_description);
72
        $waterfall_data['album'] = $this->get_album();
73
        $waterfall_data['body'] = $this->format_waterfall_content($waterfall->body);
74
        $waterfall_data['directions'] = $this->format_waterfall_content($waterfall->directions);
75
        $waterfall_data['sidebar'] = $this->get_sidebar($waterfall);
76
77
        return $waterfall_data;
78
    }
79
80
    private function get_waterfall_introduction($waterfall)
81
    {
82
        return array(
83
            'path' => "/{$waterfall->watercourse_alias}/{$waterfall->alias}/",
84
            'name' => $waterfall->name,
85
            'watercourse' => $waterfall->watercourse,
86
            'tagline' => $waterfall->introduction,
87
        );
88
    }
89
90
    private function get_main_watercourse($alias)
91
    {
92
        $watercourse = WatercourseCollector::getByAlias($alias);
93
        if ($watercourse->parent != 0) {
94
            $watercourse = WatercourseCollector::getById($watercourse->parent);
95
        }
96
        
97
        return (object) array(
98
            'name' => $watercourse->name,
99
            'title' => $watercourse->name,
100
            'alias' => $watercourse->alias,
101
        );
102
    }
103
104
    private function format_waterfall_content($content)
105
    {
106
        $content = Content::instance('FixInternalLink', $content)->activate();
107
        
108
        return $content;
109
    }
110
111 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...
112
    {
113
        $album = array();
114
        
115
        if($this->waterfall->album == 0)
116
            return $album;
117
        
118
        $photo_list = AlbumCollector::getPhotoListForAlbum($this->waterfall->album);
119
        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...
120
        {
121
            $photo_array = array();
122
            $photo_array['full_link'] = sprintf(self::$FULL_IMAGE_LINK, $photo->category, $photo->name);
123
            $photo_array['description'] = $photo->description;
124
            
125
            if($photo->height < $photo->width)
126
                list($height, $width) = array(75, 100);
127
            else
128
                list($height, $width) = array(100, 75);
129
            
130
            $photo_array['image_node'] = sprintf(
131
                self::$THUMB_IMAGE_NODE,
132
                $photo->category,
133
                $photo->name,
134
                $photo->description,
135
                $height,
136
                $width);
137
            
138
            $album[] = (object) $photo_array;
139
        }
140
        
141
        return $album;
142
    }
143
144
	private function get_sidebar($waterfall)
145
	{
146
		$sidebar_data = array();
147
		$sidebar_data['name'] = $waterfall->name;
148
		$sidebar_data['watercourse'] = $waterfall->watercourse;
149
        $sidebar_data['main_watercourse'] = $this->get_main_watercourse($waterfall->watercourse_alias);
150
		$sidebar_data['rating_display'] = ($waterfall->rating / 2) . '/5';
151
        $sidebar_data['rating'] = $waterfall->rating;
152
		$sidebar_data['height'] = Content::instance('ImperialUnit', $waterfall->height)->activate('inches');
153
		$sidebar_data['width'] = Content::instance('ImperialUnit', $waterfall->width)->activate('inches');
154
		$sidebar_data['drop_height'] = Content::instance('ImperialUnit', $waterfall->drop_height)->activate('inches');
155
		$sidebar_data['drop_count'] = $waterfall->drop_count;
156
		
157
        $sidebar_data['county'] = (object) array (
158
            'name' => $waterfall->county,
159
            'alias' => $waterfall->county_alias,
160
            'title' => $waterfall->county_title,
161
        );
162
        
163
		$sidebar_data['nearest_town'] = $waterfall->nearest_town;
164
		$sidebar_data['latitude'] = round($waterfall->latitude, 5);
165
		$sidebar_data['longitude'] = round($waterfall->longitude, 5);
166
		$sidebar_data['elevation'] = Content::instance('ImperialUnit', $waterfall->elevation)->activate('feet');
167
        
168
        $sidebar_data['map'] = $this->get_map_piece($waterfall);
169
        
170
        $sidebar_data['journal_list'] = $this->get_journal_list($waterfall->id);
171
        
172
        $sidebar_data['nearby_list'] = $this->get_nearby_list($waterfall->id);
173
		
174
		return $sidebar_data;
175
	}
176
177
    private function get_journal_list($waterfall)
178
    {
179
        $list = array();
180
        $log_result = LogCollector::getLogListForWaterfall($waterfall);
181
        foreach ($log_result as $log_row) {
0 ignored issues
show
Bug introduced by
The expression $log_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...
182
            $list[] = (object) array(
183
                'date' => date('F j, Y', strtotime($log_row->date)),
184
                'title' => $log_row->title,
185
                'url' => "/journal/{$log_row->alias}/",
186
            );
187
        }
188
        
189
        return $list;
190
    }
191
192
    private function get_map_piece($waterfall)
193
    {
194
        $map_holder = array();
195
        
196
        $url = 'http://maps.googleapis.com/maps/api/staticmap';
197
        $url .= "?center={$waterfall->latitude},{$waterfall->longitude}";
198
        $url .= "&zoom=8";
199
        $url .= "&maptype=terrain";
200
        $url .= "&size=230x200";
201
        $url .= "&markers=color:red|size:small|{$waterfall->latitude},{$waterfall->longitude}";
202
        $url .= "&sensor=false";
203
        $url .= "&key=AIzaSyA3eWALjUTSIa44KsbOUYRpG0oMd3aNo00";
204
        
205
        $image = "<img src=\"{$url}\" height=\"200\" width=\"230\" alt=\"{$waterfall->name} Location\" />";
206
        $map_holder['image'] = $image;
207
        
208
        $map_holder['uri'] = '/map/';
209
        $map_holder['title'] = "View {$waterfall->name} on a larger map";
210
        
211
        return (object) $map_holder;
212
    }
213
214
    private function get_nearby_list($waterfall)
215
    {
216
        $nearby_list = array();
217
        
218
        $result = WaterfallCollector::getNearbyList($waterfall);
219
        foreach ($result as $row) {
0 ignored issues
show
Bug introduced by
The expression $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...
220
            $nearby_list[] = (object) array(
221
                'url' => "/{$row->watercourse_alias}/{$row->alias}/",
222
                'anchor' => $row->name,
223
                'title' => "{$row->name} of {$row->watercourse}",
224
                'distance' => Content::instance('ImperialUnit', $row->distance)->activate(false),
225
            );
226
        }
227
        
228
        return $nearby_list;
229
    }
230
231
}