Completed
Push — v0_9 ( a79c41...154486 )
by Vladimir
08:58 queued 03:17
created

AvatarModel::resetAvatar()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 6
ccs 0
cts 3
cp 0
rs 9.4285
cc 1
eloc 3
nc 1
nop 0
crap 2
1
<?php
2
/**
3
 * This file contains functionality relating to database objects that have avatars and identicons such as teams and players
4
 *
5
 * @package    BZiON\Models
6
 * @license    https://github.com/allejo/bzion/blob/master/LICENSE.md GNU General Public License Version 3
7
 */
8
9
use Identicon\Identicon;
10
use Symfony\Component\HttpFoundation\File\File;
11
12
/**
13
 * A Model that has a URL, an alias, and an avatar
14
 * @package    BZiON\Models
15
 */
16
abstract class AvatarModel extends AliasModel implements NamedModel
17
{
18
    /**
19
     * The url of the object's profile avatar
20
     * @var string
21
     */
22
    protected $avatar;
23
24
    /**
25
     * The location where avatars will be stored
26
     */
27
    const AVATAR_LOCATION = "";
28
29
    /**
30
     * Get the identicon for a model. This function will overwrite the previous avatar
31
     *
32
     * @param string $idData The data (name or id) that will be used to generate an identicon
33
     *
34
     * @return string The path to the generated identicon
35
     */
36 39
    protected function getIdenticon($idData)
37
    {
38 39
        Service::getContainer()->get('logger')
39 39
            ->info('Generating new identicon for "' . $this->getName() . '" in ' . $this->getAvatarPath());
40
41 39
        $identicon = new Identicon();
42 39
        $imageData = $identicon->getImageData($idData, 250);
43
44 39
        $path = $this->getAvatarPath($imageData);
45
46 39
        file_put_contents(DOC_ROOT . $path, $imageData);
47
48 39
        return $path;
49
    }
50
51
    /**
52
     * Set the avatar of the object to be a specific file
53
     *
54
     * @param  File|null $file The avatar file
55
     * @return self
56
     */
57
    public function setAvatarFile($file)
58
    {
59
        if ($file) {
60
            // We don't use File's fread() because it's unavailable in less
61
            // recent PHP versions
62
            $path = $file->getPath() . '/' . $file->getFilename();
63
            $content = file_get_contents($path);
64
65
            $path = $this->getAvatarPath(null, false, false);
66
            $filename = $this->getAvatarFileName($content);
67
68
            $file->move(DOC_ROOT . $path, $filename);
69
70
            $this->setAvatar($path . $filename);
71
        }
72
73
        return $this;
74
    }
75
76
    /**
77
     * Get the path for the image used as the object's avatar
78
     *
79
     * @param  bool $url Whether to return an absolute URL
80
     * @return string  The path for the avatar
81
     */
82 1
    public function getAvatar($url = false)
83
    {
84 1
        if (empty($this->avatar) && $this->avatar !== null) {
85
            $this->resetAvatar();
86
        }
87
88 1
        if ($url) {
89
            return Service::getRequest()->getBaseUrl() . $this->avatar;
90
        }
91
92 1
        return $this->avatar;
93
    }
94
95
    /**
96
     * Change the avatar of the object
97
     *
98
     * @param  string $avatar The file name of the avatar
99
     * @return self
100
     */
101
    public function setAvatar($avatar)
102
    {
103
        $currentAvatar = DOC_ROOT . $this->avatar;
104
105
        if (!empty($this->avatar) && $avatar != $this->avatar && file_exists($currentAvatar)) {
106
            // Remove the old avatar
107
            unlink($currentAvatar);
108
        }
109
110
        // Clear the thumbnail cache
111
        $imagine = Service::getContainer()->get('liip_imagine.cache.manager');
112
        $imagine->remove($this->avatar);
113
114
        return $this->updateProperty($this->avatar, 'avatar', $avatar);
115
    }
116
117
    /**
118
     * Reset the object's avatar to an identicon
119
     *
120
     * @return self
121
     */
122
    public function resetAvatar()
123
    {
124
        $path = $this->getIdenticon($this->getName());
125
126
        return $this->setAvatar($path);
127
    }
128
129
    /**
130
     * Get the path to the avatar, creating its directory if it doesn't exist
131
     *
132
     * @param string|null $content The avatar data
133
     * @param bool $full Whether to return the full absolute path
134
     * @param bool $file Whether to include the name of the file
135
     * @return string The path to the avatar
136
     */
137 39
    private function getAvatarPath($content = null, $full = false, $file = true)
138
    {
139 39
        $path = static::AVATAR_LOCATION . $this->id . '/';
140
141 39
        if (!@file_exists(DOC_ROOT . $path)) {
142 39
            mkdir(DOC_ROOT . $path);
143 39
        }
144
145 39
        if ($full) {
146
            $path = DOC_ROOT . $path;
147
        }
148
149 39
        if ($file) {
150 39
            $path .= $this->getAvatarFileName($content);
151 39
        }
152
153 39
        return $path;
154
    }
155
156
    /**
157
     * Get the filename for the avatar
158
     *
159
     * @param  string|null $content The avatar data
160
     * @return string The file name of the avatar
161
     */
162 39
    private function getAvatarFileName(&$content = null)
163
    {
164
        // Calculate the avatar contents' hash, which is used to force the
165
        // browser to reload an image stored in the cache whenever it changes
166
        // (cache busting)
167
        //
168
        // MD5 is used because it's fast and we don't require a
169
        // cryptographically secure hashing function
170 39
        if ($content !== null) {
171 39
            $hash = substr(md5($content), 0, 7);
172 39
        } else {
173 39
            $hash = 'avatar';
174
        }
175
176 39
        return "$hash.png";
177
    }
178
}
179