Completed
Push — develop-3.0 ( bd5ff0...545efb )
by Mohamed
02:33
created

Project::handleNoteRequest()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
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 extends MiddlewareAbstract
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
    public function handle(Request $request, Closure $next)
48
    {
49
        // Current callback
50
        $callback = current($this->callbacks);
51
        $method   = 'handle' . $callback . 'Request';
52
53
        if ($callback && !$this->$method($request)) {
54
55
            // Current callback does not belong to the request - move next
56
            next($this->callbacks);
57
58
            return $this->handle($request, $next);
59
        }
60
61
        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
    protected function handleIssueRequest(Request $request)
72
    {
73
        return $this->isBelongToProject($request, 'issue');
74
    }
75
76
    /**
77
     * Whether or not the incoming uri is for the issue filter "project/issue/{issue}".
78
     *
79
     * @param Request $request
80
     *
81
     * @return bool
82
     */
83
    protected function handleIssueFilterRequest(Request $request)
84
    {
85
        /** @var ProjectModel|null $project */
86
        $project = $request->route()->getParameter('project');
87
        /** @var ProjectModel\Issue|null $issue */
88
        $issue = $request->route()->getParameter('issue');
89
90
        if ($project === null && $issue && $request->route()->getUri() === 'project/issue/{issue}') {
91
            // Load the project from the issue model
92
            $request->route()->forgetParameter('issue');
93
            $request->route()->setParameter('project', $issue->project);
94
            $request->route()->setParameter('issue', $issue);
95
96
            return true;
97
        }
98
99
        if ($request->route()->getUri() === 'project/issue/{issue_no}') {
100
            $issueNo = $request->route()->getParameter('issue_no');
101
            $segments = explode('-', $issueNo);
102
            $project = ProjectModel::getByKey($segments[0]);
103
            if ($project instanceof ProjectModel) {
104
                $issue = $project->issues()->number($segments[1])->limit(1)->first();
0 ignored issues
show
Bug introduced by
The method number() does not exist on Tinyissue\Model\Project\Issue. Did you maybe mean getIssueNumberAttribute()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
105
                if ($issue instanceof ProjectModel\Issue) {
106
                    $issue->setRelation('project', $project);
107
                    $request->route()->forgetParameter('issue_no');
108
                    $request->route()->setParameter('project', $project);
109
                    $request->route()->setParameter('issue', $issue);
110
111
                    return true;
112
                }
113
            }
114
        }
115
116
        return false;
117
    }
118
119
    /**
120
     * Whether or not the incoming request is valid project note request.
121
     *
122
     * @param Request $request
123
     *
124
     * @return bool
125
     */
126
    protected function handleNoteRequest(Request $request)
127
    {
128
        return $this->isBelongToProject($request, 'note');
129
    }
130
131
    /**
132
     * Whether or not the incoming request is valid project request.
133
     *
134
     * @param Request $request
135
     *
136
     * @return bool
137
     */
138
    protected function handleProjectRequest(Request $request)
139
    {
140
        /** @var ProjectModel|null $project */
141
        $project = $request->route()->getParameter('project');
142
143
        return $project instanceof ProjectModel;
144
    }
145
146
    /**
147
     * Whether or not a model entity relationship with the project is correct.
148
     *
149
     * @param Request $request
150
     * @param string  $entityName
151
     *
152
     * @throws \Symfony\Component\HttpKernel\Exception\HttpException
153
     * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
154
     *
155
     * @return bool
156
     */
157
    protected function isBelongToProject(Request $request, $entityName)
158
    {
159
        /** @var Model $entity */
160
        $entity = $request->route()->getParameter($entityName);
161
162
        /** @var ProjectModel|null $project */
163
        $project = $request->route()->getParameter('project');
164
165
        if (!$entity instanceof Model || !$project instanceof ProjectModel) {
166
            return false;
167
        }
168
169
        // Abort request invalid data
170
        if ((int) $entity->project_id !== (int) $project->id) {
171
            abort(401);
172
        }
173
174
        // Add project instance to the entity
175
        $entity->setRelation('project', $project);
176
177
        return true;
178
    }
179
}
180