Completed
Pull Request — master (#96)
by
unknown
01:15
created

FeedItem   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 104
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 1
dl 0
loc 104
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 2
A create() 0 4 1
A id() 0 6 1
A title() 0 6 1
A updated() 0 6 1
A summary() 0 6 1
A link() 0 6 1
A author() 0 6 1
A image() 0 6 1
A validate() 0 10 3
A __get() 0 8 2
1
<?php
2
3
namespace Spatie\Feed;
4
5
use Exception;
6
use Carbon\Carbon;
7
use Spatie\Feed\Exceptions\InvalidFeedItem;
8
9
class FeedItem
10
{
11
    /** @var string */
12
    protected $id;
13
14
    /** @var string */
15
    protected $title;
16
17
    /** @var \Carbon\Carbon */
18
    protected $updated;
19
20
    /** @var string */
21
    protected $summary;
22
23
    /** @var @var string */
24
    protected $link;
25
26
    /** @var string */
27
    protected $author;
28
    
29
    /** @var string */
30
    protected $image;
31
32
    public function __construct(array $data = [])
33
    {
34
        foreach ($data as $key => $value) {
35
            $this->$key = $value;
36
        }
37
    }
38
39
    public static function create(array $data = [])
40
    {
41
        return new static($data);
42
    }
43
44
    public function id(string $id)
45
    {
46
        $this->id = $id;
47
48
        return $this;
49
    }
50
51
    public function title(string $title)
52
    {
53
        $this->title = $title;
54
55
        return $this;
56
    }
57
58
    public function updated(Carbon $updated)
59
    {
60
        $this->updated = $updated;
61
62
        return $this;
63
    }
64
65
    public function summary(string $summary)
66
    {
67
        $this->summary = $summary;
68
69
        return $this;
70
    }
71
72
    public function link(string $link)
73
    {
74
        $this->link = $link;
75
76
        return $this;
77
    }
78
79
    public function author(string $author)
80
    {
81
        $this->author = $author;
82
83
        return $this;
84
    }
85
    
86
    public function image(string $image)
87
    {
88
        $this->image = $image;
89
90
        return $this;
91
    }
92
93
    public function validate()
94
    {
95
        $requiredFields = ['id', 'title', 'updated', 'summary', 'link', 'author', 'image'];
96
97
        foreach ($requiredFields as $requiredField) {
98
            if (is_null($this->$requiredField)) {
99
                throw InvalidFeedItem::missingField($this, $requiredField);
100
            }
101
        }
102
    }
103
104
    public function __get($key)
105
    {
106
        if (! isset($this->$key)) {
107
            throw new Exception("Property `{$key}` doesn't exist");
108
        }
109
110
        return $this->$key;
111
    }
112
}
113