Completed
Push — master ( f3c520...7ea85d )
by Phecho
03:40
created

VisibilityScope::apply()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 10
rs 9.4286
cc 3
eloc 6
nc 2
nop 2
1
<?php
2
3
/*
4
 * This file is part of Gitamin.
5
 *
6
 * Copyright (C) 2015-2016 The Gitamin 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 Gitamin\Scopes;
13
14
use Gitamin\Models\User;
15
use Illuminate\Database\Eloquent\Builder;
16
use Illuminate\Database\Eloquent\Model;
17
use Illuminate\Database\Eloquent\ScopeInterface;
18
use Illuminate\Support\Facades\Auth;
19
20
/**
21
 * Visibility scope.
22
 *
23
 * This is an Eloquent scope that can (globally) restrict access to a model.
24
 *
25
 * Assumptions:
26
 *
27
 * - The model has a `visibility_level` column of an integer type.
28
 * - The model has a `user_id`(creator_id) column that refers to a user model.
29
 */
30
final class VisibilityScope implements ScopeInterface
31
{
32
    // The item is visible to anyone
33
    const VISIBILITY_PUBLIC = 0;
34
    // The item is only visible to its owner
35
    const VISIBILITY_PRIVATE = 1;
36
    // The item is only visible to its owner and other logged in users
37
    const VISIBILITY_LOGGED_IN = 2;
38
39
    private $tableName;
40
41
    /**
42
     * Apply the scopes for model.
43
     *
44
     * @param \Illuminate\Database\Eloquent\Builder  $builder
45
     * @param \Illuminate\Database\Eloquent\Model  $model
46
     *
47
     * @return void
48
     */
49
    public function apply(Builder $builder, Model $model)
50
    {
51
        $this->tableName = $model->getTable();
52
53
        if (Auth::check() && Auth::user()->isApproved()) {
54
            $this->applyLoggedInScope($builder, Auth::user());
55
        } else {
56
            $this->applyPublicScope($builder);
57
        }
58
    }
59
60
    /**
61
     * Apply public scope.
62
     *
63
     * @param \Illuminate\Database\Eloquent\Builder $query
64
     *
65
     * @return \Illuminate\Database\Eloquent\Builder
66
     */
67
    public function applyPublicScope(Builder $query)
68
    {
69
        return $query->where('visibility_level', '=', self::VISIBILITY_PUBLIC);
70
    }
71
72
    /**
73
     * Apply logged in scope.
74
     *
75
     * @param \Illuminate\Database\Eloquent\Builder  $query
76
     * @param \Gitamin\Models\User  $user
77
     *
78
     * @return \Illuminate\Database\Eloquent\Builder
79
     */
80
    public function applyLoggedInScope(Builder $query, User $user)
81
    {
82
        $isPrivate = function ($query) use ($user) {
83
            $query->where('visibility_level', '=', self::VISIBILITY_PRIVATE)
84
                  ->where("{$this->tableName}.creator_id", '=', $user->id);
0 ignored issues
show
Documentation introduced by
The property id does not exist on object<Gitamin\Models\User>. 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...
85
        };
86
87
        $whereVisible = function ($query) use ($isPrivate) {
88
            $query->whereIn('visibility_level', [self::VISIBILITY_PUBLIC, self::VISIBILITY_LOGGED_IN])
89
                  ->orWhere($isPrivate);
90
        };
91
92
        return $query->where($whereVisible);
93
    }
94
95
    /**
96
     * Remove the scopes for model.
97
     *
98
     * @param \Illuminate\Database\Eloquent\Builder  $builder
99
     * @param \Illuminate\Database\Eloquent\Model  $model
100
     *
101
     * @return void
102
     */
103
    public function remove(Builder $builder, Model $model)
104
    {
105
        // TODO: Implement me
106
    }
107
}
108