ExternalArticle::getUrl()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * This file is part of laravel.su package.
4
 * For the full copyright and license information, please view the LICENSE
5
 * file that was distributed with this source code.
6
 */
7
declare(strict_types=1);
8
9
namespace App\Services\DataProviders;
10
11
use Carbon\Carbon;
12
use App\Models\Article;
13
use Illuminate\Contracts\Support\Arrayable;
14
15
/**
16
 * Class ExternalArticle.
17
 */
18
class ExternalArticle implements Arrayable
19
{
20
    /**
21
     * @var string
22
     */
23
    private $title;
24
25
    /**
26
     * @var string
27
     */
28
    private $body;
29
30
    /**
31
     * @var string
32
     */
33
    private $preview = '';
34
35
    /**
36
     * @var string
37
     */
38
    private $url;
39
40
    /**
41
     * @var \DateTime
42
     */
43
    private $createdAt;
44
45
    /**
46
     * @var array|string[]
47
     */
48
    private $images = [];
49
50
    /**
51
     * ExternalArticle constructor.
52
     * @param string $title
53
     * @param string $body
54
     * @param string $url
55
     */
56
    public function __construct(string $title, string $body, string $url)
57
    {
58
        $this->title = $title;
59
        $this->body = $body;
60
        $this->url = $url;
61
62
        $this->createdAt = Carbon::now();
63
    }
64
65
    /**
66
     * @return string
67
     */
68
    public function getPreview(): string
69
    {
70
        return $this->preview;
71
    }
72
73
    /**
74
     * @param  string                $preview
75
     * @return $this|ExternalArticle
76
     */
77
    public function setPreview(string $preview): ExternalArticle
78
    {
79
        $this->preview = $preview;
80
81
        return $this;
82
    }
83
84
    /**
85
     * @param  string $image
86
     * @return $this
87
     */
88
    public function addImageUrl(string $image)
89
    {
90
        $this->images[] = $image;
91
92
        return $this;
93
    }
94
95
    /**
96
     * @return array
97
     */
98
    public function getImages(): array
99
    {
100
        return [];
101
    }
102
103
    /**
104
     * @return string
105
     */
106
    public function getTitle(): string
107
    {
108
        return $this->title;
109
    }
110
111
    /**
112
     * @return string
113
     */
114
    public function getBody(): string
115
    {
116
        return $this->body;
117
    }
118
119
    /**
120
     * @return string
121
     */
122
    public function getUrl(): string
123
    {
124
        return $this->url;
125
    }
126
127
    /**
128
     * @return \DateTime
129
     */
130
    public function getCreatedAt(): \DateTime
131
    {
132
        return $this->createdAt;
133
    }
134
135
    /**
136
     * @param  \DateTime             $createdAt
137
     * @return $this|ExternalArticle
138
     */
139
    public function setCreatedAt(\DateTime $createdAt)
140
    {
141
        $this->createdAt = $createdAt;
142
143
        return $this;
144
    }
145
146
    /**
147
     * @return array
148
     */
149
    public function toArray(): array
150
    {
151
        return [
152
            'title'      => $this->title,
153
            'url'        => $this->url,
154
            'images'     => $this->images,
155
            'content'    => $this->body,
156
            'created_at' => $this->createdAt,
157
        ];
158
    }
159
160
    /**
161
     * @param  Article $article
162
     * @return Article
163
     */
164
    public function fill(Article $article): Article
165
    {
166
        $article->title = $this->title;
167
        //$article-
168
        $article->image = reset($this->images);
169
170
        $article->preview_source = '';
171
        $article->preview_rendered = $this->preview;
172
173
        $article->content_source = '';
174
        $article->content_rendered = $this->body;
175
176
        $article->published_at = $this->createdAt;
177
178
        $article->status = Article\Status::PUBLISHED;
179
180
        return $article;
181
    }
182
}
183