Completed
Push — dev-master ( 2b6108...c85948 )
by Vijay
05:50
created

Users::profileImageUrlPath()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 3
eloc 5
nc 4
nop 0
dl 0
loc 8
rs 9.4285
c 2
b 0
f 0
1
<?php
2
3
namespace FFCMS\Mappers;
4
5
use FFCMS\{Traits, Models};
6
7
/**
8
 * Users Mapper 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
 * @property int    $id
15
 * @property string $uuid
16
 * @property string $password
17
 * @property string $email
18
 * @property string $firstname
19
 * @property string $lastname
20
 * @property string $scopes
21
 * @property string $status
22
 * @property string $password_question
23
 * @property string $password_answer
24
 * @property string $created
25
 * @property string $login_count
26
 * @property string $login_last
27
 */
28
class Users extends Mapper
29
{
30
    use Traits\UrlHelper;
31
32
    /**
33
     * Fields and their visibility to clients, boolean or string of visible field name
34
     *
35
     * @var array $fieldsVisible
36
     */
37
    public $fieldsVisible = [
38
        'uuid'              => 'id',
39
        'password'          => false,
40
        'scopes'            => false,
41
        'login_count'       => false,
42
    ];
43
44
    /**
45
     * Fields that are editable to clients, boolean or string of visible field name
46
     *
47
     * @var array $fieldsEditable
48
     */
49
    public $fieldsEditable = [
50
        'email',
51
        'firstname',
52
        'lastname',
53
        'password_question',
54
        'password_answer',
55
    ];
56
57
    /**
58
     * Filter rules for fields
59
     *
60
     * @var array $filterRules
61
     * @link https://github.com/Wixel/GUMP
62
     */
63
    public $filterRules = [
64
        'uuid'              => 'trim|sanitize_string|lower',
65
        'password'          => 'trim|sanitize_string',
66
        'email'             => 'trim|sanitize_string|sanitize_email|lower',
67
        'firstname'         => 'trim|sanitize_string',
68
        'lastname'          => 'trim|sanitize_string',
69
        'scopes'            => 'trim|sanitize_string|lower',
70
        'status'            => 'trim|sanitize_string|lower',
71
        'password_question' => 'trim|sanitize_string',
72
        'password_answer'   => 'trim|sanitize_string',
73
        'created'           => 'trim|sanitize_string',
74
        'login_count'       => 'sanitize_numbers|whole_number',
75
        'login_last'        => 'trim|sanitize_string',
76
    ];
77
78
    /**
79
     * Validation rules for fields
80
     *
81
     * @var array $validationRules
82
     * @link https://github.com/Wixel/GUMP
83
     */
84
    public $validationRules = [
85
        'uuid'              => 'alpha_dash',
86
        'email'             => 'valid_email',
87
        'firstname'         => 'valid_name',
88
    ];
89
90
    /**
91
     * Create if needed, and return the path to the user profile image
92
     *
93
     * @param string $uuid the user uuid
0 ignored issues
show
Bug introduced by
There is no parameter named $uuid. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
94
     * @return string $path to the profile image
95
     */
96
    public function profileImageFilePath()
97
    {
98
        $f3  = \Base::instance();
99
        $dir = $f3->get('assets.dir') . '/img/users/' . $this->uuid;
100
        if (!file_exists($dir)) {
101
            mkdir($dir, 0777, true);
102
        }
103
        return $dir . '/' . 'profile.png';
104
    }
105
106
    /**
107
     * Return the URL path to the image if exists or false
108
     *
109
     * @param string $uuid the user uuid
0 ignored issues
show
Bug introduced by
There is no parameter named $uuid. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
110
     * @return bool true if the profile image exists
111
     */
112
    public function profileImageExists()
113
    {
114
        return file_exists($this->profileImageFilePath());
115
    }
116
117
    /**
118
     * Return the URL path to the image if exists or false
119
     *
120
     * @param string $uuid the user uuid
0 ignored issues
show
Bug introduced by
There is no parameter named $uuid. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
121
     * @return false|string return the url path or false if not exists
122
     */
123
    public function profileImageUrlPath()
124
    {
125
        $url = $this->profileImageExists() ? '/assets/img/users/' . $this->uuid . '/profile.png' : false;
126
        if (empty($url)) {
127
            return false;
128
        }
129
        return $url . '?' . filesize($this->profileImageFilePath());
130
    }
131
132
    /**
133
     * Create profile image from given file
134
     *
135
     * @param string $file path to file
136
     * @return boolean if the file was written and and asset record created
0 ignored issues
show
Documentation introduced by
Should the return type not be false|array? Also, consider making the array more specific, something like array<String>, or String[].

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.

If the return type contains the type array, this check recommends the use of a more specific type like String[] or array<String>.

Loading history...
137
     */
138
    public function profileImageCreate($file)
139
    {
140
        if (!file_exists($file)) {
141
            throw new Exceptions\Exception('Profile image creation file does not exist.');
142
        }
143
        $f3 = \Base::instance();
144
145
        // read exif metadata
146
        $reader = \PHPExif\Reader\Reader::factory(\PHPExif\Reader\Reader::TYPE_NATIVE);
147
        $exif = $reader->read($file);
148
        $metadata = $exif->getData();
149
        unset($exif);
150
151
        // convert image to png
152
        $img = new \Image($file);
153
        $img->resize(512, 512);
154
155
        // convert to .png, create new profile image file
156
        $profileImagePath = $this->profileImageFilePath();
157
        if (!$f3->write($profileImagePath, $img->dump('png', 9))) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $f3->write($profileImage..., $img->dump('png', 9)) 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...
158
            return false;
159
        }
160
161
        // create asset table entry
162
        $asset = new Assets;
163
164
        // load pre existing asset
165
        $asset->load(['users_uuid = ? AND ' . $this->db->quoteKey('key') . ' = ?', $this->uuid, 'profile-image']);
166
167
        // set values
168
        $asset->users_uuid = $this->uuid;
169
        $asset->filename = $profileImagePath;
170
        $asset->name = $this->firstname . ' ' . $this->lastname;
171
        $asset->description = $this->firstname . ' ' . $this->lastname . ' Profile Image';
172
        $asset->size = filesize($profileImagePath);
173
        $asset->url = $this->url($this->profileImageUrlPath());
0 ignored issues
show
Security Bug introduced by
It seems like $this->profileImageUrlPath() targeting FFCMS\Mappers\Users::profileImageUrlPath() can also be of type false; however, FFCMS\Traits\UrlHelper::url() does only seem to accept string, did you maybe forget to handle an error condition?
Loading history...
174
        $asset->type = 'image/png';
175
        $asset->key = 'profile_image';
176
        $asset->groups = 'users';
177
        $asset->categories = 'profile';
0 ignored issues
show
Documentation introduced by
The property categories does not exist on object<FFCMS\Mappers\Assets>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
178
        $asset->tags = 'users,profile';
179
        $asset->metadata = json_encode($metadata, JSON_PRETTY_PRINT);
180
181
        return $asset->save();
182
    }
183
}
184