Code Duplication    Length = 65-76 lines in 2 locations

src/Models/Traits/HasBlocks.php 1 location

@@ 7-71 (lines=65) @@
4
5
use Larafolio\Models\TextBlock;
6
7
trait HasBlocks
8
{
9
    /**
10
     * Get a model from a relationship by model name.
11
     *
12
     * @param string $relationship Name of relationship.
13
     * @param string $name         Name of model to get.
14
     *
15
     * @return \Illuminate\Database\Eloquent\Model|null
16
     */
17
    abstract protected function getFromRelationshipByName($relationship, $name);
18
19
    /**
20
     * A project has many text blocks.
21
     *
22
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
23
     */
24
    public function blocks()
25
    {
26
        return $this->hasMany(TextBlock::class);
27
    }
28
29
    /**
30
     * Return true if project has blocks.
31
     *
32
     * @return bool
33
     */
34
    public function hasBlocks()
35
    {
36
        return !$this->blocks->isEmpty();
37
    }
38
39
    /**
40
     * Get a text block by name, if exists.
41
     *
42
     * @param string $name Name of text block to get.
43
     *
44
     * @return Larafolio\Models\TextBlock|null
45
     */
46
    public function block($name)
47
    {
48
        return $this->getFromRelationshipByName('blocks', $name);
49
    }
50
51
    /**
52
     * Get block text by block name, if block exists.
53
     *
54
     * @param string $name      Name of text block to get.
55
     * @param bool   $formatted If true, return formmated text.
56
     *
57
     * @return string|null
58
     */
59
    public function blockText($name, $formatted = true)
60
    {
61
        if (!$block = $this->block($name)) {
62
            return;
63
        }
64
65
        if ($formatted) {
66
            return $block->formattedText();
67
        }
68
69
        return $block->text();
70
    }
71
}
72

src/Models/Traits/HasLinks.php 1 location

@@ 7-82 (lines=76) @@
4
5
use Larafolio\Models\Link;
6
7
trait HasLinks
8
{
9
    /**
10
     * Get a model from a relationship by model name.
11
     *
12
     * @param string $relationship Name of relationship.
13
     * @param string $name         Name of model to get.
14
     *
15
     * @return \Illuminate\Database\Eloquent\Model|null
16
     */
17
    abstract protected function getFromRelationshipByName($relationship, $name);
18
19
    /**
20
     * A project has many links.
21
     *
22
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
23
     */
24
    public function links()
25
    {
26
        return $this->hasMany(Link::class);
27
    }
28
29
    /**
30
     * Return true if project has links.
31
     *
32
     * @return bool
33
     */
34
    public function hasLinks()
35
    {
36
        return !$this->links->isEmpty();
37
    }
38
39
    /**
40
     * Get link by name, if exists.
41
     *
42
     * @param string $name Name of link to get.
43
     *
44
     * @return Larafolio\Models\Link|null
45
     */
46
    public function link($name)
47
    {
48
        return $this->getFromRelationshipByName('links', $name);
49
    }
50
51
    /**
52
     * Get link url.
53
     *
54
     * @param string $name Name of link.
55
     *
56
     * @return string|null
57
     */
58
    public function linkUrl($name)
59
    {
60
        if (!$link = $this->link($name)) {
61
            return;
62
        }
63
64
        return $link->url();
65
    }
66
67
    /**
68
     * Get link text.
69
     *
70
     * @param string $name Name of link.
71
     *
72
     * @return string|null
73
     */
74
    public function linkText($name)
75
    {
76
        if (!$link = $this->link($name)) {
77
            return;
78
        }
79
80
        return $link->text();
81
    }
82
}
83