UserRepository::updateAvatar()   B
last analyzed

Complexity

Conditions 3
Paths 5

Size

Total Lines 28
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 18
nc 5
nop 3
dl 0
loc 28
ccs 0
cts 22
cp 0
crap 12
rs 8.8571
c 0
b 0
f 0
1
<?php
2
/*
3
 * This file is part of the Laravel Platfourm package.
4
 *
5
 * (c) Avtandil Kikabidze aka LONGMAN <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Longman\Platfourm\User\Repositories\Eloquent;
12
13
use Exception;
14
use GuzzleHttp\Client;
15
use Illuminate\Contracts\Cache\Repository as Cache;
16
use Longman\Platfourm\Contracts\Auth\AuthUserService;
17
use Longman\Platfourm\Contracts\Repository\Repository;
18
use Longman\Platfourm\Contracts\Repository\RepositoryCriteria;
19
use Longman\Platfourm\Repository\Eloquent\BaseRepository;
20
use Longman\Platfourm\User\Models\Eloquent\User;
21
22
class UserRepository extends BaseRepository implements Repository, RepositoryCriteria
23
{
24
25
    /**
26
     * Specify Model class name.
27
     *
28
     * @return string
29
     */
30
    public function model()
31
    {
32
        if (class_exists(\App\Models\User::class)) {
33
            return \App\Models\User::class;
34
        }
35
36
        return User::class;
37
    }
38
39
    /**
40
     * Update avatar from gravatar
41
     *
42
     * @param \GuzzleHttp\Client                                $httpClient
43
     * @param \Illuminate\Contracts\Cache\Repository            $cache
44
     * @param \Longman\Platfourm\Contracts\Auth\AuthUserService $authUserService
45
     * @return bool
46
     */
47
    public function updateAvatar(Client $httpClient, Cache $cache, AuthUserService $authUserService)
48
    {
49
        $user_id   = $authUserService->user()->id;
0 ignored issues
show
Documentation introduced by
The property id does not exist on object<Longman\Platfourm...r\Models\Eloquent\User>. 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...
50
        $email     = $authUserService->user()->email;
0 ignored issues
show
Documentation introduced by
The property email does not exist on object<Longman\Platfourm...r\Models\Eloquent\User>. 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...
51
        $hash      = md5(strtolower(trim($email)));
52
        $cache_key = 'gravatar_' . $hash;
53
54
        if ($cache->has($cache_key)) {
55
            return true;
56
        }
57
58
        $cache->put($cache_key, 1, config('cms.user.avatar.gravatar_cache_ttl', 1440));
59
60
        $grav_url = 'https://www.gravatar.com/avatar/' . $hash . '&s=512&r=x';
61
62
        $image_name = $hash . '.jpg';
63
        $path       = public_path(config('cms.user.avatar.path', 'cache/avatar'));
64
        try {
65
            $httpClient->request('GET', $grav_url, ['sink' => $path . '/' . $image_name]);
66
67
            $this->model->findOrFail($user_id)->update(['avatar' => $image_name]);
68
            $authUserService->user()->avatar = $image_name;
0 ignored issues
show
Documentation introduced by
The property avatar does not exist on object<Longman\Platfourm...r\Models\Eloquent\User>. 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...
69
        } catch (Exception $e) {
70
            return false;
71
        }
72
73
        return true;
74
    }
75
76
    /**
77
     * Attach role to user.
78
     *
79
     * @param type $roleName
80
     *
81
     * @return type
82
     */
83
    public function attachRole($roleName)
84
    {
85
        return $this->model->attachRole($roleName);
86
    }
87
88
    /**
89
     * Attach permission to user.
90
     *
91
     * @param string $permissionName
92
     * @param array  $options
93
     *
94
     * @return type
95
     */
96
    public function attachPermission($permissionName, array $options = [])
97
    {
98
        return $this->model->attachPermission($permissionName, $options);
99
    }
100
101
    /**
102
     * Activate user with the given id.
103
     *
104
     * @param type $id
105
     *
106
     * @return type
107
     */
108
    public function activate($id)
109
    {
110
        $user = $this->model->whereId($id)->whereStatus('New')->first();
111
112
        if (is_null($user)) {
113
            return false;
114
        }
115
116
        if ($user->update(['status' => 'Active'])) {
117
            return true;
118
        }
119
120
        return false;
121
    }
122
123
}
124