CategoryableRepository   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 2
dl 0
loc 51
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getModel() 0 4 1
A create() 0 9 2
A getByCategoryId() 0 7 1
A getByProductAndCategoryId() 0 8 1
1
<?php
2
3
namespace App\Repositories;
4
5
use App\Libraries\Categoryable\Categoryable;
6
7
/**
8
 * Class CategoryableRepository
9
 * @package App\Repositories
10
 */
11
class CategoryableRepository extends Repository
12
{
13
    /**
14
     * @return Categoryable
15
     */
16
    public function getModel()
17
    {
18
        return new Categoryable();
19
    }
20
21
    /**
22
     * @param $category
23
     * @param $categoryable
24
     * @return Categoryable
25
     */
26
    public function create($category, $categoryable)
27
    {
28
        return self::getModel()
29
            ->create([
30
                'categoryable_id' => $categoryable->id,
31
                'categoryable_type' => get_class($categoryable),
32
                'category_id' => is_numeric($category) ? $category : $category->id
33
            ]);
34
    }
35
36
    /**
37
     * @param $id
38
     * @return mixed
39
     */
40
    public function getByCategoryId($id)
41
    {
42
        return self::getModel()
0 ignored issues
show
Documentation Bug introduced by
The method where does not exist on object<App\Libraries\Categoryable\Categoryable>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
43
            ->where('category_id', $id)
44
            ->active()
45
            ->first();
46
    }
47
48
    /**
49
     * @param $product
50
     * @param $category_id
51
     * @return mixed
52
     */
53
    public function getByProductAndCategoryId($product, $category_id)
54
    {
55
        return self::getModel()
0 ignored issues
show
Documentation Bug introduced by
The method where does not exist on object<App\Libraries\Categoryable\Categoryable>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
56
            ->where('categoryable_id', $product->id)
57
            ->where('categoryable_type', get_class($product))
58
            ->where('category_id', $category_id)
59
            ->first();
60
    }
61
}