Completed
Push — master ( fc2b7e...fee500 )
by Freek
01:52
created

Feed::getLastUpdatedDate()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 2
eloc 8
nc 2
nop 1
dl 0
loc 14
rs 9.4285
1
<?php
2
3
namespace Spatie\Feed;
4
5
use Illuminate\Http\Response as HttpResponse;
6
use Illuminate\Support\Collection;
7
use Spatie\Feed\Exceptions\InvalidConfiguration;
8
9
class Feed
10
{
11
    /** @var array */
12
    protected $feedConfiguration;
13
14
    public function __construct(array $feedConfiguration)
15
    {
16
        $this->feedConfiguration = $feedConfiguration;
17
18
        if (!str_contains($feedConfiguration['items'], '@')) {
19
            throw InvalidConfiguration::delimiterNotPresent($feedConfiguration['items']);
20
        }
21
    }
22
23
    public function getFeedResponse() : HttpResponse
24
    {
25
        $feedContent = $this->getFeedContent($this->feedConfiguration);
26
27
        return response($feedContent, 200, ['Content-Type' => 'application/xml;charset=UTF-8']);
28
    }
29
30
    public function getFeedContent() : string
31
    {
32
        list($class, $method) = explode('@', $this->feedConfiguration['items']);
33
34
        $items = app($class)->$method();
35
36
        $meta = [
37
            'link' => $this->feedConfiguration['url'],
38
            'description' => $this->feedConfiguration['description'],
39
            'title' => $this->feedConfiguration['title'],
40
            'updated' => $this->getLastUpdatedDate($items),
41
        ];
42
43
        base_path()
44
45
        return view('laravel-feed::feed', compact('meta', 'items'))->render();
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected T_RETURN
Loading history...
46
    }
47
48
    protected function getLastUpdatedDate(Collection $items) : string
49
    {
50
        if (!count($items)) {
51
            return '';
52
        }
53
54
        $lastItem = $items
55
            ->sortBy(function (FeedItem $feedItem) {
56
                return $feedItem->getFeedItemUpdated()->format('YmdHis');
57
            })
58
            ->last();
59
60
        return $lastItem->getFeedItemUpdated()->toAtomString();
61
    }
62
}
63