Completed
Push — master ( a449a7...7f0061 )
by Gino
12:11
created

UrlHelperTrait   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 0
dl 0
loc 46
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A setUrls() 0 8 3
A setPostUrls() 0 10 3
1
<?php
2
3
namespace GinoPane\BlogTaxonomy\Components;
4
5
use RainLab\Blog\Models\Post;
6
7
/**
8
 * Trait UrlHelperTrait
9
 *
10
 * @package GinoPane\BlogTaxonomy\Components
11
 */
12
trait UrlHelperTrait
13
{
14
    /**
15
     * Reference to the page name for linking to posts
16
     *
17
     * @var string
18
     */
19
    protected $postPage;
20
21
    /**
22
     * Reference to the page name for linking to categories
23
     *
24
     * @var string
25
     */
26
    protected $categoryPage;
27
28
    /**
29
     * @param $items
30
     * @param $urlPage
31
     * @param $controller
32
     */
33
    public function setUrls($items, $urlPage, $controller)
34
    {
35
        if ($items) {
36
            foreach ($items as $item) {
37
                $item->setUrl($urlPage, $controller);
38
            }
39
        }
40
    }
41
42
    /**
43
     * Set Urls to posts
44
     *
45
     * @param Post $post
46
     */
47
    public function setPostUrls(Post $post)
48
    {
49
        $post->setUrl($this->postPage, $this->controller);
0 ignored issues
show
Bug introduced by
The property controller does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
50
51
        if ($post && $post->categories->count()) {
52
            $post->categories->each(function ($category) {
53
                $category->setUrl($this->categoryPage, $this->controller);
54
            });
55
        }
56
    }
57
}