Completed
Push — 1 ( 574f35...b1e328 )
by Morven
01:29
created

GalleryHub_Controller::PaginatedGalleries()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 23
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 23
rs 9.0856
c 0
b 0
f 0
cc 3
eloc 16
nc 3
nop 0
1
<?php
2
3
class GalleryHub_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
6
    /**
7
     * Get a custom list of children with resized gallery images
8
     * 
9
     * @return PaginatedList
10
     */
11
    public function PaginatedGalleries()
12
    {
13
        $children = $this->AllChildren();
14
        $limit = $this->ThumbnailsPerPage;
15
        $list = ArrayList::create();
16
17
        foreach ($children as $child) {
18
            $image = $child->SortedImages()->first();
19
            $child_data = $child->toMap();
20
            $child_data["Link"] = $child->Link();
21
            if ($image) {
22
                $child_data["GalleryThumbnail"] = $this->ScaledImage($image, true);
23
            } else {
24
                $child_data["GalleryThumbnail"] = null;
25
            }
26
            $list->add(ArrayData::create($child_data));
27
        }
28
29
        $pages = PaginatedList::create($list, $this->getRequest());
30
        $pages->setPageLength($limit);
31
32
        return $pages;
33
    }
34
35
    /**
36
     * Generate an image based on the provided type
37
     * (either )
38
     *
39
     * @param Image $image
40
     * @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...
41
     * @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...
42
     */
43
    protected function ScaledImage(Image $image, $thumbnail = false)
44
    {
45
        $img = false;
46
        $background = $this->PaddedImageBackground;
47
        
48
        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...
49
            $resize_type = $this->ThumbnailResizeType;
50
            $width = $this->ThumbnailWidth;
51
            $height = $this->ThumbnailHeight;
52
        } else {
53
            $resize_type = $this->ImageResizeType;
54
            $width = $this->ImageWidth;
55
            $height = $this->ImageHeight;
56
        }
57
58
        switch ($resize_type) {
59
            case 'crop':
60
                $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...
61
                break;
62
            case 'pad':
63
                $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...
64
                break;
65
            case 'ratio':
66
                $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...
67
                break;
68
            case 'width':
69
                $img = $image->ScaleWidth($width);
70
                break;
71
            case 'height':
72
                $img = $image->ScaleHeight($height);
73
                break;
74
        }
75
76
        $this->extend("augmentImageResize", $image, $thumbnail, $img);
77
78
        return $img;
79
    }
80
}
81