Completed
Push — master ( 1ae71a...7ee79f )
by Vitaliy
07:15 queued 10s
created

EloquentProcessingService   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 91.67%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
c 1
b 0
f 0
lcom 1
cbo 3
dl 0
loc 35
ccs 11
cts 12
cp 0.9167
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A beforeOperations() 0 4 1
A count() 0 6 1
A afterOperations() 0 9 3
1
<?php
2
3
namespace ViewComponents\Eloquent;
4
5
use ArrayIterator;
6
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
7
use Illuminate\Database\Query\Builder;
8
use RuntimeException;
9
use Traversable;
10
use ViewComponents\ViewComponents\Data\ProcessingService\AbstractProcessingService;
11
12
/**
13
 * Class DbTableProcessingManager.
14
 */
15
class EloquentProcessingService extends AbstractProcessingService
16
{
17
    /**
18
     * @param Builder|EloquentBuilder $data
19
     * @return Traversable
20
     */
21 14
    protected function afterOperations($data)
22
    {
23 14
        if ($data instanceof EloquentBuilder) {
24 14
            return $data->get();
25 1
        } elseif ($data instanceof Builder) {
26 1
            return new ArrayIterator($data->get());
27
        }
28
        throw new RuntimeException('Unsupported type of data source.');
29
    }
30
31
    /**
32
     * @param Builder|EloquentBuilder $data
33
     * @return Builder|EloquentBuilder
34
     */
35 21
    protected function beforeOperations($data)
36
    {
37 21
        return clone $data;
38
    }
39
40
    /**
41
     * @return int
42
     */
43 7
    public function count()
44
    {
45 7
        return $this->applyOperations(
46 7
            $this->beforeOperations($this->dataSource)
47 7
        )->count();
48
    }
49
}
50