Completed
Push — master ( 5f4753...9f13ac )
by Mahmoud
03:17
created

Request::all()   B

Complexity

Conditions 6
Paths 4

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 8.8571
c 0
b 0
f 0
cc 6
eloc 7
nc 4
nop 0
1
<?php
2
3
namespace App\Port\Request\Abstracts;
4
5
use App\Containers\User\Models\User;
6
use App\Port\Exception\Exceptions\IncorrectIdException;
7
use App\Port\Exception\Exceptions\ValidationFailedException;
8
use App\Port\HashId\Traits\HashIdTrait;
9
use Illuminate\Contracts\Validation\Validator;
10
use Illuminate\Foundation\Http\FormRequest as LaravelFrameworkRequest;
11
use Illuminate\Support\Facades\Config;
12
13
/**
14
 * Class Request
15
 *
16
 * A.K.A (app/Http/Requests/Request.php)
17
 *
18
 * @author  Mahmoud Zalt  <[email protected]>
19
 */
20
abstract class Request extends LaravelFrameworkRequest
21
{
22
23
    use HashIdTrait;
24
25
    /**
26
     * This function will be called from the Requests (authorize) to check if a user
27
     * has permission to perform an action.
28
     * User can set multiple permissions (separated with "|") and if the user has
29
     * any of the permissions, he will be authorize to proceed with this action.
30
     *
31
     * @return  bool
32
     */
33
    public function hasAccess(User $user = null)
34
    {
35
        if(!$user){
36
            $user = $this->user();
37
        }
38
39
        // $this->access is optionally set on the Request
40
41
        // allow access when the access is not defined
42
        // allow access when nothing no roles or permissions are declared
43
        if (!isset($this->access) || !isset($this->access['permission'])) {
0 ignored issues
show
Documentation introduced by
The property access does not exist on object<App\Port\Request\Abstracts\Request>. 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...
44
            return true;
45
        }
46
47
        // allow access if has permission set but is empty or null
48
        if (!$this->access['permission']) {
0 ignored issues
show
Documentation introduced by
The property access does not exist on object<App\Port\Request\Abstracts\Request>. 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...
49
            return true;
50
        }
51
52
        $permissions = explode('|', $this->access['permission']);
0 ignored issues
show
Documentation introduced by
The property access does not exist on object<App\Port\Request\Abstracts\Request>. 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...
53
54
        $hasAccess = array_map(function ($permission) use ($user){
55
            return $user->hasPermissionTo($permission);
56
        }, $permissions);
57
58
        // allow access if user has access to any of the defined permissions.
59
        return in_array(true, $hasAccess);
60
    }
61
62
    /**
63
     * Overriding this function to modify the any user input before
64
     * applying the validation rules.
65
     *
66
     * @return  array
67
     */
68
    public function all()
69
    {
70
        $requestData = parent::all();
71
72
        // the hash ID feature must be enabled to use this decoder feature.
73
        if (Config::get('hello.hash-id') && isset($this->decode) && !empty($this->decode)) {
0 ignored issues
show
Documentation introduced by
The property decode does not exist on object<App\Port\Request\Abstracts\Request>. 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...
74
            $requestData = $this->decodeHashedIdsBeforeValidatingThem($requestData);
75
        }
76
77
        if (isset($this->applyRulesOn) && !empty($this->applyRulesOn)) {
0 ignored issues
show
Documentation introduced by
The property applyRulesOn does not exist on object<App\Port\Request\Abstracts\Request>. 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...
78
            $requestData = $this->applyValidationRulesToUrlParams($requestData);
79
        }
80
81
        return $requestData;
82
    }
83
84
    /**
85
     * without decoding the encoded ID's you won't be able to use
86
     * validation features like `exists:table,id`
87
     *
88
     * @param array $requestData
89
     *
90
     * @return  array
91
     */
92
    private function decodeHashedIdsBeforeValidatingThem(Array $requestData)
93
    {
94
        foreach ($this->decode as $id) {
0 ignored issues
show
Documentation introduced by
The property decode does not exist on object<App\Port\Request\Abstracts\Request>. 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...
95
96
            if (isset($requestData[$id])) {
97
                // validate the user is not trying to pass real ID
98
                if (is_int($requestData[$id])) {
99
                    throw new IncorrectIdException('Only Hashed ID\'s allowed to be passed.');
100
                }
101
102
                // perform the decoding
103
                $requestData[$id] = $this->decodeThisId($requestData[$id]);
104
            } // do nothing if the input is incorrect, because what if it's not required!
105
        }
106
107
        return $requestData;
108
    }
109
110
    /**
111
     * apply validation rules to the ID's in the URL, since Laravel
112
     * doesn't validate them by default!
113
     *
114
     * Now you can use validation riles like this: `'id' => 'required|integer|exists:items,id'`
115
     *
116
     * @param array $requestData
117
     *
118
     * @return  array
119
     */
120
    private function applyValidationRulesToUrlParams(Array $requestData)
121
    {
122
        foreach ($this->applyRulesOn as $param) {
0 ignored issues
show
Documentation introduced by
The property applyRulesOn does not exist on object<App\Port\Request\Abstracts\Request>. 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...
123
            $requestData[$param] = $this->route($param);
124
        }
125
126
        return $requestData;
127
    }
128
129
    /**
130
     * Overriding this function to throw a custom
131
     * exception instead of the default Laravel exception.
132
     *
133
     * @param \Illuminate\Contracts\Validation\Validator $validator
134
     *
135
     * @return mixed|void
136
     */
137
    public function failedValidation(Validator $validator)
138
    {
139
        throw new ValidationFailedException($validator->getMessageBag());
140
    }
141
}
142