Completed
Push — master ( 2630e0...1e98be )
by Freek
01:46
created

Feed::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 1
1
<?php
2
3
namespace Spatie\Feed;
4
5
use Illuminate\Http\Response;
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
        if (! str_contains($this->getFeedMethod(), '@')) {
18
            throw InvalidConfiguration::delimiterNotPresent($this->getFeedMethod());
19
        }
20
    }
21
22
    public function getFeedResponse()
23
    {
24
        return response($this->getFeedContent(), 200, ['Content-Type' => 'application/xml;charset=UTF-8']);
25
    }
26
27
    public function getFeedContent()
28
    {
29
        list($class, $method) = explode('@', $this->getFeedMethod());
30
31
        $items = app($class)->{$method}($this->getFeedArgument());
32
33
        $meta = ['id' => url($this->feedConfiguration['url']), 'link' => url($this->feedConfiguration['url']), 'title' => $this->feedConfiguration['title'], 'updated' => $this->getLastUpdatedDate($items)];
34
35
        return view('laravel-feed::feed', compact('meta', 'items'))->render();
0 ignored issues
show
Bug introduced by
The method render does only exist in Illuminate\View\View, but not in Illuminate\Contracts\View\Factory.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
36
    }
37
38
    protected function getFeedMethod()
39
    {
40
        return is_array($this->feedConfiguration['items']) ? $this->feedConfiguration['items'][0] : $this->feedConfiguration['items'];
41
    }
42
43
    protected function getFeedArgument()
44
    {
45
        return is_array($this->feedConfiguration['items']) ? $this->feedConfiguration['items'][1] : null;
46
    }
47
48
    protected function getLastUpdatedDate(Collection $items)
49
    {
50
        if (! count($items)) {
51
            return '';
52
        }
53
54
        $lastItem = $items->sortBy(function (FeedItem $feedItem) {
55
            return $feedItem->getFeedItemUpdated()->format('YmdHis');
56
        })->last();
57
58
        return $lastItem->getFeedItemUpdated()->toAtomString();
59
    }
60
}
61