GalleryHub_Controller::ScaledImage()   C
last analyzed

Complexity

Conditions 7
Paths 12

Size

Total Lines 37
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 37
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 29
nc 12
nop 2
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 null|Image|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 == true) {
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->Fill($width,$height);
61
                break;
62
            case 'pad':
63
                $img = $image->Pad($width,$height,$background);
64
                break;
65
            case 'ratio':
66
                $img = $image->Fit($width,$height);
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