NewsPolicy::create()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.8333
c 0
b 0
f 0
cc 3
nc 3
nop 1
1
<?php
2
3
namespace FaithGen\News\Policies;
4
5
use Carbon\Carbon;
6
use FaithGen\News\Helpers\NewsHelper;
7
use FaithGen\News\Models\News;
8
use FaithGen\SDK\Models\Ministry;
9
use Illuminate\Auth\Access\HandlesAuthorization;
10
11
class NewsPolicy
12
{
13
    use HandlesAuthorization;
14
15
    /**
16
     * Determine whether the user can view any news.
17
     *
18
     * @param \App\Models\Ministry $user
19
     * @return mixed
20
     */
21
    public function viewAny(Ministry $user)
0 ignored issues
show
Unused Code introduced by
The parameter $user is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
22
    {
23
        //
24
    }
25
26
    /**
27
     * Determine whether the user can view the news.
28
     *
29
     * @param Ministry $user
30
     * @param News $news
31
     * @return mixed
32
     */
33
    public function view(Ministry $user, News $news)
34
    {
35
        return $user->id === $news->ministry_id;
0 ignored issues
show
Documentation introduced by
The property ministry_id does not exist on object<FaithGen\News\Models\News>. 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...
36
    }
37
38
    /**
39
     * Determine whether the user can create news.
40
     *
41
     * @param Ministry $user
42
     * @return mixed
43
     */
44
    public function create(Ministry $user)
45
    {
46
        if ($user->account->level !== 'Free') {
47
            return true;
48
        } else {
49
            $newsCount = News::where('ministry_id', $user->id)->whereBetween('created_at', [Carbon::now()->firstOfMonth(), Carbon::now()->lastOfMonth()])->count();
50
            if ($newsCount >= NewsHelper::$freeNewsCount) {
51
                return false;
52
            } else {
53
                return true;
54
            }
55
        }
56
    }
57
58
    /**
59
     * Determine whether the user can update the news.
60
     *
61
     * @param \App\Models\Ministry $user
62
     * @param News $news
63
     * @return mixed
64
     */
65
    public function update(Ministry $user, News $news)
66
    {
67
        return $user->id === $news->ministry_id;
0 ignored issues
show
Documentation introduced by
The property ministry_id does not exist on object<FaithGen\News\Models\News>. 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...
68
    }
69
70
    /**
71
     * Determine whether the user can delete the news.
72
     *
73
     * @param \App\Models\Ministry $user
74
     * @param News $news
75
     * @return mixed
76
     */
77
    public static function delete(Ministry $user, News $news)
78
    {
79
        return $user->id === $news->ministry_id;
0 ignored issues
show
Documentation introduced by
The property ministry_id does not exist on object<FaithGen\News\Models\News>. 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...
80
    }
81
}
82