AvatarModel::uploadImageFile()   A
last analyzed

Complexity

Conditions 4
Paths 6

Size

Total Lines 18
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 12
nc 6
nop 2
dl 0
loc 18
rs 9.2
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of Jitamin.
5
 *
6
 * Copyright (C) Jitamin Team
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Jitamin\Model;
13
14
use Exception;
15
use Jitamin\Foundation\Database\Model;
16
17
/**
18
 * Avatar File.
19
 */
20
class AvatarModel extends Model
21
{
22
    /**
23
     * Path prefix.
24
     *
25
     * @var string
26
     */
27
    const PATH_PREFIX = 'avatars';
28
29
    /**
30
     * Get image filename.
31
     *
32
     * @param int $user_id
33
     *
34
     * @return string
35
     */
36
    public function getFilename($user_id)
37
    {
38
        return $this->db->table(UserModel::TABLE)->eq('id', $user_id)->findOneColumn('avatar_path');
0 ignored issues
show
Documentation introduced by
The property db does not exist on object<Jitamin\Model\AvatarModel>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read 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.");
        }
    }

}

If the property has read access only, you can use the @property-read 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...
39
    }
40
41
    /**
42
     * Add avatar in the user profile.
43
     *
44
     * @param int    $user_id Foreign key
45
     * @param string $path    Path on the disk
46
     *
47
     * @return bool
48
     */
49
    public function create($user_id, $path)
50
    {
51
        $result = $this->db->table(UserModel::TABLE)->eq('id', $user_id)->update([
0 ignored issues
show
Documentation introduced by
The property db does not exist on object<Jitamin\Model\AvatarModel>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read 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.");
        }
    }

}

If the property has read access only, you can use the @property-read 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...
52
            'avatar_path' => $path,
53
        ]);
54
55
        $this->userSession->refresh($user_id);
0 ignored issues
show
Documentation introduced by
The property userSession does not exist on object<Jitamin\Model\AvatarModel>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read 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.");
        }
    }

}

If the property has read access only, you can use the @property-read 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...
56
57
        return $result;
58
    }
59
60
    /**
61
     * Remove avatar from the user profile.
62
     *
63
     * @param int $user_id Foreign key
64
     *
65
     * @return bool
66
     */
67
    public function remove($user_id)
68
    {
69
        try {
70
            $filename = $this->getFilename($user_id);
71
72
            if (!empty($filename)) {
73
                $this->objectStorage->remove($filename);
0 ignored issues
show
Documentation introduced by
The property objectStorage does not exist on object<Jitamin\Model\AvatarModel>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read 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.");
        }
    }

}

If the property has read access only, you can use the @property-read 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...
74
75
                return $this->db->table(UserModel::TABLE)->eq('id', $user_id)->update(['avatar_path' => '']);
0 ignored issues
show
Documentation introduced by
The property db does not exist on object<Jitamin\Model\AvatarModel>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read 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.");
        }
    }

}

If the property has read access only, you can use the @property-read 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...
76
            }
77
        } catch (Exception $e) {
78
            $this->logger->error($e->getMessage());
0 ignored issues
show
Documentation introduced by
The property logger does not exist on object<Jitamin\Model\AvatarModel>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read 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.");
        }
    }

}

If the property has read access only, you can use the @property-read 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...
79
80
            return false;
81
        }
82
83
        return true;
84
    }
85
86
    /**
87
     * Upload avatar image file.
88
     *
89
     * @param int   $user_id
90
     * @param array $file
91
     *
92
     * @return bool
93
     */
94
    public function uploadImageFile($user_id, array $file)
95
    {
96
        try {
97
            if ($file['error'] == UPLOAD_ERR_OK && $file['size'] > 0) {
98
                $destinationFilename = $this->generatePath($user_id, $file['name']);
99
                $this->objectStorage->moveUploadedFile($file['tmp_name'], $destinationFilename);
0 ignored issues
show
Documentation introduced by
The property objectStorage does not exist on object<Jitamin\Model\AvatarModel>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read 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.");
        }
    }

}

If the property has read access only, you can use the @property-read 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...
100
                $this->create($user_id, $destinationFilename);
101
            } else {
102
                throw new Exception('File not uploaded: '.var_export($file['error'], true));
103
            }
104
        } catch (Exception $e) {
105
            $this->logger->error($e->getMessage());
0 ignored issues
show
Documentation introduced by
The property logger does not exist on object<Jitamin\Model\AvatarModel>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read 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.");
        }
    }

}

If the property has read access only, you can use the @property-read 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...
106
107
            return false;
108
        }
109
110
        return true;
111
    }
112
113
    /**
114
     * Upload avatar image content.
115
     *
116
     * @param int    $user_id
117
     * @param string $blob
118
     *
119
     * @return bool
120
     */
121
    public function uploadImageContent($user_id, &$blob)
122
    {
123
        try {
124
            $destinationFilename = $this->generatePath($user_id, 'imageContent');
125
            $this->objectStorage->put($destinationFilename, $blob);
0 ignored issues
show
Documentation introduced by
The property objectStorage does not exist on object<Jitamin\Model\AvatarModel>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read 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.");
        }
    }

}

If the property has read access only, you can use the @property-read 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...
126
            $this->create($user_id, $destinationFilename);
127
        } catch (Exception $e) {
128
            $this->logger->error($e->getMessage());
0 ignored issues
show
Documentation introduced by
The property logger does not exist on object<Jitamin\Model\AvatarModel>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read 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.");
        }
    }

}

If the property has read access only, you can use the @property-read 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...
129
130
            return false;
131
        }
132
133
        return true;
134
    }
135
136
    /**
137
     * Generate the path for a new filename.
138
     *
139
     * @param int    $user_id
140
     * @param string $filename
141
     *
142
     * @return string
143
     */
144
    public function generatePath($user_id, $filename)
145
    {
146
        return implode(DIRECTORY_SEPARATOR, [self::PATH_PREFIX, $user_id, hash('sha1', $filename.time())]);
147
    }
148
}
149