Completed
Pull Request — master (#1)
by Elf
03:46 queued 02:36
created

Datatables::getConfiguredEngineClass()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php
2
3
namespace ElfSundae\Laravel\DataTables;
4
5
use Illuminate\Support\Collection;
6
use Yajra\DataTables\DataTables as BaseDataTables;
7
8
class Datatables extends BaseDataTables
9
{
10
    /**
11
     * Create an engine instance with configured class.
12
     *
13
     * @param  string  $type
14
     * @param  mixed  $source
15
     * @return \Yajra\DataTables\Engines\BaseEngine|null
16
     */
17
    protected function createEngineForType($type, $source)
18
    {
19
        if ($class = $this->getConfiguredEngineClass($type)) {
20
            return new $class($source, $this->request);
21
        }
22
    }
23
24
    /**
25
     * Get the configured engine class.
26
     *
27
     * @param  string  $type
28
     * @return string
29
     */
30
    protected function getConfiguredEngineClass($type)
31
    {
32
        return app('config')->get('datatables.engines.'.$type);
33
    }
34
35
    /**
36
     * {@inheritdoc}
37
     */
38
    public function queryBuilder($builder)
39
    {
40
        return $this->createEngineForType('query', $builder) ?:
41
            parent::queryBuilder($builder);
42
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47
    public function eloquent($builder)
48
    {
49
        return $this->createEngineForType('eloquent', $builder) ?:
50
            parent::eloquent($builder);
51
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56
    public function collection($collection)
57
    {
58
        if (is_array($collection)) {
59
            $collection = new Collection($collection);
60
        }
61
62
        return $this->createEngineForType('collection', $collection) ?:
63
            parent::collection($collection);
64
    }
65
}
66