Passed
Push — master ( 2b1d34...f57e1f )
by
unknown
02:32
created

AdvertItemRepositoryEloquent::getItemsByCode()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 39

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 5

Importance

Changes 0
Metric Value
dl 0
loc 39
ccs 18
cts 18
cp 1
rs 8.9848
c 0
b 0
f 0
cc 5
nc 5
nop 4
crap 5
1
<?php
2
3
/*
4
 * This file is part of ibrand/advert.
5
 *
6
 * (c) iBrand <https://www.ibrand.cc>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace iBrand\Component\Advert\Repositories\Eloquent;
13
14
use iBrand\Component\Advert\Models\AdvertItem;
15
use iBrand\Component\Advert\Repositories\AdvertItemRepository;
16
use Prettus\Repository\Eloquent\BaseRepository;
17
use Prettus\Repository\Traits\CacheableRepository;
18
use DB;
19
20
class AdvertItemRepositoryEloquent extends BaseRepository implements AdvertItemRepository
21
{
22
    use CacheableRepository;
23
24
    /**
25
     * Specify Model class name.
26
     *
27
     * @return string
28
     */
29 4
    public function model()
30
    {
31 4
        return AdvertItem::class;
32
    }
33
34
    /**
35
     * @param array $attributes
36
     * @param int $parentId
37
     * @return mixed
38
     * @throws \Prettus\Validator\Exceptions\ValidatorException
39
     */
40 1
    public function create(array $attributes, $parentId = 0)
41
    {
42 1
        if ($parentId) {
43 1
            $attributes['parent_id'] = $parentId;
44
        }
45
46 1
        return parent::create($attributes);
47
    }
48
49
50
    public function getItemsByCode($code,$associate_with = [],$depth = 0, $status = 1)
51
    {
52
53 3
        $advert = $this->whereHas('advert', function ($query) use ($code) {
0 ignored issues
show
Documentation introduced by
function ($query) use($c...odel()::STATUS_OPEN); } is of type object<Closure>, but the function expects a object<Prettus\Repository\Eloquent\closure>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
54 3
            return $query->where('code', $code)->where('status', $this->model()::STATUS_OPEN);
55 3
        })->first();
56
57 3
        if (!$advert) {
58 1
            return null;
59
        }
60
61 3
        $query = $this->model->with('associate');
62
63 3
        if (count($associate_with)>0) {
64
65 1
            foreach ($associate_with as $with){
66
67 1
                $query = $query->with('associate.'.$with);
68
            }
69
70
        }
71
72 3
        $query = $query->where('advert_id', $advert->advert_id)
73 3
            ->where('status', $status)
74 3
            ->orderBy('sort');
75
76 3
        if (!$depth) {
77 3
            $query = $query->get();
78
        } else {
79 1
            $sub = $this->model->withDepth();
80
81 1
            $query = $query->from(DB::raw("({$sub->toSql()}) as sub"))
82 1
                ->where('depth', '<', $depth)->get();
83
        }
84
85 3
        return $query->toTree();
86
87
88
    }
89
}
90