Passed
Pull Request — master (#17)
by ARCANEDEV
05:21
created

MediaItem   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
eloc 14
c 1
b 0
f 0
dl 0
loc 91
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A toArray() 0 6 1
A jsonSerialize() 0 3 1
A __construct() 0 6 1
A toJson() 0 3 1
1
<?php namespace Arcanesoft\Media\Entities;
2
3
use Illuminate\Contracts\Support\Arrayable;
4
use Illuminate\Contracts\Support\Jsonable;
5
use JsonSerializable;
6
7
/**
8
 * Class     MediaItem
9
 *
10
 * @package  Arcanesoft\Media\Entities
11
 * @author   ARCANEDEV <[email protected]>
12
 */
13
abstract class MediaItem implements Arrayable, Jsonable, JsonSerializable
14
{
15
    /* -----------------------------------------------------------------
16
     |  Constants
17
     | -----------------------------------------------------------------
18
     */
19
20
    const TYPE_DIRECTORY = 'directory';
21
    const TYPE_FILE      = 'file';
22
23
    /* -----------------------------------------------------------------
24
     |  Properties
25
     | -----------------------------------------------------------------
26
     */
27
28
    public $path;
29
30
    public $name;
31
32
    /* -----------------------------------------------------------------
33
     |  Constructor
34
     | -----------------------------------------------------------------
35
     */
36
37
    /**
38
     * MediaItem constructor.
39
     *
40
     * @param  array   $data
41
     * @param  string  $path
42
     */
43
    public function __construct(array $data, string $path)
44
    {
45
        $this->path = $path;
46
        $this->name = $data['name'];
47
48
        $this->load($data, $path);
49
    }
50
51
    abstract protected function load(array $data, string $path);
52
53
    /* -----------------------------------------------------------------
54
     |  Getters
55
     | -----------------------------------------------------------------
56
     */
57
58
    /**
59
     * Get the item type.
60
     *
61
     * @return mixed
62
     */
63
    abstract public function type(): string;
64
65
    /* -----------------------------------------------------------------
66
     |  Other Methods
67
     | -----------------------------------------------------------------
68
     */
69
70
    /**
71
     * Get the instance as an array.
72
     *
73
     * @return array
74
     */
75
    public function toArray()
76
    {
77
        return [
78
            'type' => $this->type(),
79
            'name' => $this->name,
80
            'path' => $this->path,
81
        ];
82
    }
83
84
    /**
85
     * Convert the object to its JSON representation.
86
     *
87
     * @param  int  $options
88
     *
89
     * @return string
90
     */
91
    public function toJson($options = 0)
92
    {
93
        return json_encode($this->jsonSerialize(), $options);
94
    }
95
96
    /**
97
     * Get the instance as an array which should be serialized to JSON.
98
     *
99
     * @return array
100
     */
101
    public function jsonSerialize()
102
    {
103
        return $this->toArray();
104
    }
105
}
106