Completed
Push — master ( 882deb...45265e )
by Sherif
03:17
created

ReportRepository::find()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 16
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 16
rs 9.4285
cc 3
eloc 7
nc 4
nop 3
1
<?php namespace App\Modules\V1\Reporting\Repositories;
2
3
use App\Modules\V1\Core\AbstractRepositories\AbstractRepository;
4
5
class ReportRepository extends AbstractRepository
6
{
7
    /**
8
     * Return the model full namespace.
9
     * 
10
     * @return string
11
     */
12
    protected function getModel()
13
    {
14
        return 'App\Modules\V1\Reporting\Report';
15
    }
16
17
    /**
18
     * Render the given report db view based on the given
19
     * condition.
20
     *
21
     * @param  string  $reportName
22
     * @param  array   $conditions array of conditions
23
     * @param  integer $perPage
24
     * @param  array   $relations
25
     * @return object
26
     */
27
    public function getReport($reportName, $conditions = false, $perPage = 0, $relations = [])
28
    {
29
        /**
30
         * Fetch the report from db.
31
         */
32
        $reportConditions = $this->constructConditions(['report_name' => $reportName]);
33
        $report           = call_user_func_array("{$this->getModel()}::with", array($relations))->whereRaw($reportConditions['conditionString'], $reportConditions['conditionValues'])->first();
34
        
35
        /**
36
         * Check report existance and permission.
37
         */
38
        if ( ! $report) 
39
        {
40
            \ErrorHandler::notFound('report');
41
        }
42
        else if ( ! \Core::users()->can($report->view_name, 'reports'))
43
        {
44
            \ErrorHandler::noPermissions();
45
        }
46
47
        /**
48
         * Fetch data from the report based on the given conditions.
49
         */
50
        $report = \DB::table($report->view_name);
51
        unset($conditions['page']);
52
        if (count($conditions))
53
        {
54
            $conditions = $this->constructConditions($conditions);
0 ignored issues
show
Security Bug introduced by
It seems like $conditions can also be of type false; however, App\Modules\V1\Core\Abst...::constructConditions() does only seem to accept array, did you maybe forget to handle an error condition?
Loading history...
55
            $report->whereRaw($conditions['conditionString'], $conditions['conditionValues']);   
56
        }
57
        /**
58
         * Paginate or all data.
59
         */
60
        if ($perPage) 
61
        {
62
            return $report->paginate($perPage);
63
        }
64
        else
65
        {
66
            return $report->get();  
67
        }
68
    }
69
}
70