Completed
Push — 1 ( 2c4d81...574f35 )
by Morven
01:27
created

GalleryPage_Controller::init()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
class GalleryPage_Controller extends Page_Controller
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
4
{
5
    public function init() {
6
        parent::init();
7
    }
8
9 View Code Duplication
    public function PaginatedImages()
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...
10
    {
11
        $list = $this->SortedImages();
12
        $limit = $this->ThumbnailsPerPage;
13
14
        $pages = PaginatedList::create($list, $this->getRequest());
15
        $pages->setpageLength($limit);
16
17
        return $pages;
18
    }
19
20
    /**
21
     * Generate an image based on the provided type
22
     * (either )
23
     *
24
     * @param Image $image
25
     * @param string $thumbnail generate a smaller image (based on thumbnail settings)
0 ignored issues
show
Documentation introduced by
Should the type for parameter $thumbnail not be false|string?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
26
     * @return void
0 ignored issues
show
Documentation introduced by
Should the return type not be Image|null|false?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
27
     */
28
    protected function ScaledImage(Image $image, $thumbnail = false)
29
    {
30
        $img = false;
31
        $background = $this->PaddedImageBackground;
32
        
33
        if ($thumbnail) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $thumbnail of type false|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
34
            $resize_type = $this->ThumbnailResizeType;
35
            $width = $this->ThumbnailWidth;
36
            $height = $this->ThumbnailHeight;
37
        } else {
38
            $resize_type = $this->ImageResizeType;
39
            $width = $this->ImageWidth;
40
            $height = $this->ImageHeight;
41
        }
42
43
        switch ($resize_type) {
44
            case 'crop':
45
                $img = $image->CroppedImage($width,$height);
0 ignored issues
show
Deprecated Code introduced by
The method Image::CroppedImage() has been deprecated with message: 4.0 Use Fill instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
46
                break;
47
            case 'pad':
48
                $img = $image->PaddedImage($width,$height,$background);
0 ignored issues
show
Deprecated Code introduced by
The method Image::PaddedImage() has been deprecated with message: 4.0 Use Pad instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
49
                break;
50
            case 'ratio':
51
                $img = $image->SetRatioSize($width,$height);
0 ignored issues
show
Deprecated Code introduced by
The method Image::SetRatioSize() has been deprecated with message: 4.0 Use Fit instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
52
                break;
53
            case 'width':
54
                $img = $image->ScaleWidth($width);
55
                break;
56
            case 'height':
57
                $img = $image->ScaleHeight($height);
58
                break;
59
        }
60
61
        $this->extend("augmentImageResize", $image, $thumbnail, $img);
62
63
        return $img;
64
    }
65
66
    protected function GalleryImage(Image $image)
67
    {
68
        return $this->ScaledImage($image);
69
    }
70
71
    protected function GalleryThumbnail(Image $image)
72
    {
73
        return $this->ScaledImage($image, true);
74
    }
75
76
    /**
77
     * Generate an image gallery from the Gallery template, if no images are
78
     * available, then return an empty string.
79
     *
80
     * @return string
81
     */
82
    public function Gallery()
83
    {
84
        if ($this->Images()->exists()) {
85
86
            // Create a list of images with generated gallery image and thumbnail
87
            $images = ArrayList::create();
88
            foreach ($this->PaginatedImages() as $image) {
89
                $image_data = $image->toMap();
90
                $image_data["GalleryImage"] = $this->GalleryImage($image);
91
                $image_data["GalleryThumbnail"] = $this->GalleryThumbnail($image);
92
                $images->add(ArrayData::create($image_data));
93
            }
94
            
95
            $vars = array(
96
                'Images' => $images,
97
                'Width' => $this->ImageWidth,
98
                'Height' => $this->ImageHeight
99
            );
100
101
            return $this->renderWith('Gallery',$vars);
102
        } else {
103
            return "";
104
        }
105
    }
106
}
107