Completed
Push — dev-master ( 29e920...408a96 )
by Vijay
03:32
created

Users   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 70
rs 10
wmc 13
lcom 0
cbo 4

1 Method

Rating   Name   Duplication   Size   Complexity  
C profile() 0 60 13
1
<?php
2
3
namespace FFCMS\Controllers\Media\Images;
4
5
use FFCMS\Mappers;
6
7
/**
8
 * Index Controller Class.
9
 *
10
 * @author Vijay Mahrra <[email protected]>
11
 * @copyright (c) Copyright 2016 Vijay Mahrra
12
 * @license GPLv3 (http://www.gnu.org/licenses/gpl-3.0.html)
13
 */
14
class Users
15
{
16
    /**
17
     *
18
     *
19
     * @param \Base $f3
20
     * @param array $params
21
     * @return void
0 ignored issues
show
Documentation introduced by
Should the return type not be string|null?

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...
22
     */
23
    public function profile(\Base $f3, array $params = [])
24
    {
25
        // get asset table entry
26
        $asset = new Mappers\Assets;
27
        $asset->load(['users_uuid = ? AND ' . $asset->quotekey('key') . ' = ?', $params['uuid'], $params['key']]);
28
        if (null === $asset->uuid) {
29
            return $f3->status(404);
30
        }
31
32
        // get image dimensions
33
        $max    = $f3->get('assets.image.max');
34
        $height = abs((int) $f3->get('REQUEST.height'));
35
        $width  = abs((int) $f3->get('REQUEST.width'));
36
37
        if (empty($height) || empty($width)) {
38
            // resize on longest side
39
            if ($height > $width) {
40
                $width = $height;
41
            } elseif ($width > $height) {
42
                $height = $width;
43
            } else {
44
                // use default height/width if missing
45
                $height = $f3->get('assets.image.default.height');
46
                $width  = $f3->get('assets.image.default.width');
47
            }
48
        } elseif ($width > $max['width'] || $height > $max['height']) {
49
            // make sure maximum width/height not exceeded
50
            $height = $height > $max['height'] ? $max['height'] : $height;
51
            $width  = $width > $max['width'] ? $max['width'] : $width;
52
        }
53
54
        // load user mapper
55
        $usersMapper = new Mappers\Users;
56
        $usersMapper->load($params['uuid']);
57
58
        // work out filename
59
        $hash     = $f3->hash($asset); // generate unique hash for asset
0 ignored issues
show
Unused Code introduced by
$hash is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
60
        $filename = 'profile_' . $width . 'x' . $height . '.jpg';
61
62
        // return the url if exists
63
        $url      = $usersMapper->profileImageUrl($filename);
64
        if (false !== $url) {
65
            return $f3->reroute($url);
66
        }
67
68
        // 404 if the original asset file does not exist
69
        if (!file_exists($asset->filename)) {
70
            return $f3->status(404);
71
        }
72
73
        // create new resized file
74
        $img      = new \Image($asset->filename);
75
        $img->resize($width, $height);
76
        if (!$f3->write($usersMapper->profileImageFilePath($filename), $img->dump('jpeg', $f3->get('assets.image.default.quality.jpg')))) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $f3->write($usersMapper-...default.quality.jpg'))) of type integer|false is loosely compared to false; this is ambiguous if the integer can be zero. You might want to explicitly use === null instead.

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

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
77
            return $f3->status(404);
78
        }
79
80
        // serve the generated image via the web
81
        return $f3->reroute($url);
82
    }
83
}
84