Completed
Push — develop ( a45eac...0b88af )
by Mohamed
02:46
created

Project::isBelongToProject()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 19
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4.0218

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 19
ccs 8
cts 9
cp 0.8889
rs 9.2
cc 4
eloc 8
nc 3
nop 2
crap 4.0218
1
<?php
2
3
/*
4
 * This file is part of the Tinyissue package.
5
 *
6
 * (c) Mohamed Alsharaf <[email protected]>
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 Tinyissue\Http\Middleware;
13
14
use Closure;
15
use Illuminate\Database\Eloquent\Model;
16
use Illuminate\Http\Request;
17
use Tinyissue\Model\Project as ProjectModel;
18
19
/**
20
 * Project is a Middleware class to for checking if the route parameters are correct.
21
 * e.g. The issue id is belongs to the project id.
22
 *
23
 * @author Mohamed Alsharaf <[email protected]>
24
 */
25
class Project
26
{
27
    /**
28
     * List of callbacks to handle the incoming request.
29
     *
30
     * @var array
31
     */
32
    protected $callbacks = [
33
        'Issue',
34
        'IssueFilter',
35
        'Note',
36
        'Project',
37
    ];
38
39
    /**
40
     * Handle an incoming request.
41
     *
42
     * @param Request  $request
43
     * @param \Closure $next
44
     *
45
     * @return mixed
46
     */
47 39
    public function handle(Request $request, Closure $next)
48
    {
49
        // Current callback
50 39
        $callback = current($this->callbacks);
51 39
        $method   = 'handle' . $callback . 'Request';
52
53 39
        if ($callback && !$this->$method($request)) {
54
55
            // Current callback does not belong to the request - move next
56 34
            next($this->callbacks);
57
58 34
            return $this->handle($request, $next);
59
        }
60
61 39
        return $next($request);
62
    }
63
64
    /**
65
     * Whether or not the incoming request is valid project issue request.
66
     *
67
     * @param Request $request
68
     *
69
     * @return bool
70
     */
71 39
    protected function handleIssueRequest(Request $request)
72
    {
73 39
        return $this->isBelongToProject($request, 'issue');
74
    }
75
76
    /**
77
     * Whether or not a model entity relationship with the project is correct.
78
     *
79
     * @param Request $request
80
     * @param string  $entityName
81
     *
82
     * @throws \Symfony\Component\HttpKernel\Exception\HttpException
83
     * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
84
     *
85
     * @return bool
86
     */
87 39
    protected function isBelongToProject(Request $request, $entityName)
88
    {
89
        /** @var Model $entity */
90 39
        $entity = $request->route()->getParameter($entityName);
91
92
        /** @var ProjectModel|null $project */
93 39
        $project = $request->route()->getParameter('project');
94
95 39
        if (!$entity instanceof Model || !$project instanceof ProjectModel) {
96 34
            return false;
97
        }
98
99
        // Abort request invalid data
100 21
        if ($entity->project_id !== $project->id) {
101 2
            abort(401);
102
        }
103
104 20
        return true;
105
    }
106
107
    /**
108
     * Whether or not the incoming uri is for the issue filter "project/issue/{issue}".
109
     *
110
     * @param Request $request
111
     *
112
     * @return bool
113
     */
114 34
    protected function handleIssueFilterRequest(Request $request)
115
    {
116
        /** @var ProjectModel|null $project */
117 34
        $project = $request->route()->getParameter('project');
118
        /** @var ProjectModel\Issue|null $issue */
119 34
        $issue = $request->route()->getParameter('issue');
120
121 34
        if ($project === null && $issue && $request->route()->getUri() === 'project/issue/{issue}') {
122
            // Load the project from the issue model
123 1
            $request->route()->forgetParameter('issue');
124 1
            $request->route()->setParameter('project', $issue->project);
125 1
            $request->route()->setParameter('issue', $issue);
126
127 1
            return true;
128
        }
129
130 33
        return false;
131
    }
132
133
    /**
134
     * Whether or not the incoming request is valid project note request.
135
     *
136
     * @param Request $request
137
     *
138
     * @return bool
139
     */
140 33
    protected function handleNoteRequest(Request $request)
141
    {
142 33
        return $this->isBelongToProject($request, 'note');
143
    }
144
145
    /**
146
     * Whether or not the incoming request is valid project note request.
147
     *
148
     * @param Request $request
149
     *
150
     * @return bool
151
     */
152 33
    protected function handleProjectRequest(Request $request)
153
    {
154
        /** @var ProjectModel|null $project */
155 33
        $project = $request->route()->getParameter('project');
156
157 33
        if (auth()->guest() && $project instanceof ProjectModel && $project->isPrivate()) {
158
            abort(401);
159
        }
160
161 33
        return true;
162
    }
163
}
164