Completed
Push — master ( 21b5e5...d039a4 )
by Mahmoud
03:43
created

Request::decodeHashedIdsBeforeValidatingThem()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 17
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 17
rs 9.2
c 0
b 0
f 0
cc 4
eloc 7
nc 4
nop 1
1
<?php
2
3
namespace App\Port\Request\Abstracts;
4
5
use App\Containers\Authorization\Traits\AuthorizationTrait;
6
use App\Port\Exception\Exceptions\ValidationFailedException;
7
use App\Port\HashId\Traits\HashIdTrait;
8
use Illuminate\Contracts\Validation\Validator;
9
use Illuminate\Foundation\Http\FormRequest as LaravelFrameworkRequest;
10
use Illuminate\Support\Facades\Config;
11
12
/**
13
 * Class Request
14
 *
15
 * A.K.A (app/Http/Requests/Request.php)
16
 *
17
 * @author  Mahmoud Zalt  <[email protected]>
18
 */
19
abstract class Request extends LaravelFrameworkRequest
20
{
21
22
    use HashIdTrait;
23
    use AuthorizationTrait;
24
25
    /**
26
     * Overriding this function to modify the any user input before
27
     * applying the validation rules.
28
     *
29
     * @return  array
30
     */
31
    public function all()
32
    {
33
        $requestData = parent::all();
34
35
        // the hash ID feature must be enabled to use this decoder feature.
36
        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...
37
            $requestData = $this->decodeHashedIdsBeforeValidatingThem($requestData);
38
        }
39
40
        if (isset($this->urlParameters) && !empty($this->urlParameters)) {
0 ignored issues
show
Documentation introduced by
The property urlParameters 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...
41
            $requestData = $this->applyValidationRulesToUrlParams($requestData);
42
        }
43
44
        return $requestData;
45
    }
46
47
    /**
48
     * apply validation rules to the ID's in the URL, since Laravel
49
     * doesn't validate them by default!
50
     *
51
     * Now you can use validation riles like this: `'id' => 'required|integer|exists:items,id'`
52
     *
53
     * @param array $requestData
54
     *
55
     * @return  array
56
     */
57
    private function applyValidationRulesToUrlParams(Array $requestData)
58
    {
59
        foreach ($this->urlParameters as $param) {
0 ignored issues
show
Documentation introduced by
The property urlParameters 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...
60
            $requestData[$param] = $this->route($param);
61
        }
62
63
        return $requestData;
64
    }
65
66
    /**
67
     * Overriding this function to throw a custom
68
     * exception instead of the default Laravel exception.
69
     *
70
     * @param \Illuminate\Contracts\Validation\Validator $validator
71
     *
72
     * @return mixed|void
73
     */
74
    public function failedValidation(Validator $validator)
75
    {
76
        throw new ValidationFailedException($validator->getMessageBag());
77
    }
78
}
79