Completed
Push — master ( 5798cc...56eac4 )
by Mahmoud
03:11
created

Request   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 9
lcom 0
cbo 5
dl 0
loc 63
rs 10
c 1
b 0
f 1

3 Methods

Rating   Name   Duplication   Size   Complexity  
A failedValidation() 0 4 1
A all() 0 11 4
A decode() 0 19 4
1
<?php
2
3
namespace App\Port\Request\Abstracts;
4
5
use App\Port\Exception\Exceptions\IncorrectIdException;
6
use App\Port\Exception\Exceptions\ValidationFailedException;
7
use App\Port\HashId\Traits\HashIdTrait;
8
use App\Port\Request\Traits\RequestTrait;
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
    use RequestTrait;
25
26
    /**
27
     * Overriding this function to throw a custom
28
     * exception instead of the default Laravel exception.
29
     *
30
     * @param \Illuminate\Contracts\Validation\Validator $validator
31
     *
32
     * @return mixed|void
33
     */
34
    public function failedValidation(Validator $validator)
35
    {
36
        throw new ValidationFailedException($validator->getMessageBag());
37
    }
38
39
    /**
40
     * Overriding this function to modify the any user input before
41
     * applying the validation rules.
42
     *
43
     * @return  array
44
     */
45
    public function all()
46
    {
47
        $requestData = parent::all();
48
49
        // the hash ID feature must be enabled to use this decoder feature.
50
        if (isset($this->decode) && !empty($this->decode) && Config::get('hello.hash-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...
51
            $requestData = $this->decode($requestData);
52
        }
53
54
        return $requestData;
55
    }
56
57
    /**
58
     * @param array $requestData
59
     *
60
     * @return  array
61
     */
62
    private function decode(Array $requestData)
63
    {
64
        // without decoding the encoded ID's you won't be able to use
65
        // validation features like `exists:table,id`
66
        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...
67
68
            if (isset($requestData[$id])) {
69
                // validate the user is not trying to pass real ID
70
                if (is_int($requestData[$id])) {
71
                    throw new IncorrectIdException('Only Hashed ID\'s allowed to be passed.');
72
                }
73
74
                // perform the decoding
75
                $requestData[$id] = $this->decodeThisId($requestData[$id]);
76
            } // do nothing if the input is incorrect, because what if it's not required!
77
        }
78
79
        return $requestData;
80
    }
81
82
}
83