Completed
Push — master ( 846842...c3c945 )
by ARCANEDEV
11s
created

EloquentRedirector::getCachedRedirects()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4285
c 1
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
crap 1
1
<?php namespace Arcanedev\LaravelSeo\Redirectors;
2
3
use Arcanedev\LaravelSeo\Contracts\Redirector;
4
use Arcanedev\LaravelSeo\Models\Redirect;
5
6
/**
7
 * Class     EloquentRedirector
8
 *
9
 * @package  Arcanedev\LaravelSeo\Redirectors
10
 * @author   ARCANEDEV <[email protected]>
11
 */
12
class EloquentRedirector extends AbstractRedirector implements Redirector
13
{
14
    /* -----------------------------------------------------------------
15
     |  Main Methods
16
     | -----------------------------------------------------------------
17
     */
18
    /**
19
     * Get the redirected URLs.
20
     *
21
     * @return array
22
     */
23 3
    public function getRedirectedUrls()
24
    {
25 3
        return $this->getCachedRedirects()
26 3
            ->keyBy('old_url')
27
            ->transform(function (Redirect $item) {
28 3
                return [$item->new_url, $item->status];
29 3
            })
30 3
            ->toArray();
31
    }
32
33
    /**
34
     * Get the cached redirection urls.
35
     *
36
     * @return \Illuminate\Database\Eloquent\Collection
37
     */
38
    protected function getCachedRedirects()
39
    {
40 3
        return cache()->remember($this->getOption('cache.key'), $this->getOption('cache.duration'), function () {
41 3
            return $this->getRedirectModel()->get();
42 3
        });
43
    }
44
45
    /**
46
     * Get the redirect model.
47
     *
48
     * @return \Arcanedev\LaravelSeo\Models\Redirect
49
     */
50 3
    private function getRedirectModel()
51
    {
52 3
        return app(
53 3
            $this->getOption('model', \Arcanedev\LaravelSeo\Models\Redirect::class)
54 1
        );
55
    }
56
}
57