Passed
Push — master ( 034e39...aabddd )
by Andrea Marco
04:28 queued 11s
created

Dto::fromRequest()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 2
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 5
ccs 3
cts 3
cp 1
crap 2
rs 10
1
<?php
2
3
namespace Cerbero\LaravelDto;
4
5
use Cerbero\Dto\Dto as BaseDto;
6
use Cerbero\Dto\Manipulators\Listener as BaseListener;
7
use Cerbero\LaravelDto\Manipulators\Listener;
8
use Illuminate\Contracts\Support\Arrayable;
9
use Illuminate\Contracts\Support\Jsonable;
10
use Illuminate\Database\Eloquent\Model;
11
use Illuminate\Http\Request;
12
use Illuminate\Support\Enumerable;
0 ignored issues
show
Bug introduced by
The type Illuminate\Support\Enumerable was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
13
use Illuminate\Support\Traits\Macroable;
14
use JsonSerializable;
15
use Traversable;
16
17
use const Cerbero\Dto\CAST_PRIMITIVES;
0 ignored issues
show
Bug introduced by
The constant Cerbero\Dto\CAST_PRIMITIVES was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
18
use const Cerbero\Dto\IGNORE_UNKNOWN_PROPERTIES;
0 ignored issues
show
Bug introduced by
The constant Cerbero\Dto\IGNORE_UNKNOWN_PROPERTIES was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
19
use const Cerbero\Dto\NONE;
0 ignored issues
show
Bug introduced by
The constant Cerbero\Dto\NONE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
20
use const Cerbero\Dto\PARTIAL;
0 ignored issues
show
Bug introduced by
The constant Cerbero\Dto\PARTIAL was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
21
22
/**
23
 * The data transfer object.
24
 *
25
 */
26
abstract class Dto extends BaseDto implements Arrayable, Jsonable
27
{
28
    use Macroable;
0 ignored issues
show
Bug introduced by
The trait Illuminate\Support\Traits\Macroable requires the property $name which is not provided by Cerbero\LaravelDto\Dto.
Loading history...
29
30
    /**
31
     * Retrieve an instance of DTO from the given or current request
32
     *
33
     * @param Request|null $request
34
     * @param int $flags
35
     * @return self
36
     */
37 3
    public static function fromRequest(Request $request = null, int $flags = NONE): self
38
    {
39 3
        $request = $request ?: Request::capture();
40
41 3
        return static::make($request->all(), $flags | PARTIAL | IGNORE_UNKNOWN_PROPERTIES);
42
    }
43
44
    /**
45
     * Retrieve an instance of DTO from the request
46
     *
47
     * @param Model $model
48
     * @param int $flags
49
     * @return self
50
     */
51 3
    public static function fromModel(Model $model, int $flags = NONE): self
52
    {
53 3
        return static::make($model->toArray(), $flags | CAST_PRIMITIVES | PARTIAL | IGNORE_UNKNOWN_PROPERTIES);
54
    }
55
56
    /**
57
     * Retrieve an instance of DTO from the given source
58
     *
59
     * @param mixed $source
60
     * @param int $flags
61
     * @return self
62
     */
63 3
    public static function from($source, int $flags = NONE): self
64
    {
65 3
        if ($source instanceof Enumerable) {
66 2
            $source = $source->all();
67 3
        } elseif ($source instanceof Arrayable) {
68 3
            $source = $source->toArray();
69 3
        } elseif ($source instanceof Jsonable) {
70 3
            $source = json_decode($source->toJson(), true);
71 3
        } elseif ($source instanceof JsonSerializable) {
72 3
            $source = $source->jsonSerialize();
73 3
        } elseif ($source instanceof Traversable) {
74 3
            $source = iterator_to_array($source);
75
        }
76
77 3
        return static::make((array) $source, $flags);
78
    }
79
80
    /**
81
     * Retrieve the listener instance
82
     *
83
     * @return BaseListener
84
     */
85 24
    protected function getListener(): BaseListener
86
    {
87 24
        return Listener::instance();
88
    }
89
}
90