Issues (33)

src/Policies/AlbumPolicy.php (5 issues)

1
<?php
2
3
namespace FaithGen\Gallery\Policies;
4
5
use Carbon\Carbon;
6
use FaithGen\Gallery\Helpers\AlbumHelper;
7
use FaithGen\Gallery\Models\Album;
8
use FaithGen\SDK\Models\Ministry;
9
use Illuminate\Auth\Access\HandlesAuthorization;
10
11
class AlbumPolicy
12
{
13
    use HandlesAuthorization;
14
15
    /**
16
     * Determine whether the user can view any albums.
17
     *
18
     * @param \App\Models\Ministry $user
0 ignored issues
show
The type App\Models\Ministry was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
19
     * @return mixed
20
     */
21
    public function viewAny(Ministry $user)
0 ignored issues
show
The parameter $user is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

21
    public function viewAny(/** @scrutinizer ignore-unused */ Ministry $user)

This check looks for 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 album.
28
     *
29
     * @param Ministry $user
30
     * @param Album $album
31
     * @return mixed
32
     */
33
    public function view(Ministry $user, Album $album)
34
    {
35
        return $user->id === $album->ministry_id;
36
    }
37
38
    /**
39
     * Determine whether the user can create albums.
40
     *
41
     * @param \App\Models\Ministry $user
42
     * @return mixed
43
     */
44
    public function create(Ministry $user)
45
    {
46
        $albumsCount = Album::where('ministry_id', $user->id)->whereBetween('created_at', [Carbon::now()->firstOfMonth(), Carbon::now()->lastOfMonth()])->count();
47
48
        return $this->getAuthorization($user, $albumsCount, 'albums');
0 ignored issues
show
It seems like $albumsCount can also be of type Illuminate\Database\Eloquent\Builder and Illuminate\Database\Query\Builder; however, parameter $count of FaithGen\Gallery\Policie...icy::getAuthorization() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

48
        return $this->getAuthorization($user, /** @scrutinizer ignore-type */ $albumsCount, 'albums');
Loading history...
49
    }
50
51
    /**
52
     * Determine whether the user can update the album.
53
     *if.
54
     * @param Ministry $user
55
     * @param Album $album
56
     * @return mixed
57
     */
58
    public function update(Ministry $user, Album $album)
59
    {
60
        return $user->id === $album->ministry_id;
61
    }
62
63
    /**
64
     * Determine whether the user can delete the album.
65
     *
66
     * @param Ministry $user
67
     * @param Album $album
68
     * @return mixed
69
     */
70
    public function delete(Ministry $user, Album $album)
71
    {
72
        return $user->id === $album->ministry_id;
73
    }
74
75
    public function addImages(Ministry $ministry, Album $album)
76
    {
77
        $albumSize = $album->images()->count();
78
        if (strcmp($ministry->id, $album->ministry_id) !== 0) {
79
            return false;
80
        } else {
81
            return self::getAuthorization($ministry, $albumSize, 'images');
0 ignored issues
show
Bug Best Practice introduced by
The method FaithGen\Gallery\Policie...icy::getAuthorization() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

81
            return self::/** @scrutinizer ignore-call */ getAuthorization($ministry, $albumSize, 'images');
Loading history...
82
            $allow = self::getAuthorization($ministry, $albumSize, 'images');
0 ignored issues
show
$allow = self::getAuthor..., $albumSize, 'images') is not reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
83
            if (! $allow) {
84
                return false;
85
            } else {
86
                if ($ministry->account->level === 'Free') {
87
                    $balance = AlbumHelper::$freeAlbumImagesCount - $albumSize;
88
                } elseif ($ministry->account->level === 'Premium') {
89
                    $balance = AlbumHelper::$premiumAlbumImagesCount - $albumSize;
90
                } else {
91
                    $balance = 10000;
92
                }
93
94
                return true;
95
                //return sizeof(request()->images) > $balance;
96
            }
97
        }
98
    }
99
100
    private function getAuthorization(Ministry $ministry, int $count, string $type): bool
101
    {
102
        if (strcmp($type, 'albums') === 0) {
103
            $freeCount = AlbumHelper::$freeAlbumsCount;
104
            $premiumCount = AlbumHelper::$premiumAlbumsCount;
105
        } else {
106
            $freeCount = AlbumHelper::$freeAlbumImagesCount;
107
            $premiumCount = AlbumHelper::$premiumAlbumImagesCount;
108
        }
109
        if ($ministry->account->level === 'Free') {
110
            if ($count >= $freeCount) {
111
                return false;
112
            } else {
113
                return true;
114
            }
115
        } elseif ($ministry->account->level === 'Premium') {
116
            if ($count >= $premiumCount) {
117
                return false;
118
            } else {
119
                return true;
120
            }
121
        } else {
122
            return true;
123
        }
124
    }
125
}
126