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

EloquentRedirector   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 3
lcom 1
cbo 2
dl 0
loc 45
ccs 13
cts 13
cp 1
rs 10
c 1
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getRedirectedUrls() 0 9 1
A getCachedRedirects() 0 6 1
A getRedirectModel() 0 6 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